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

Commit

Permalink
Do not check for existence of key prior to deletion
Browse files Browse the repository at this point in the history
We only need to test that the return value from `removeItem()` is
non-null. A `null` value indicates an error, while true/false indicates
the operation occurred; different adapters will return different values
depending on whether the key existed, so a boolean value always
indicates the key no longer exists in the storage.
  • Loading branch information
weierophinney committed Apr 23, 2018
1 parent 10eb0a3 commit 580cb67
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 9 deletions.
6 changes: 1 addition & 5 deletions src/Psr/SimpleCache/SimpleCacheDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,8 @@ public function delete($key)
{
$this->validateKey($key);

if (! $this->storage->hasItem($key)) {
return true;
}

try {
return (bool) $this->storage->removeItem($key);
return null !== $this->storage->removeItem($key);
} catch (Throwable $e) {
return false;
} catch (Exception $e) {
Expand Down
5 changes: 1 addition & 4 deletions test/Psr/SimpleCache/SimpleCacheDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,22 +346,19 @@ public function testSetShouldReRaiseExceptionThrownByStorage()

public function testDeleteShouldProxyToStorage()
{
$this->storage->hasItem('key')->willReturn(true);
$this->storage->removeItem('key')->willReturn(true);
$this->assertTrue($this->cache->delete('key'));
}

public function testDeleteShouldReturnTrueWhenItemDoesNotExist()
{
$this->storage->hasItem('key')->willReturn(false);
$this->storage->removeItem('key')->shouldNotBeCalled();
$this->storage->removeItem('key')->willReturn(false);
$this->assertTrue($this->cache->delete('key'));
}

public function testDeleteShouldReturnFalseWhenExceptionThrownByStorage()
{
$exception = new Exception\ExtensionNotLoadedException('failure', 500);
$this->storage->hasItem('key')->willReturn(true);
$this->storage->removeItem('key')->willThrow($exception);

$this->assertFalse($this->cache->delete('key'));
Expand Down

0 comments on commit 580cb67

Please sign in to comment.