Skip to content

Commit 26db00b

Browse files
authored
Added tests for tags (#64)
* Added tests for tags * code style * Name * Make sure an empty array is returned * Updates to reflect new Interface
1 parent 9b5bea6 commit 26db00b

File tree

1 file changed

+34
-60
lines changed

1 file changed

+34
-60
lines changed

src/TaggableCachePoolTest.php

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,9 @@ protected function tearDown()
4545
}
4646
}
4747

48-
public function testBasicUsage()
48+
public function invalidKeys()
4949
{
50-
if (isset($this->skippedTests[__FUNCTION__])) {
51-
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
52-
53-
return;
54-
}
55-
56-
$item = $this->cache->getItem('key');
57-
$item->set('value');
58-
$item->setTags(['tag1', 'tag2']);
59-
$this->cache->save($item);
60-
61-
// The item should be saved
62-
$this->assertTrue($this->cache->hasItem('key'));
63-
64-
// I want to clear all post by author
65-
$this->cache->invalidateTags(['tag1']);
66-
67-
// The item should be removed
68-
$this->assertFalse($this->cache->hasItem('key'), 'Tags does not seams to be saved');
50+
return CachePoolTest::invalidKeys();
6951
}
7052

7153
public function testMultipleTags()
@@ -94,7 +76,7 @@ public function testMultipleTags()
9476
$this->assertTrue($this->cache->hasItem('key4'));
9577
}
9678

97-
public function testTagAccessor()
79+
public function testPreviousTag()
9880
{
9981
if (isset($this->skippedTests[__FUNCTION__])) {
10082
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
@@ -103,38 +85,18 @@ public function testTagAccessor()
10385
}
10486

10587
$item = $this->cache->getItem('key')->set('value');
106-
$this->assertCount(0, $item->getTags());
107-
108-
$item->addTag('tag0');
109-
$this->assertCount(1, $item->getTags());
110-
111-
$item->setTags(['tag1', 'tag2']);
112-
$this->assertCount(2, $item->getTags());
113-
$tags = $item->getTags();
114-
$this->assertTrue(in_array('tag1', $tags));
115-
$this->assertTrue(in_array('tag2', $tags));
116-
117-
$item->addTags(['tag3', 'tag4']);
118-
$this->assertCount(4, $item->getTags());
119-
$tags = $item->getTags();
120-
$this->assertTrue(in_array('tag4', $tags));
121-
$this->assertTrue(in_array('tag3', $tags));
122-
}
123-
124-
/**
125-
* @expectedException \Psr\Cache\InvalidArgumentException
126-
*/
127-
public function testTagAccessorWithNoString()
128-
{
129-
if (isset($this->skippedTests[__FUNCTION__])) {
130-
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
88+
$tags = $item->getPreviousTags();
89+
$this->assertTrue(is_array($tags));
90+
$this->assertCount(0, $tags);
13191

132-
return;
133-
}
92+
$item->setTags(['tag0']);
93+
$this->assertCount(0, $item->getPreviousTags());
13494

135-
$item = $this->cache->getItem('key')->set('value');
136-
$item->addTag(new \stdClass());
13795
$this->cache->save($item);
96+
$this->assertCount(0, $item->getPreviousTags());
97+
98+
$item = $this->cache->getItem('key');
99+
$this->assertCount(1, $item->getPreviousTags());
138100
}
139101

140102
/**
@@ -149,14 +111,15 @@ public function testTagAccessorWithEmptyTag()
149111
}
150112

151113
$item = $this->cache->getItem('key')->set('value');
152-
$item->addTag('');
114+
$item->setTags(['']);
153115
$this->cache->save($item);
154116
}
155117

156118
/**
157119
* @expectedException \Psr\Cache\InvalidArgumentException
120+
* @dataProvider invalidKeys
158121
*/
159-
public function testTagAccessorWithInvalidTag()
122+
public function testTagAccessorWithInvalidTag($tag)
160123
{
161124
if (isset($this->skippedTests[__FUNCTION__])) {
162125
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
@@ -165,7 +128,7 @@ public function testTagAccessorWithInvalidTag()
165128
}
166129

167130
$item = $this->cache->getItem('key')->set('value');
168-
$item->addTag('@foo@');
131+
$item->setTags([$tag]);
169132
$this->cache->save($item);
170133
}
171134

@@ -178,14 +141,11 @@ public function testTagAccessorDuplicateTags()
178141
}
179142

180143
$item = $this->cache->getItem('key')->set('value');
181-
$item->addTag('tag');
182-
$this->cache->save($item);
183-
$item->addTag('tag');
184-
$this->cache->save($item);
185-
$item->addTag('tag');
144+
$item->setTags(['tag', 'tag', 'tag']);
186145
$this->cache->save($item);
146+
$item = $this->cache->getItem('key');
187147

188-
$this->assertCount(1, $item->getTags());
148+
$this->assertCount(1, $item->getPreviousTags());
189149
}
190150

191151
/**
@@ -292,6 +252,20 @@ public function testInvalidateTags()
292252
$this->cache->save($item);
293253
$this->cache->invalidateTags(['tag1']);
294254

295-
$this->assertTrue($this->cache->hasItem('key'), 'Item key list should be removed when clearing the tags');
255+
$this->assertTrue($this->cache->hasItem('key'), 'Item k list should be removed when clearing the tags');
256+
}
257+
258+
/**
259+
* When an item is overwritten we need to clear tags for original item.
260+
*/
261+
public function testTagsAreCleanedOnSave()
262+
{
263+
$pool = $this->cache;
264+
$i = $pool->getItem('key')->set('value');
265+
$pool->save($i->setTags(['foo']));
266+
$i = $pool->getItem('key');
267+
$pool->save($i->setTags(['bar']));
268+
$pool->invalidateTags(['foo']);
269+
$this->assertTrue($pool->getItem('key')->isHit());
296270
}
297271
}

0 commit comments

Comments
 (0)