PhpArrayCacheTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. $this->createSimpleCache()->clear();
  49. if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
  50. FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
  51. }
  52. }
  53. public function createSimpleCache()
  54. {
  55. return new PhpArrayCacheWrapper(self::$file, new NullCache());
  56. }
  57. public function testStore()
  58. {
  59. $arrayWithRefs = [];
  60. $arrayWithRefs[0] = 123;
  61. $arrayWithRefs[1] = &$arrayWithRefs[0];
  62. $object = (object) [
  63. 'foo' => 'bar',
  64. 'foo2' => 'bar2',
  65. ];
  66. $expected = [
  67. 'null' => null,
  68. 'serializedString' => serialize($object),
  69. 'arrayWithRefs' => $arrayWithRefs,
  70. 'object' => $object,
  71. 'arrayWithObject' => ['bar' => $object],
  72. ];
  73. $cache = new PhpArrayCache(self::$file, new NullCache());
  74. $cache->warmUp($expected);
  75. foreach ($expected as $key => $value) {
  76. $this->assertSame(serialize($value), serialize($cache->get($key)), 'Warm up should create a PHP file that OPCache can load in memory');
  77. }
  78. }
  79. public function testStoredFile()
  80. {
  81. $expected = [
  82. 'integer' => 42,
  83. 'float' => 42.42,
  84. 'boolean' => true,
  85. 'array_simple' => ['foo', 'bar'],
  86. 'array_associative' => ['foo' => 'bar', 'foo2' => 'bar2'],
  87. ];
  88. $cache = new PhpArrayCache(self::$file, new NullCache());
  89. $cache->warmUp($expected);
  90. $values = eval(substr(file_get_contents(self::$file), 6));
  91. $this->assertSame($expected, $values, 'Warm up should create a PHP file that OPCache can load in memory');
  92. }
  93. }
  94. class PhpArrayCacheWrapper extends PhpArrayCache
  95. {
  96. public function set($key, $value, $ttl = null)
  97. {
  98. \call_user_func(\Closure::bind(function () use ($key, $value) {
  99. $this->values[$key] = $value;
  100. $this->warmUp($this->values);
  101. $this->values = eval(substr(file_get_contents($this->file), 6));
  102. }, $this, PhpArrayCache::class));
  103. return true;
  104. }
  105. public function setMultiple($values, $ttl = null)
  106. {
  107. if (!\is_array($values) && !$values instanceof \Traversable) {
  108. return parent::setMultiple($values, $ttl);
  109. }
  110. \call_user_func(\Closure::bind(function () use ($values) {
  111. foreach ($values as $key => $value) {
  112. $this->values[$key] = $value;
  113. }
  114. $this->warmUp($this->values);
  115. $this->values = eval(substr(file_get_contents($this->file), 6));
  116. }, $this, PhpArrayCache::class));
  117. return true;
  118. }
  119. }