PhpArrayCacheTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * @group legacy
  17. */
  18. class PhpArrayCacheTest extends CacheTestCase
  19. {
  20. protected $skippedTests = [
  21. 'testBasicUsageWithLongKey' => 'PhpArrayCache does no writes',
  22. 'testDelete' => 'PhpArrayCache does no writes',
  23. 'testDeleteMultiple' => 'PhpArrayCache does no writes',
  24. 'testDeleteMultipleGenerator' => 'PhpArrayCache does no writes',
  25. 'testSetTtl' => 'PhpArrayCache does no expiration',
  26. 'testSetMultipleTtl' => 'PhpArrayCache does no expiration',
  27. 'testSetExpiredTtl' => 'PhpArrayCache does no expiration',
  28. 'testSetMultipleExpiredTtl' => 'PhpArrayCache does no expiration',
  29. 'testGetInvalidKeys' => 'PhpArrayCache does no validation',
  30. 'testGetMultipleInvalidKeys' => 'PhpArrayCache does no validation',
  31. 'testSetInvalidKeys' => 'PhpArrayCache does no validation',
  32. 'testDeleteInvalidKeys' => 'PhpArrayCache does no validation',
  33. 'testDeleteMultipleInvalidKeys' => 'PhpArrayCache does no validation',
  34. 'testSetInvalidTtl' => 'PhpArrayCache does no validation',
  35. 'testSetMultipleInvalidKeys' => 'PhpArrayCache does no validation',
  36. 'testSetMultipleInvalidTtl' => 'PhpArrayCache does no validation',
  37. 'testHasInvalidKeys' => 'PhpArrayCache does no validation',
  38. 'testSetValidData' => 'PhpArrayCache does no validation',
  39. 'testDefaultLifeTime' => 'PhpArrayCache does not allow configuring a default lifetime.',
  40. 'testPrune' => 'PhpArrayCache just proxies',
  41. ];
  42. protected static $file;
  43. public static function setUpBeforeClass(): void
  44. {
  45. self::$file = sys_get_temp_dir().'/symfony-cache/php-array-adapter-test.php';
  46. }
  47. protected function tearDown(): void
  48. {
  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. $data = [
  82. 'integer' => 42,
  83. 'float' => 42.42,
  84. 'boolean' => true,
  85. 'array_simple' => ['foo', 'bar'],
  86. 'array_associative' => ['foo' => 'bar', 'foo2' => 'bar2'],
  87. ];
  88. $expected = [
  89. [
  90. 'integer' => 0,
  91. 'float' => 1,
  92. 'boolean' => 2,
  93. 'array_simple' => 3,
  94. 'array_associative' => 4,
  95. ],
  96. [
  97. 0 => 42,
  98. 1 => 42.42,
  99. 2 => true,
  100. 3 => ['foo', 'bar'],
  101. 4 => ['foo' => 'bar', 'foo2' => 'bar2'],
  102. ],
  103. ];
  104. $cache = new PhpArrayCache(self::$file, new NullCache());
  105. $cache->warmUp($data);
  106. $values = eval(substr(file_get_contents(self::$file), 6));
  107. $this->assertSame($expected, $values, 'Warm up should create a PHP file that OPCache can load in memory');
  108. }
  109. }