|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Cache\Tests\Adapter; |
| 13 | + |
| 14 | +use Psr\Cache\CacheItemInterface; |
| 15 | +use Symfony\Component\Cache\Adapter\NullAdapter; |
| 16 | + |
| 17 | +class NullAdapterTest extends \PHPUnit_Framework_TestCase |
| 18 | +{ |
| 19 | + public function createCachePool() |
| 20 | + { |
| 21 | + return new NullAdapter(); |
| 22 | + } |
| 23 | + |
| 24 | + public function testGetItem() |
| 25 | + { |
| 26 | + $adapter = $this->createCachePool(); |
| 27 | + |
| 28 | + $item = $adapter->getItem('key'); |
| 29 | + $this->assertFalse($item->isHit()); |
| 30 | + $this->assertNull($item->get(), "Item's value must be null when isHit is false."); |
| 31 | + } |
| 32 | + |
| 33 | + public function testHasItem() |
| 34 | + { |
| 35 | + $this->assertFalse($this->createCachePool()->hasItem('key')); |
| 36 | + } |
| 37 | + |
| 38 | + public function testGetItems() |
| 39 | + { |
| 40 | + $adapter = $this->createCachePool(); |
| 41 | + |
| 42 | + $keys = array('foo', 'bar', 'baz', 'biz'); |
| 43 | + |
| 44 | + /** @var CacheItemInterface[] $items */ |
| 45 | + $items = $adapter->getItems($keys); |
| 46 | + $count = 0; |
| 47 | + |
| 48 | + foreach ($items as $key => $item) { |
| 49 | + $itemKey = $item->getKey(); |
| 50 | + |
| 51 | + $this->assertEquals($itemKey, $key, 'Keys must be preserved when fetching multiple items'); |
| 52 | + $this->assertTrue(in_array($key, $keys), 'Cache key can not change.'); |
| 53 | + $this->assertFalse($item->isHit()); |
| 54 | + |
| 55 | + // Remove $key for $keys |
| 56 | + foreach ($keys as $k => $v) { |
| 57 | + if ($v === $key) { |
| 58 | + unset($keys[$k]); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + ++$count; |
| 63 | + } |
| 64 | + |
| 65 | + $this->assertSame(4, $count); |
| 66 | + } |
| 67 | + |
| 68 | + public function testIsHit() |
| 69 | + { |
| 70 | + $adapter = $this->createCachePool(); |
| 71 | + |
| 72 | + $item = $adapter->getItem('key'); |
| 73 | + $this->assertFalse($item->isHit()); |
| 74 | + } |
| 75 | + |
| 76 | + public function testClear() |
| 77 | + { |
| 78 | + $this->assertTrue($this->createCachePool()->clear()); |
| 79 | + } |
| 80 | + |
| 81 | + public function testDeleteItem() |
| 82 | + { |
| 83 | + $this->assertTrue($this->createCachePool()->deleteItem('key')); |
| 84 | + } |
| 85 | + |
| 86 | + public function testDeleteItems() |
| 87 | + { |
| 88 | + $this->assertTrue($this->createCachePool()->deleteItems(array('key', 'foo', 'bar'))); |
| 89 | + } |
| 90 | + |
| 91 | + public function testSave() |
| 92 | + { |
| 93 | + $adapter = $this->createCachePool(); |
| 94 | + |
| 95 | + $item = $adapter->getItem('key'); |
| 96 | + $this->assertFalse($item->isHit()); |
| 97 | + $this->assertNull($item->get(), "Item's value must be null when isHit is false."); |
| 98 | + |
| 99 | + $this->assertFalse($adapter->save($item)); |
| 100 | + } |
| 101 | + |
| 102 | + public function testDeferredSave() |
| 103 | + { |
| 104 | + $adapter = $this->createCachePool(); |
| 105 | + |
| 106 | + $item = $adapter->getItem('key'); |
| 107 | + $this->assertFalse($item->isHit()); |
| 108 | + $this->assertNull($item->get(), "Item's value must be null when isHit is false."); |
| 109 | + |
| 110 | + $this->assertFalse($adapter->saveDeferred($item)); |
| 111 | + } |
| 112 | + |
| 113 | + public function testCommit() |
| 114 | + { |
| 115 | + $adapter = $this->createCachePool(); |
| 116 | + |
| 117 | + $item = $adapter->getItem('key'); |
| 118 | + $this->assertFalse($item->isHit()); |
| 119 | + $this->assertNull($item->get(), "Item's value must be null when isHit is false."); |
| 120 | + |
| 121 | + $this->assertFalse($adapter->saveDeferred($item)); |
| 122 | + $this->assertFalse($this->createCachePool()->commit()); |
| 123 | + } |
| 124 | +} |
0 commit comments