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

Commit

Permalink
Merge branch 'hotfix/147' into develop
Browse files Browse the repository at this point in the history
Forward port #147
  • Loading branch information
weierophinney committed Apr 17, 2018
2 parents 002c32d + 2199d50 commit 50a756e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- [#147](https://github.com/zendframework/zend-cache/pull/147) fixes the Redis extension by ensuring it casts the results of `exists()` to a
boolean when testing if the storage contains an item.

- [#146](https://github.com/zendframework/zend-cache/pull/146) fixes several methods to change `@return` annotations to `@throws` where applicable.

- [#134](https://github.com/zendframework/zend-cache/pull/134) adds a missing import statement for `Traversable` within the `AdapterOptions` class.
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/Adapter/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected function internalHasItem(& $normalizedKey)
{
$redis = $this->getRedisResource();
try {
return $redis->exists($this->namespacePrefix . $normalizedKey);
return (bool) $redis->exists($this->namespacePrefix . $normalizedKey);
} catch (RedisResourceException $e) {
throw new Exception\RuntimeException($redis->getLastError(), $e->getCode(), $e);
}
Expand Down
48 changes: 48 additions & 0 deletions test/Storage/Adapter/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

namespace ZendTest\Cache\Storage\Adapter;

use PHPUnit_Framework_MockObject_MockObject;
use Zend\Cache;
use Redis as RedisResource;
use Zend\Cache\Storage\Adapter\Redis;
use Zend\Cache\Storage\Adapter\RedisOptions;
use Zend\Cache\Storage\Adapter\RedisResourceManager;

/**
* @covers Zend\Cache\Storage\Adapter\Redis<extended>
Expand Down Expand Up @@ -337,4 +341,48 @@ public function testTouchItem()
$this->assertTrue($this->_storage->touchItem($key));
$this->assertEquals($ttl, ceil($this->_storage->getMetadata($key)['ttl']));
}

public function testHasItemReturnsFalseIfRedisExistsReturnsZero()
{
$redis = $this->mockInitializedRedisResource();
$redis->method('exists')->willReturn(0);
$adapter = $this->createAdapterFromResource($redis);

$hasItem = $adapter->hasItem('does-not-exist');

$this->assertFalse($hasItem);
}

public function testHasItemReturnsTrueIfRedisExistsReturnsNonZeroInt()
{
$redis = $this->mockInitializedRedisResource();
$redis->method('exists')->willReturn(23);
$adapter = $this->createAdapterFromResource($redis);

$hasItem = $adapter->hasItem('does-not-exist');

$this->assertTrue($hasItem);
}

/**
* @return Redis
*/
private function createAdapterFromResource(RedisResource $redis)
{
$resourceManager = new RedisResourceManager();
$resourceId = 'my-resource';
$resourceManager->setResource($resourceId, $redis);
$options = new RedisOptions(['resource_manager' => $resourceManager, 'resource_id' => $resourceId]);
return new Redis($options);
}

/**
* @return PHPUnit_Framework_MockObject_MockObject|RedisResource
*/
private function mockInitializedRedisResource()
{
$redis = $this->getMock(RedisResource::class);
$redis->socket = true;
return $redis;
}
}

0 comments on commit 50a756e

Please sign in to comment.