PhpArrayCacheTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Tests\Simple;
  11. use Symfony\Component\Cache\Simple\NullCache;
  12. use Symfony\Component\Cache\Simple\PhpArrayCache;
  13. use Symfony\Component\Cache\Tests\Adapter\FilesystemAdapterTest;
  14. /**
  15. * @group time-sensitive
  16. */
  17. class PhpArrayCacheTest extends CacheTestCase
  18. {
  19. protected $skippedTests = [
  20. 'testBasicUsageWithLongKey' => 'PhpArrayCache does no writes',
  21. 'testDelete' => 'PhpArrayCache does no writes',
  22. 'testDeleteMultiple' => 'PhpArrayCache does no writes',
  23. 'testDeleteMultipleGenerator' => 'PhpArrayCache does no writes',
  24. 'testSetTtl' => 'PhpArrayCache does no expiration',
  25. 'testSetMultipleTtl' => 'PhpArrayCache does no expiration',
  26. 'testSetExpiredTtl' => 'PhpArrayCache does no expiration',
  27. 'testSetMultipleExpiredTtl' => 'PhpArrayCache does no expiration',
  28. 'testGetInvalidKeys' => 'PhpArrayCache does no validation',
  29. 'testGetMultipleInvalidKeys' => 'PhpArrayCache does no validation',
  30. 'testSetInvalidKeys' => 'PhpArrayCache does no validation',
  31. 'testDeleteInvalidKeys' => 'PhpArrayCache does no validation',
  32. 'testDeleteMultipleInvalidKeys' => 'PhpArrayCache does no validation',
  33. 'testSetInvalidTtl' => 'PhpArrayCache does no validation',
  34. 'testSetMultipleInvalidKeys' => 'PhpArrayCache does no validation',
  35. 'testSetMultipleInvalidTtl' => 'PhpArrayCache does no validation',
  36. 'testHasInvalidKeys' => 'PhpArrayCache does no validation',
  37. 'testSetValidData' => 'PhpArrayCache does no validation',
  38. 'testDefaultLifeTime' => 'PhpArrayCache does not allow configuring a default lifetime.',
  39. 'testPrune' => 'PhpArrayCache just proxies',
  40. ];
  41. protected static $file;
  42. public static function setupBeforeClass()
  43. {
  44. self::$file = sys_get_temp_dir().'/symfony-cache/php-array-adapter-test.php';
  45. }
  46. protected function tearDown()
  47. {
  48. if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
  49. FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
  50. }
  51. }
  52. public function createSimpleCache()
  53. {
  54. return new PhpArrayCacheWrapper(self::$file, new NullCache());
  55. }
  56. public function testStore()
  57. {
  58. $arrayWithRefs = [];
  59. $arrayWithRefs[0] = 123;
  60. $arrayWithRefs[1] = &$arrayWithRefs[0];
  61. $object = (object) [
  62. 'foo' => 'bar',
  63. 'foo2' => 'bar2',
  64. ];
  65. $expected = [
  66. 'null' => null,
  67. 'serializedString' => serialize($object),
  68. 'arrayWithRefs' => $arrayWithRefs,
  69. 'object' => $object,
  70. 'arrayWithObject' => ['bar' => $object],
  71. ];
  72. $cache = new PhpArrayCache(self::$file, new NullCache());
  73. $cache->warmUp($expected);
  74. foreach ($expected as $key => $value) {
  75. $this->assertSame(serialize($value), serialize($cache->get($key)), 'Warm up should create a PHP file that OPCache can load in memory');
  76. }
  77. }
  78. public function testStoredFile()
  79. {
  80. $expected = [
  81. 'integer' => 42,
  82. 'float' => 42.42,
  83. 'boolean' => true,
  84. 'array_simple' => ['foo', 'bar'],
  85. 'array_associative' => ['foo' => 'bar', 'foo2' => 'bar2'],
  86. ];
  87. $cache = new PhpArrayCache(self::$file, new NullCache());
  88. $cache->warmUp($expected);
  89. $values = eval(substr(file_get_contents(self::$file), 6));
  90. $this->assertSame($expected, $values, 'Warm up should create a PHP file that OPCache can load in memory');
  91. }
  92. }
  93. class PhpArrayCacheWrapper extends PhpArrayCache
  94. {
  95. public function set($key, $value, $ttl = null)
  96. {
  97. (\Closure::bind(function () use ($key, $value) {
  98. $this->values[$key] = $value;
  99. $this->warmUp($this->values);
  100. $this->values = eval(substr(file_get_contents($this->file), 6));
  101. }, $this, PhpArrayCache::class))();
  102. return true;
  103. }
  104. public function setMultiple($values, $ttl = null)
  105. {
  106. if (!\is_array($values) && !$values instanceof \Traversable) {
  107. return parent::setMultiple($values, $ttl);
  108. }
  109. (\Closure::bind(function () use ($values) {
  110. foreach ($values as $key => $value) {
  111. $this->values[$key] = $value;
  112. }
  113. $this->warmUp($this->values);
  114. $this->values = eval(substr(file_get_contents($this->file), 6));
  115. }, $this, PhpArrayCache::class))();
  116. return true;
  117. }
  118. }