PhpArrayAdapterTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\Adapter;
  11. use Psr\Cache\CacheItemInterface;
  12. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  13. use Symfony\Component\Cache\Adapter\NullAdapter;
  14. use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
  15. /**
  16. * @group time-sensitive
  17. */
  18. class PhpArrayAdapterTest extends AdapterTestCase
  19. {
  20. protected $skippedTests = [
  21. 'testGet' => 'PhpArrayAdapter is read-only.',
  22. 'testRecursiveGet' => 'PhpArrayAdapter is read-only.',
  23. 'testBasicUsage' => 'PhpArrayAdapter is read-only.',
  24. 'testBasicUsageWithLongKey' => 'PhpArrayAdapter is read-only.',
  25. 'testClear' => 'PhpArrayAdapter is read-only.',
  26. 'testClearWithDeferredItems' => 'PhpArrayAdapter is read-only.',
  27. 'testDeleteItem' => 'PhpArrayAdapter is read-only.',
  28. 'testSaveExpired' => 'PhpArrayAdapter is read-only.',
  29. 'testSaveWithoutExpire' => 'PhpArrayAdapter is read-only.',
  30. 'testDeferredSave' => 'PhpArrayAdapter is read-only.',
  31. 'testDeferredSaveWithoutCommit' => 'PhpArrayAdapter is read-only.',
  32. 'testDeleteItems' => 'PhpArrayAdapter is read-only.',
  33. 'testDeleteDeferredItem' => 'PhpArrayAdapter is read-only.',
  34. 'testCommit' => 'PhpArrayAdapter is read-only.',
  35. 'testSaveDeferredWhenChangingValues' => 'PhpArrayAdapter is read-only.',
  36. 'testSaveDeferredOverwrite' => 'PhpArrayAdapter is read-only.',
  37. 'testIsHitDeferred' => 'PhpArrayAdapter is read-only.',
  38. 'testExpiresAt' => 'PhpArrayAdapter does not support expiration.',
  39. 'testExpiresAtWithNull' => 'PhpArrayAdapter does not support expiration.',
  40. 'testExpiresAfterWithNull' => 'PhpArrayAdapter does not support expiration.',
  41. 'testDeferredExpired' => 'PhpArrayAdapter does not support expiration.',
  42. 'testExpiration' => 'PhpArrayAdapter does not support expiration.',
  43. 'testGetItemInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
  44. 'testGetItemsInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
  45. 'testHasItemInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
  46. 'testDeleteItemInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
  47. 'testDeleteItemsInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
  48. 'testDefaultLifeTime' => 'PhpArrayAdapter does not allow configuring a default lifetime.',
  49. 'testPrune' => 'PhpArrayAdapter just proxies',
  50. ];
  51. protected static $file;
  52. public static function setUpBeforeClass(): void
  53. {
  54. self::$file = sys_get_temp_dir().'/symfony-cache/php-array-adapter-test.php';
  55. }
  56. protected function tearDown(): void
  57. {
  58. if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
  59. FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
  60. }
  61. }
  62. public function createCachePool($defaultLifetime = 0, $testMethod = null)
  63. {
  64. if ('testGetMetadata' === $testMethod) {
  65. return new PhpArrayAdapter(self::$file, new FilesystemAdapter());
  66. }
  67. return new PhpArrayAdapterWrapper(self::$file, new NullAdapter());
  68. }
  69. public function testStore()
  70. {
  71. $arrayWithRefs = [];
  72. $arrayWithRefs[0] = 123;
  73. $arrayWithRefs[1] = &$arrayWithRefs[0];
  74. $object = (object) [
  75. 'foo' => 'bar',
  76. 'foo2' => 'bar2',
  77. ];
  78. $expected = [
  79. 'null' => null,
  80. 'serializedString' => serialize($object),
  81. 'arrayWithRefs' => $arrayWithRefs,
  82. 'object' => $object,
  83. 'arrayWithObject' => ['bar' => $object],
  84. ];
  85. $adapter = $this->createCachePool();
  86. $adapter->warmUp($expected);
  87. foreach ($expected as $key => $value) {
  88. $this->assertSame(serialize($value), serialize($adapter->getItem($key)->get()), 'Warm up should create a PHP file that OPCache can load in memory');
  89. }
  90. }
  91. public function testStoredFile()
  92. {
  93. $data = [
  94. 'integer' => 42,
  95. 'float' => 42.42,
  96. 'boolean' => true,
  97. 'array_simple' => ['foo', 'bar'],
  98. 'array_associative' => ['foo' => 'bar', 'foo2' => 'bar2'],
  99. ];
  100. $expected = [
  101. [
  102. 'integer' => 0,
  103. 'float' => 1,
  104. 'boolean' => 2,
  105. 'array_simple' => 3,
  106. 'array_associative' => 4,
  107. ],
  108. [
  109. 0 => 42,
  110. 1 => 42.42,
  111. 2 => true,
  112. 3 => ['foo', 'bar'],
  113. 4 => ['foo' => 'bar', 'foo2' => 'bar2'],
  114. ],
  115. ];
  116. $adapter = $this->createCachePool();
  117. $adapter->warmUp($data);
  118. $values = eval(substr(file_get_contents(self::$file), 6));
  119. $this->assertSame($expected, $values, 'Warm up should create a PHP file that OPCache can load in memory');
  120. }
  121. }
  122. class PhpArrayAdapterWrapper extends PhpArrayAdapter
  123. {
  124. protected $data = [];
  125. public function save(CacheItemInterface $item)
  126. {
  127. (\Closure::bind(function () use ($item) {
  128. $key = $item->getKey();
  129. $this->keys[$key] = $id = \count($this->values);
  130. $this->data[$key] = $this->values[$id] = $item->get();
  131. $this->warmUp($this->data);
  132. list($this->keys, $this->values) = eval(substr(file_get_contents($this->file), 6));
  133. }, $this, PhpArrayAdapter::class))();
  134. return true;
  135. }
  136. }