Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Fixed #157. PSR-16: clear by namespace when possible #159

Merged
merged 1 commit into from
Apr 23, 2018
Merged
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
14 changes: 11 additions & 3 deletions src/Psr/SimpleCache/SimpleCacheDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Traversable;
use Zend\Cache\Exception\InvalidArgumentException as ZendCacheInvalidArgumentException;
use Zend\Cache\Psr\SerializationTrait;
use Zend\Cache\Storage\ClearByNamespaceInterface;
use Zend\Cache\Storage\FlushableInterface;
use Zend\Cache\Storage\StorageInterface;

Expand Down Expand Up @@ -150,10 +151,17 @@ public function delete($key)
*/
public function clear()
{
if (! $this->storage instanceof FlushableInterface) {
return false;
$namespace = $this->storage->getOptions()->getNamespace();

if ($this->storage instanceof ClearByNamespaceInterface && $namespace) {
return $this->storage->clearByNamespace($namespace);
}
return $this->storage->flush();

if ($this->storage instanceof FlushableInterface) {
return $this->storage->flush();
}

return false;
}

/**
Expand Down
44 changes: 41 additions & 3 deletions test/Psr/SimpleCache/SimpleCacheDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Zend\Cache\Psr\SimpleCache\SimpleCacheException;
use Zend\Cache\Storage\Adapter\AdapterOptions;
use Zend\Cache\Storage\Capabilities;
use Zend\Cache\Storage\ClearByNamespaceInterface;
use Zend\Cache\Storage\FlushableInterface;
use Zend\Cache\Storage\StorageInterface;

Expand Down Expand Up @@ -409,15 +410,52 @@ public function testDeleteShouldReRaiseExceptionThrownByStorage()

public function testClearReturnsFalseIfStorageIsNotFlushable()
{
$this->assertFalse($this->cache->clear());
$this->options->getNamespace()->willReturn(null);
$storage = $this->prophesize(StorageInterface::class);
$storage->getOptions()->will([$this->options, 'reveal']);
$this->mockCapabilities($storage);

$cache = new SimpleCacheDecorator($storage->reveal());
$this->assertFalse($cache->clear());
}

public function testClearProxiesToStorageIfStorageCanBeClearedByNamespace()
{
$this->options->getNamespace()->willReturn('foo');
$storage = $this->prophesize(StorageInterface::class);
$storage->willImplement(FlushableInterface::class);
$storage->willImplement(ClearByNamespaceInterface::class);
$this->mockCapabilities($storage);
$storage->getOptions()->will([$this->options, 'reveal']);
$storage->clearByNamespace('foo')->shouldBeCalled()->willReturn(true);
$storage->flush()->shouldNotBeCalled();

$cache = new SimpleCacheDecorator($storage->reveal());
$this->assertTrue($cache->clear());
}

public function testClearProxiesToStorageFlushIfStorageCanBeClearedByNamespaceWithNoNamespace()
{
$this->options->getNamespace()->willReturn(null);
$storage = $this->prophesize(StorageInterface::class);
$storage->willImplement(FlushableInterface::class);
$storage->willImplement(ClearByNamespaceInterface::class);
$this->mockCapabilities($storage);
$storage->getOptions()->will([$this->options, 'reveal']);
$storage->clearByNamespace(Argument::any())->shouldNotBeCalled();
$storage->flush()->shouldBeCalled()->willReturn(true);

$cache = new SimpleCacheDecorator($storage->reveal());
$this->assertTrue($cache->clear());
}

public function testClearProxiesToStorageIfStorageIsFlushable()
public function testClearProxiesToStorageFlushIfStorageIsFlushable()
{
$storage = $this->prophesize(StorageInterface::class);
$storage->willImplement(FlushableInterface::class);
$this->mockCapabilities($storage);
$storage->flush()->willReturn(true);
$storage->getOptions()->will([$this->options, 'reveal']);
$storage->flush()->shouldBeCalled()->willReturn(true);

$cache = new SimpleCacheDecorator($storage->reveal());
$this->assertTrue($cache->clear());
Expand Down