Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/CachePoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ abstract class CachePoolTest extends TestCase
*/
abstract public function createCachePool(): CacheItemPoolInterface;

/**
* A second pool backed by the same storage as createCachePool(), under a different namespace.
*
* Return null when the implementation cannot provide one, the test is then skipped.
*/
public function createCachePoolWithOtherNamespace(): ?CacheItemPoolInterface
{
return null;
}

#[Before]
public function setupService()
{
Expand Down Expand Up @@ -318,6 +328,32 @@ public function testClearWithDeferredItems()
$this->assertFalse($this->cache->getItem('key')->isHit(), 'Deferred items must be cleared on clear(). ');
}

public function testClearDoesNotAffectOtherNamespaces()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$other = $this->createCachePoolWithOtherNamespace();

if (null === $other) {
$this->markTestSkipped('This implementation cannot provide a second pool on the same storage with another namespace.');
}

$this->cache->save($this->cache->getItem('key')->set('value'));
$other->save($other->getItem('key')->set('other value'));

$this->assertTrue($this->cache->clear(), 'clear() must return true if cache was cleared. ');
$this->assertFalse($this->cache->hasItem('key'), 'The cleared pool should be empty after it is cleared.');

$otherItem = $other->getItem('key');
$this->assertTrue($otherItem->isHit(), 'Clearing a pool must not clear a pool using another namespace on the same storage.');
$this->assertSame('other value', $otherItem->get(), 'The item stored in the other namespace must keep its own value.');

// The tearDownService() hook only clears $this->cache, so the other pool is cleared here.
$other->clear();
}

public function testDeleteItem()
{
if (isset($this->skippedTests[__FUNCTION__])) {
Expand Down
10 changes: 7 additions & 3 deletions src/TaggableCachePoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function testRemoveTagWhenItemIsRemoved()

// Save the item and then delete it
$this->cache->save($item);
$this->cache->deleteItem('key');
$this->assertTrue($this->cache->deleteItem('key'), 'deleteItem() must return true when a tagged item is deleted');

// Create a new item (same key) (no tags)
$item = $this->cache->getItem('key')->set('value');
Expand Down Expand Up @@ -287,15 +287,19 @@ public function testInvalidateTags()
}

$item = $this->cache->getItem('key')->set('value');
$item->setTags(['tag1', 'tag2']);
$item->setTags(['tag1']);
$this->cache->save($item);
$item = $this->cache->getItem('key2')->set('value');
$item->setTags(['tag1']);
$item->setTags(['tag2']);
$this->cache->save($item);
$item = $this->cache->getItem('key3')->set('value');
$item->setTags(['tag3']);
$this->cache->save($item);

$this->cache->invalidateTags(['tag1', 'tag2']);
$this->assertFalse($this->cache->hasItem('key'), 'Item should be cleared when tag is invalidated');
$this->assertFalse($this->cache->hasItem('key2'), 'Item should be cleared when tag is invalidated');
$this->assertTrue($this->cache->hasItem('key3'), 'An item tagged with none of the invalidated tags must survive');

// Create a new item (no tags)
$item = $this->cache->getItem('key')->set('value');
Expand Down
9 changes: 9 additions & 0 deletions tests/CachePoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ final class CachePoolTest extends CachePoolContract
{
private ?string $namespace = null;

private ?string $otherNamespace = null;

public function createCachePool(): CacheItemPoolInterface
{
$this->namespace ??= bin2hex(random_bytes(16));

return new ValidatingCachePool(new FilesystemAdapter($this->namespace));
}

public function createCachePoolWithOtherNamespace(): CacheItemPoolInterface
{
$this->otherNamespace ??= bin2hex(random_bytes(16));

return new ValidatingCachePool(new FilesystemAdapter($this->otherNamespace));
}

public function testDataProvidersExposeCases()
{
self::assertNotEmpty(self::invalidKeys());
Expand Down