NullAdapterTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 testHasItem()
  31. {
  32. $this->assertFalse($this->createCachePool()->hasItem('key'));
  33. }
  34. public function testGetItems()
  35. {
  36. $adapter = $this->createCachePool();
  37. $keys = ['foo', 'bar', 'baz', 'biz'];
  38. /** @var CacheItemInterface[] $items */
  39. $items = $adapter->getItems($keys);
  40. $count = 0;
  41. foreach ($items as $key => $item) {
  42. $itemKey = $item->getKey();
  43. $this->assertEquals($itemKey, $key, 'Keys must be preserved when fetching multiple items');
  44. $this->assertContains($key, $keys, 'Cache key can not change.');
  45. $this->assertFalse($item->isHit());
  46. // Remove $key for $keys
  47. foreach ($keys as $k => $v) {
  48. if ($v === $key) {
  49. unset($keys[$k]);
  50. }
  51. }
  52. ++$count;
  53. }
  54. $this->assertSame(4, $count);
  55. }
  56. public function testIsHit()
  57. {
  58. $adapter = $this->createCachePool();
  59. $item = $adapter->getItem('key');
  60. $this->assertFalse($item->isHit());
  61. }
  62. public function testClear()
  63. {
  64. $this->assertTrue($this->createCachePool()->clear());
  65. }
  66. public function testDeleteItem()
  67. {
  68. $this->assertTrue($this->createCachePool()->deleteItem('key'));
  69. }
  70. public function testDeleteItems()
  71. {
  72. $this->assertTrue($this->createCachePool()->deleteItems(['key', 'foo', 'bar']));
  73. }
  74. public function testSave()
  75. {
  76. $adapter = $this->createCachePool();
  77. $item = $adapter->getItem('key');
  78. $this->assertFalse($item->isHit());
  79. $this->assertNull($item->get(), "Item's value must be null when isHit is false.");
  80. $this->assertFalse($adapter->save($item));
  81. }
  82. public function testDeferredSave()
  83. {
  84. $adapter = $this->createCachePool();
  85. $item = $adapter->getItem('key');
  86. $this->assertFalse($item->isHit());
  87. $this->assertNull($item->get(), "Item's value must be null when isHit is false.");
  88. $this->assertFalse($adapter->saveDeferred($item));
  89. }
  90. public function testCommit()
  91. {
  92. $adapter = $this->createCachePool();
  93. $item = $adapter->getItem('key');
  94. $this->assertFalse($item->isHit());
  95. $this->assertNull($item->get(), "Item's value must be null when isHit is false.");
  96. $this->assertFalse($adapter->saveDeferred($item));
  97. $this->assertFalse($this->createCachePool()->commit());
  98. }
  99. }