CustomNamespaces.test.php 3.8 KB

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