1111
1212namespace Cache \IntegrationTests ;
1313
14- use Cache \Taggable \ TaggablePoolInterface ;
14+ use Cache \Adapter \ Common \ TaggableCacheItemPoolInterface ;
1515
16+ /**
17+ * @author Tobias Nyholm <[email protected] > 18+ */
1619abstract class TaggableCachePoolTest extends \PHPUnit_Framework_TestCase
1720{
1821 /**
@@ -21,12 +24,12 @@ abstract class TaggableCachePoolTest extends \PHPUnit_Framework_TestCase
2124 protected $ skippedTests = [];
2225
2326 /**
24- * @type TaggablePoolInterface
27+ * @type TaggableCacheItemPoolInterface
2528 */
2629 private $ cache ;
2730
2831 /**
29- * @return TaggablePoolInterface that is used in the tests
32+ * @return TaggableCacheItemPoolInterface that is used in the tests
3033 */
3134 abstract public function createCachePool ();
3235
@@ -59,7 +62,7 @@ public function testBasicUsage()
5962 $ this ->assertTrue ($ this ->cache ->hasItem ('key ' ));
6063
6164 // I want to clear all post by author
62- $ this ->cache ->clearTags (['tag1 ' ]);
65+ $ this ->cache ->invalidateTags (['tag1 ' ]);
6366
6467 // The item should be removed
6568 $ this ->assertFalse ($ this ->cache ->hasItem ('key ' ), 'Tags does not seams to be saved ' );
@@ -78,13 +81,13 @@ public function testMultipleTags()
7881 $ this ->cache ->save ($ this ->cache ->getItem ('key3 ' )->set ('value ' )->setTags (['tag2 ' , 'tag3 ' ]));
7982 $ this ->cache ->save ($ this ->cache ->getItem ('key4 ' )->set ('value ' )->setTags (['tag4 ' , 'tag3 ' ]));
8083
81- $ this ->cache ->clearTags (['tag1 ' ]);
84+ $ this ->cache ->invalidateTags (['tag1 ' ]);
8285 $ this ->assertFalse ($ this ->cache ->hasItem ('key1 ' ));
8386 $ this ->assertFalse ($ this ->cache ->hasItem ('key2 ' ));
8487 $ this ->assertTrue ($ this ->cache ->hasItem ('key3 ' ));
8588 $ this ->assertTrue ($ this ->cache ->hasItem ('key4 ' ));
8689
87- $ this ->cache ->clearTags (['tag2 ' ]);
90+ $ this ->cache ->invalidateTags (['tag2 ' ]);
8891 $ this ->assertFalse ($ this ->cache ->hasItem ('key1 ' ));
8992 $ this ->assertFalse ($ this ->cache ->hasItem ('key2 ' ));
9093 $ this ->assertFalse ($ this ->cache ->hasItem ('key3 ' ));
@@ -99,23 +102,96 @@ public function testTagAccessor()
99102 return ;
100103 }
101104
102- $ item = $ this ->cache ->getItem ('key ' );
103- $ item ->setTags (['tag1 ' , 'tag2 ' ]);
104- $ item ->addTag ('tag3 ' );
105+ $ item = $ this ->cache ->getItem ('key ' )->set ('value ' );
106+ $ this ->assertCount (0 , $ item ->getTags ());
107+
108+ $ item ->addTag ('tag0 ' );
109+ $ this ->assertCount (1 , $ item ->getTags ());
105110
111+ $ item ->setTags (['tag1 ' , 'tag2 ' ]);
112+ $ this ->assertCount (2 , $ item ->getTags ());
106113 $ tags = $ item ->getTags ();
107- $ this ->assertCount (3 , $ tags );
108114 $ this ->assertTrue (in_array ('tag1 ' , $ tags ));
109115 $ this ->assertTrue (in_array ('tag2 ' , $ tags ));
110- $ this ->assertTrue (in_array ('tag3 ' , $ tags ));
111116
112- $ item ->setTags (['tag4 ' ]);
117+ $ item ->addTags (['tag3 ' , 'tag4 ' ]);
118+ $ this ->assertCount (4 , $ item ->getTags ());
113119 $ tags = $ item ->getTags ();
114- $ this ->assertCount (1 , $ tags );
115- $ this ->assertFalse (in_array ('tag1 ' , $ tags ));
116120 $ 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__ ]);
131+
132+ return ;
133+ }
134+
135+ $ item = $ this ->cache ->getItem ('key ' )->set ('value ' );
136+ $ item ->addTag (new \stdClass ());
137+ $ this ->cache ->save ($ item );
138+ }
139+
140+ /**
141+ * @expectedException \Psr\Cache\InvalidArgumentException
142+ */
143+ public function testTagAccessorWithEmptyTag ()
144+ {
145+ if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
146+ $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
147+
148+ return ;
149+ }
150+
151+ $ item = $ this ->cache ->getItem ('key ' )->set ('value ' );
152+ $ item ->addTag ('' );
153+ $ this ->cache ->save ($ item );
117154 }
118155
156+ /**
157+ * @expectedException \Psr\Cache\InvalidArgumentException
158+ */
159+ public function testTagAccessorWithInvalidTag ()
160+ {
161+ if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
162+ $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
163+
164+ return ;
165+ }
166+
167+ $ item = $ this ->cache ->getItem ('key ' )->set ('value ' );
168+ $ item ->addTag ('@foo@ ' );
169+ $ this ->cache ->save ($ item );
170+ }
171+
172+ public function testTagAccessorDuplicateTags ()
173+ {
174+ if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
175+ $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
176+
177+ return ;
178+ }
179+
180+ $ 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 ' );
186+ $ this ->cache ->save ($ item );
187+
188+ $ this ->assertCount (1 , $ item ->getTags ());
189+ }
190+
191+ /**
192+ * The tag must be removed whenever we remove an item. If not, when creating a new item
193+ * with the same key will get the same tags.
194+ */
119195 public function testRemoveTagWhenItemIsRemoved ()
120196 {
121197 if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
@@ -131,16 +207,16 @@ public function testRemoveTagWhenItemIsRemoved()
131207 $ this ->cache ->save ($ item );
132208 $ this ->cache ->deleteItem ('key ' );
133209
134- // Create a new item (no tags)
210+ // Create a new item (same key) ( no tags)
135211 $ item = $ this ->cache ->getItem ('key ' )->set ('value ' );
136212 $ this ->cache ->save ($ item );
137213
138- // Clear the tag
139- $ this ->cache ->clearTags (['tag1 ' ]);
214+ // Clear the tag, The new item should not be cleared
215+ $ this ->cache ->invalidateTags (['tag1 ' ]);
140216 $ this ->assertTrue ($ this ->cache ->hasItem ('key ' ), 'Item key should be removed from the tag list when the item is removed ' );
141217 }
142218
143- public function testClear ()
219+ public function testClearPool ()
144220 {
145221 if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
146222 $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
@@ -152,17 +228,18 @@ public function testClear()
152228 $ item ->setTags (['tag1 ' ]);
153229 $ this ->cache ->save ($ item );
154230
231+ // Clear the pool
155232 $ this ->cache ->clear ();
156233
157234 // Create a new item (no tags)
158235 $ item = $ this ->cache ->getItem ('key ' )->set ('value ' );
159236 $ this ->cache ->save ($ item );
160- $ this ->cache ->clearTags (['tag1 ' ]);
237+ $ this ->cache ->invalidateTags (['tag1 ' ]);
161238
162- $ this ->assertTrue ($ this ->cache ->hasItem ('key ' ), 'Tags should be removed on clear() ' );
239+ $ this ->assertTrue ($ this ->cache ->hasItem ('key ' ), 'Tags should be removed when the pool was cleared. ' );
163240 }
164241
165- public function testClearTag ()
242+ public function testInvalidateTag ()
166243 {
167244 if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
168245 $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
@@ -171,15 +248,47 @@ public function testClearTag()
171248 }
172249
173250 $ item = $ this ->cache ->getItem ('key ' )->set ('value ' );
251+ $ item ->setTags (['tag1 ' , 'tag2 ' ]);
252+ $ this ->cache ->save ($ item );
253+ $ item = $ this ->cache ->getItem ('key2 ' )->set ('value ' );
254+ $ item ->setTags (['tag1 ' ]);
255+ $ this ->cache ->save ($ item );
256+
257+ $ this ->cache ->invalidateTag ('tag2 ' );
258+ $ this ->assertFalse ($ this ->cache ->hasItem ('key ' ), 'Item should be cleared when tag is invalidated ' );
259+ $ this ->assertTrue ($ this ->cache ->hasItem ('key2 ' ), 'Item should be cleared when tag is invalidated ' );
260+
261+ // Create a new item (no tags)
262+ $ item = $ this ->cache ->getItem ('key ' )->set ('value ' );
263+ $ this ->cache ->save ($ item );
264+ $ this ->cache ->invalidateTags (['tag1 ' ]);
265+
266+ $ this ->assertTrue ($ this ->cache ->hasItem ('key ' ), 'Item key list should be removed when clearing the tags ' );
267+ }
268+
269+ public function testInvalidateTags ()
270+ {
271+ if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
272+ $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
273+
274+ return ;
275+ }
276+
277+ $ item = $ this ->cache ->getItem ('key ' )->set ('value ' );
278+ $ item ->setTags (['tag1 ' , 'tag2 ' ]);
279+ $ this ->cache ->save ($ item );
280+ $ item = $ this ->cache ->getItem ('key2 ' )->set ('value ' );
174281 $ item ->setTags (['tag1 ' ]);
175282 $ this ->cache ->save ($ item );
176283
177- $ this ->cache ->clearTags (['tag1 ' ]);
284+ $ this ->cache ->invalidateTags (['tag1 ' , 'tag2 ' ]);
285+ $ this ->assertFalse ($ this ->cache ->hasItem ('key ' ), 'Item should be cleared when tag is invalidated ' );
286+ $ this ->assertFalse ($ this ->cache ->hasItem ('key2 ' ), 'Item should be cleared when tag is invalidated ' );
178287
179288 // Create a new item (no tags)
180289 $ item = $ this ->cache ->getItem ('key ' )->set ('value ' );
181290 $ this ->cache ->save ($ item );
182- $ this ->cache ->clearTags (['tag1 ' ]);
291+ $ this ->cache ->invalidateTags (['tag1 ' ]);
183292
184293 $ this ->assertTrue ($ this ->cache ->hasItem ('key ' ), 'Item key list should be removed when clearing the tags ' );
185294 }
0 commit comments