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+ file_put_contents ("{$ testDir }Driver.php " , $ driverClassString );
92+ file_put_contents ("{$ testDir }Item.php " , $ itemClassString );
93+
94+ /**
95+ * Then adjust the Chmod
96+ */
97+ chmod ("{$ testDir }Driver.php " , 0644 );
98+ chmod ("{$ testDir }Item.php " , 0644 );
99+
100+ CacheManager::setNamespacePath (phpFastCache \CustomDriversPath::class . '\\' );
101+ $ cacheInstance = CacheManager::getInstance ('Files2 ' , []);
102+ $ cacheKey = 'cacheKey ' ;
103+ $ RandomCacheValue = str_shuffle (uniqid ('pfc ' , true ));
104+
105+ /**
106+ * Existing cache item test
107+ */
108+ $ cacheItem = $ cacheInstance ->getItem ($ cacheKey );
109+ $ RandomCacheValue = str_shuffle (uniqid ('pfc ' , true ));
110+ $ cacheItem ->set ($ RandomCacheValue );
111+ $ cacheInstance ->save ($ cacheItem );
112+
113+ /**
114+ * Remove objects references
115+ */
116+ $ cacheInstance ->detachAllItems ();
117+ unset($ cacheItem );
118+
119+ $ cacheValue = (new CacheConditional ($ cacheInstance ))->get ($ cacheKey , function () use ($ cacheKey , $ testHelper , $ RandomCacheValue ){
120+ /**
121+ * No parameter are passed
122+ * to this closure
123+ */
124+ $ testHelper ->printFailText ('Unexpected closure call. ' );
125+ return $ RandomCacheValue . '-1337 ' ;
126+ });
127+
128+ if ($ cacheValue === $ RandomCacheValue ){
129+ $ testHelper ->printPassText (sprintf ('The cache promise successfully returned expected value "%s". ' , $ cacheValue ));
130+ }else {
131+ $ testHelper ->printFailText (sprintf ('The cache promise returned an unexpected value "%s". ' , $ cacheValue ));
132+ }
133+
134+ $ cacheInstance ->clear ();
135+ $ testHelper ->terminateTest ();
0 commit comments