1+ <?php
2+
3+ /**
4+ * @author Khoa Bui (khoaofgod) <[email protected] > http://www.phpfastcache.com 5+ * @author Georges.L (Geolim4) <[email protected] > 6+ */
7+
8+ use phpFastCache \CacheManager ;
9+ use phpFastCache \Helper \CacheConditionalHelper as CacheConditional ;
10+ use phpFastCache \Helper \TestHelper ;
11+ use Psr \Cache \CacheItemPoolInterface ;
12+
13+ chdir (__DIR__ );
14+ require_once __DIR__ . '/../src/autoload.php ' ;
15+ $ testHelper = new TestHelper ('Custom namespaces ' );
16+
17+ $ testDir = __DIR__ . '/../src/phpFastCache/CustomDriversPath/Files2/ ' ;
18+
19+ if (@!mkdir ($ testDir , 0777 , true ) && !is_dir ($ testDir ))
20+ {
21+ $ testHelper ->printFailText ('Cannot create CustomDriversPath directory ' );
22+ $ testHelper ->terminateTest ();
23+ }
24+
25+ /**
26+ * The driver class string
27+ */
28+ $ driverClassString = <<<DRIVER_CLASS_STRING
29+ <?php
30+ /**
31+ *
32+ * This file is part of phpFastCache.
33+ *
34+ * @license MIT License (MIT)
35+ *
36+ * For full copyright and license information, please see the docs/CREDITS.txt file.
37+ *
38+ * @author Khoa Bui (khoaofgod) <[email protected] > http://www.phpfastcache.com 39+ * @author Georges.L (Geolim4) <[email protected] > 40+ *
41+ */
42+
43+ namespace phpFastCache\CustomDriversPath\Files2;
44+ use phpFastCache\Drivers\Files\Driver as FilesDriver;
45+
46+ /**
47+ * Class Driver
48+ * @package phpFastCache\CustomDriversPath\Files2
49+ */
50+ class Driver extends FilesDriver
51+ {
52+
53+ }
54+ DRIVER_CLASS_STRING ;
55+
56+ /**
57+ * The item class string
58+ */
59+ $ itemClassString = <<<ITEM_CLASS_STRING
60+ <?php
61+ /**
62+ *
63+ * This file is part of phpFastCache.
64+ *
65+ * @license MIT License (MIT)
66+ *
67+ * For full copyright and license information, please see the docs/CREDITS.txt file.
68+ *
69+ * @author Khoa Bui (khoaofgod) <[email protected] > http://www.phpfastcache.com 70+ * @author Georges.L (Geolim4) <[email protected] > 71+ *
72+ */
73+
74+ namespace phpFastCache\CustomDriversPath\Files2;
75+ use phpFastCache\Drivers\Files\Item as FilesItem;
76+
77+ /**
78+ * Class Item
79+ * @package phpFastCache\CustomDriversPath\Files2
80+ */
81+ class Item extends FilesItem
82+ {
83+
84+ }
85+ ITEM_CLASS_STRING ;
86+
87+
88+ /**
89+ * Write the files
90+ */
91+
92+
93+ if (!file_put_contents ("{$ testDir }Driver.php " , $ driverClassString )
94+ || !file_put_contents ("{$ testDir }Item.php " , $ itemClassString )
95+ ){
96+ $ testHelper ->printFailText ('The php files of driver "Files2" were not written ' );
97+ $ testHelper ->terminateTest ();
98+ }else {
99+ $ testHelper ->printPassText ('The php files of driver "Files2" were written ' );
100+ }
101+
102+ /**
103+ * Then adjust the Chmod
104+ */
105+ chmod ("{$ testDir }Driver.php " , 0644 );
106+ chmod ("{$ testDir }Item.php " , 0644 );
107+
108+ if (!class_exists (phpFastCache \CustomDriversPath \Files2 \Item::class)
109+ || !class_exists (phpFastCache \CustomDriversPath \Files2 \Driver::class)
110+ ){
111+ $ testHelper ->printFailText ('The php classes of driver "Files2" does not exists ' );
112+ $ testHelper ->terminateTest ();
113+ }else {
114+ $ testHelper ->printPassText ('The php classes of driver "Files2" were found ' );
115+ }
116+
117+ CacheManager::setNamespacePath (phpFastCache \CustomDriversPath::class . '\\' );
118+ $ cacheInstance = CacheManager::getInstance ('Files2 ' , []);
119+ $ cacheKey = 'cacheKey ' ;
120+ $ RandomCacheValue = str_shuffle (uniqid ('pfc ' , true ));
121+
122+ /**
123+ * Existing cache item test
124+ */
125+ $ cacheItem = $ cacheInstance ->getItem ($ cacheKey );
126+ $ RandomCacheValue = str_shuffle (uniqid ('pfc ' , true ));
127+ $ cacheItem ->set ($ RandomCacheValue );
128+ $ cacheInstance ->save ($ cacheItem );
129+
130+ /**
131+ * Remove objects references
132+ */
133+ $ cacheInstance ->detachAllItems ();
134+ unset($ cacheItem );
135+
136+ $ cacheValue = (new CacheConditional ($ cacheInstance ))->get ($ cacheKey , function () use ($ cacheKey , $ testHelper , $ RandomCacheValue ){
137+ /**
138+ * No parameter are passed
139+ * to this closure
140+ */
141+ $ testHelper ->printFailText ('Unexpected closure call. ' );
142+ return $ RandomCacheValue . '-1337 ' ;
143+ });
144+
145+ if ($ cacheValue === $ RandomCacheValue ){
146+ $ testHelper ->printPassText (sprintf ('The cache promise successfully returned expected value "%s". ' , $ cacheValue ));
147+ }else {
148+ $ testHelper ->printFailText (sprintf ('The cache promise returned an unexpected value "%s". ' , $ cacheValue ));
149+ }
150+
151+ $ cacheInstance ->clear ();
152+ $ testHelper ->terminateTest ();
0 commit comments