NullAdapterTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 PHPUnit\Framework\TestCase;
  12. use Psr\Cache\CacheItemInterface;
  13. use Symfony\Component\Cache\Adapter\NullAdapter;
  14. /**
  15. * @group time-sensitive
  16. */
  17. class NullAdapterTest extends TestCase
  18. {
  19. public function createCachePool()
  20. {
  21. return new NullAdapter();
  22. }
  23. public function testGetItem()
  24. {
  25. $adapter = $this->createCachePool();
  26. $item = $adapter->getItem('key');
  27. $this->assertFalse($item->isHit());
  28. $this->assertNull($item->get(), "Item's value must be null when isHit is false.");
  29. }
  30. public function testGet()
  31. {
  32. $adapter = $this->createCachePool();
  33. $fetched = [];
  34. $adapter->get('myKey', function ($item) use (&$fetched) { $fetched[] = $item; });
  35. $this->assertCount(1, $fetched);
  36. $item = $fetched[0];
  37. $this->assertFalse($item->isHit());
  38. $this->assertNull($item->get(), "Item's value must be null when isHit is false.");
  39. $this->assertSame('myKey', $item->getKey());
  40. }
  41. public function testHasItem()
  42. {
  43. $this->assertFalse($this->createCachePool()->hasItem('key'));
  44. }
  45. public function testGetItems()
  46. {
  47. $adapter = $this->createCachePool();
  48. $keys = ['foo', 'bar', 'baz', 'biz'];
  49. /** @var CacheItemInterface[] $items */
  50. $items = $adapter->getItems($keys);
  51. $count = 0;
  52. foreach ($items as $key => $item) {
  53. $itemKey = $item->getKey();
  54. $this->assertEquals($itemKey, $key, 'Keys must be preserved when fetching multiple items');
  55. $this->assertContains($key, $keys, 'Cache key can not change.');
  56. $this->assertFalse($item->isHit());
  57. // Remove $key for $keys
  58. foreach ($keys as $k => $v) {
  59. if ($v === $key) {
  60. unset($keys[$k]);
  61. }
  62. }
  63. ++$count;
  64. }
  65. $this->assertSame(4, $count);
  66. }
  67. public function testIsHit()
  68. {
  69. $adapter = $this->createCachePool();
  70. $item = $adapter->getItem('key');
  71. $this->assertFalse($item->isHit());
  72. }
  73. public function testClear()
  74. {
  75. $this->assertTrue($this->createCachePool()->clear());
  76. }
  77. public function testDeleteItem()
  78. {
  79. $this->assertTrue($this->createCachePool()->deleteItem('key'));
  80. }
  81. public function testDeleteItems()
  82. {
  83. $this->assertTrue($this->createCachePool()->deleteItems(['key', 'foo', 'bar']));
  84. }
  85. public function testSave()
  86. {
  87. $adapter = $this->createCachePool();
  88. $item = $adapter->getItem('key');
  89. $this->assertFalse($item->isHit());
  90. $this->assertNull($item->get(), "Item's value must be null when isHit is false.");
  91. $this->assertFalse($adapter->save($item));
  92. }
  93. public function testDeferredSave()
  94. {
  95. $adapter = $this->createCachePool();
  96. $item = $adapter->getItem('key');
  97. $this->assertFalse($item->isHit());
  98. $this->assertNull($item->get(), "Item's value must be null when isHit is false.");
  99. $this->assertFalse($adapter->saveDeferred($item));
  100. }
  101. public function testCommit()
  102. {
  103. $adapter = $this->createCachePool();
  104. $item = $adapter->getItem('key');
  105. $this->assertFalse($item->isHit());
  106. $this->assertNull($item->get(), "Item's value must be null when isHit is false.");
  107. $this->assertFalse($adapter->saveDeferred($item));
  108. $this->assertFalse($this->createCachePool()->commit());
  109. }
  110. }