PhpArrayAdapterTest.php 4.8 KB

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