diff --git a/src/HierarchicalCachePoolTest.php b/src/HierarchicalCachePoolTest.php index b166e3a..a643d3e 100644 --- a/src/HierarchicalCachePoolTest.php +++ b/src/HierarchicalCachePoolTest.php @@ -65,26 +65,6 @@ public function testBasicUsage() $this->assertFalse($this->cache->hasItem('|users|4711|followers|4|likes')); } - public function testBasicUsageWithTags() - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); - - return; - } - - $user = 4711; - for ($i = 0; $i < 10; $i++) { - $item = $this->cache->getItem(sprintf('|users|%d|followers|%d|likes', $user, $i), ['user']); - $item->set('Justin Bieber'); - $this->cache->save($item); - } - - $this->assertTrue($this->cache->hasItem('|users|4711|followers|4|likes', ['user'])); - $this->cache->deleteItem('|users|4711|followers', ['user']); - $this->assertFalse($this->cache->hasItem('|users|4711|followers|4|likes', ['user'])); - } - public function testChain() { if (isset($this->skippedTests[__FUNCTION__])) { @@ -129,31 +109,6 @@ public function testChain() $this->assertFalse($this->cache->hasItem('|aaa|bbb|zzz|ddd')); } - public function testTagRemoval() - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); - - return; - } - - $item = $this->cache->getItem('|aaa|bbb', ['tag1']); - $item->set('value'); - $this->cache->save($item); - - $this->cache->deleteItem('|aaa|bbb', ['tag2']); - $this->assertTrue($this->cache->hasItem('|aaa|bbb', ['tag1'])); - - $this->cache->deleteItem('|aaa|bbb'); - $this->assertTrue($this->cache->hasItem('|aaa|bbb', ['tag1'])); - - $this->cache->deleteItem('|aaa', ['tag2']); - $this->assertTrue($this->cache->hasItem('|aaa|bbb', ['tag1'])); - - $this->cache->deleteItem('|aaa'); - $this->assertTrue($this->cache->hasItem('|aaa|bbb', ['tag1'])); - } - public function testRemoval() { if (isset($this->skippedTests[__FUNCTION__])) { diff --git a/src/TaggableCachePoolTest.php b/src/TaggableCachePoolTest.php index bc57a3b..a7aaa0e 100644 --- a/src/TaggableCachePoolTest.php +++ b/src/TaggableCachePoolTest.php @@ -12,7 +12,6 @@ namespace Cache\IntegrationTests; use Cache\Taggable\TaggablePoolInterface; -use Psr\Cache\CacheItemPoolInterface; abstract class TaggableCachePoolTest extends \PHPUnit_Framework_TestCase { @@ -22,7 +21,7 @@ abstract class TaggableCachePoolTest extends \PHPUnit_Framework_TestCase protected $skippedTests = []; /** - * @type CacheItemPoolInterface|TaggablePoolInterface + * @type TaggablePoolInterface */ private $cache; @@ -38,52 +37,12 @@ protected function setUp() protected function tearDown() { - $this->cache->clear(); - } - - public function invalidKeys() - { - return CachePoolTest::invalidKeys(); - } - - public function testBasicUsage() - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); - - return; + if ($this->cache !== null) { + $this->cache->clear(); } - - $item = $this->cache->getItem('tobias', ['developer', 'speaker']); - $item->set('foobar'); - $this->cache->save($item); - - $item = $this->cache->getItem('aaron', ['developer', 'nice guy']); - $item->set('foobar'); - $this->cache->save($item); - - $item = $this->cache->getItem('the king of Sweden', ['nice guy', 'king']); - $item->set('foobar'); - $this->cache->save($item); - - $this->assertTrue($this->cache->getItem('tobias', ['developer', 'speaker'])->isHit()); - $this->assertTrue($this->cache->getItem('tobias', ['speaker', 'developer'])->isHit()); - $this->assertFalse($this->cache->getItem('tobias', ['developer'])->isHit()); - $this->assertFalse($this->cache->getItem('tobias', ['king'])->isHit()); - $this->assertFalse($this->cache->getItem('tobias')->isHit()); - - // Remove everything tagged with 'nice guy' - $this->cache->clear(['nice guy']); - $this->assertTrue($this->cache->getItem('tobias', ['developer', 'speaker'])->isHit()); - $this->assertFalse($this->cache->getItem('aaron', ['developer', 'nice guy'])->isHit()); - $this->assertFalse($this->cache->getItem('the king of Sweden', ['nice guy', 'king'])->isHit()); - - // To clear everything you do as you usually do - $this->cache->clear(); - $this->assertFalse($this->cache->getItem('tobias', ['developer', 'speaker'])->isHit()); } - public function testGetItem() + public function testBasicUsage() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); @@ -91,33 +50,22 @@ public function testGetItem() return; } - $item = $this->cache->getItem('tobias', ['developer', 'speaker']); + $item = $this->cache->getItem('key'); $item->set('value'); + $item->setTags(['tag1', 'tag2']); $this->cache->save($item); - $item = $this->cache->getItem('tobias', ['developer']); - $this->assertFalse($item->isHit(), 'There should be no item with key "tobias" and tag "developer"'); - } - - public function testGetItems() - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); - - return; - } + // The item should be saved + $this->assertTrue($this->cache->hasItem('key')); - $item = $this->cache->getItem('tobias', ['developer']); - $item->set('value'); - $this->cache->save($item); + // I want to clear all post by author + $this->cache->clearTags(['tag1']); - $items = $this->cache->getItems(['tobias', 'aaron'], ['developer']); - $this->assertCount(2, $items); - $this->assertTrue($items['tobias']->isHit()); - $this->assertFalse($items['aaron']->isHit()); + // The item should be removed + $this->assertFalse($this->cache->hasItem('key')); } - public function testHasItem() + public function testMultipleTags() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); @@ -125,15 +73,25 @@ public function testHasItem() return; } - $item = $this->cache->getItem('tobias', ['developer']); - $item->set('value'); - $this->cache->save($item); + $this->cache->save($this->cache->getItem('key1')->set('value')->setTags(['tag1', 'tag2'])); + $this->cache->save($this->cache->getItem('key2')->set('value')->setTags(['tag1', 'tag3'])); + $this->cache->save($this->cache->getItem('key3')->set('value')->setTags(['tag2', 'tag3'])); + $this->cache->save($this->cache->getItem('key4')->set('value')->setTags(['tag4', 'tag3'])); + + $this->cache->clearTags(['tag1']); + $this->assertFalse($this->cache->hasItem('key1')); + $this->assertFalse($this->cache->hasItem('key2')); + $this->assertTrue($this->cache->hasItem('key3')); + $this->assertTrue($this->cache->hasItem('key4')); - $this->assertTrue($this->cache->hasItem('tobias', ['developer'])); - $this->assertFalse($this->cache->hasItem('aaron', ['developer'])); + $this->cache->clearTags(['tag2']); + $this->assertFalse($this->cache->hasItem('key1')); + $this->assertFalse($this->cache->hasItem('key2')); + $this->assertFalse($this->cache->hasItem('key3')); + $this->assertTrue($this->cache->hasItem('key4')); } - public function testDeleteItem() + public function testTagAccessor() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); @@ -141,18 +99,24 @@ public function testDeleteItem() return; } - $item = $this->cache->getItem('tobias', ['developer', 'speaker']); - $item->set('foobar'); - $this->cache->save($item); + $item = $this->cache->getItem('key'); + $item->setTags(['tag1', 'tag2']); + $item->addTag('tag3'); - $this->cache->deleteItem('tobias', ['developer']); - $this->assertTrue($this->cache->getItem('tobias', ['developer', 'speaker'])->isHit()); + $tags = $item->getTags(); + $this->assertCount(3, $tags); + $this->assertTrue(in_array('tag1', $tags)); + $this->assertTrue(in_array('tag2', $tags)); + $this->assertTrue(in_array('tag3', $tags)); - $this->cache->deleteItem('tobias', ['developer', 'speaker']); - $this->assertFalse($this->cache->getItem('tobias', ['developer', 'speaker'])->isHit()); + $item->setTags(['tag4']); + $tags = $item->getTags(); + $this->assertCount(1, $tags); + $this->assertFalse(in_array('tag1', $tags)); + $this->assertTrue(in_array('tag4', $tags)); } - public function testNoKeyModificationWhenNoTags() + public function testRemoveTagWhenItemIsRemoved() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); @@ -161,61 +125,22 @@ public function testNoKeyModificationWhenNoTags() } $item = $this->cache->getItem('key'); - $item->set('foobar'); + $item->setTags(['tag1']); + + // Save the item and then delete it $this->cache->save($item); + $this->cache->deleteItem('key'); + // Create a new item (no tags) $item = $this->cache->getItem('key'); - $this->assertEquals('key', $item->getKey(), 'Key can not change when using no tags'); - } - - public function testKeysShouldAppearUnchanged() - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); - - return; - } - - $item = $this->cache->getItem('key', ['tag']); - $item->set('foobar'); $this->cache->save($item); - $item = $this->cache->getItem('key', ['tag']); - $this->assertEquals('key', $item->getKey(), 'Key should appear intact even when using tags'); - } - - public function testSaveDeferred() - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); - - return; - } - - $item = $this->cache->getItem('key', ['tag']); - $item->set('foobar'); - $this->cache->saveDeferred($item); - - $item = $this->cache->getItem('key2', ['tag2']); - $item->set('foobar'); - $this->cache->saveDeferred($item); - - // Both should be hit - $this->assertTrue($this->cache->getItem('key', ['tag'])->isHit()); - $this->assertTrue($this->cache->getItem('key2', ['tag2'])->isHit()); - - // Clear tag2 and make sure we do not remove everything - $this->cache->clear(['tag2']); - $this->assertTrue($this->cache->getItem('key', ['tag'])->isHit()); - $this->assertFalse($this->cache->getItem('key2', ['tag2'])->isHit()); - - // Clear everything and make sure everything is removed - $this->cache->clear(); - $this->assertFalse($this->cache->getItem('key', ['tag'])->isHit()); - $this->assertFalse($this->cache->getItem('key2', ['tag2'])->isHit()); + // Clear the tag + $this->cache->clearTags(['tag1']); + $this->assertTrue($this->cache->hasItem('key'), 'Item key should be removed from the tag list when the item is removed'); } - public function testKeysWithDeferred() + public function testClear() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); @@ -223,82 +148,21 @@ public function testKeysWithDeferred() return; } - $item = $this->cache->getItem('key', ['tag']); - $item->set('foobar'); - $this->cache->saveDeferred($item); - - $this->assertTrue($this->cache->getItem('key', ['tag'])->isHit()); - $this->assertFalse($this->cache->getItem('key', ['tag2'])->isHit()); - $this->assertFalse($this->cache->getItem('key')->isHit()); + $item = $this->cache->getItem('key'); + $item->setTags(['tag1']); + $this->cache->save($item); $this->cache->clear(); - } - - /** - * @expectedException \Psr\Cache\InvalidArgumentException - * @dataProvider invalidKeys - */ - public function testClearInvalidKeys($key) - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); - - return; - } - - $this->cache->clear([$key]); - } - - /** - * @expectedException \Psr\Cache\InvalidArgumentException - * @dataProvider invalidKeys - */ - public function testGetItemInvalidKeys($key) - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); - - return; - } - - $this->cache->getItem('key', [$key]); - } - - /** - * @expectedException \Psr\Cache\InvalidArgumentException - * @dataProvider invalidKeys - */ - public function testGetItemsInvalidKeys($key) - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); - - return; - } - - $this->cache->getItems(['key'], [$key]); - } - - /** - * @expectedException \Psr\Cache\InvalidArgumentException - * @dataProvider invalidKeys - */ - public function testHasItemInvalidKeys($key) - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); - return; - } + // Create a new item (no tags) + $item = $this->cache->getItem('key'); + $this->cache->save($item); + $this->cache->clearTags(['tag1']); - $this->cache->hasItem($key); + $this->assertTrue($this->cache->hasItem('key'), 'Tags should be removed on clear()'); } - /** - * @expectedException \Psr\Cache\InvalidArgumentException - * @dataProvider invalidKeys - */ - public function testDeleteItemInvalidKeys($key) + public function testClearTag() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); @@ -306,21 +170,17 @@ public function testDeleteItemInvalidKeys($key) return; } - $this->cache->deleteItem('key', [$key]); - } + $item = $this->cache->getItem('key'); + $item->setTags(['tag1']); + $this->cache->save($item); - /** - * @expectedException \Psr\Cache\InvalidArgumentException - * @dataProvider invalidKeys - */ - public function testDeleteItemsInvalidKeys($key) - { - if (isset($this->skippedTests[__FUNCTION__])) { - $this->markTestSkipped($this->skippedTests[__FUNCTION__]); + $this->cache->clearTags(['tag1']); - return; - } + // Create a new item (no tags) + $item = $this->cache->getItem('key'); + $this->cache->save($item); + $this->cache->clearTags(['tag1']); - $this->cache->deleteItems(['key'], [$key]); + $this->assertTrue($this->cache->hasItem('key'), 'Item key list should be removed when clearing the tags'); } }