Thanks to visit codestin.com
Credit goes to github.com

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

Commit 90461f9

Browse files
committed
Merge branch 'hotfix/147'
Close #147
2 parents d7c08eb + 2199d50 commit 90461f9

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25+
- [#147](https://github.com/zendframework/zend-cache/pull/147) fixes the Redis extension by ensuring it casts the results of `exists()` to a
26+
boolean when testing if the storage contains an item.
27+
2528
- [#146](https://github.com/zendframework/zend-cache/pull/146) fixes several methods to change `@return` annotations to `@throws` where applicable.
2629

2730
- [#134](https://github.com/zendframework/zend-cache/pull/134) adds a missing import statement for `Traversable` within the `AdapterOptions` class.

src/Storage/Adapter/Redis.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ protected function internalHasItem(& $normalizedKey)
206206
{
207207
$redis = $this->getRedisResource();
208208
try {
209-
return $redis->exists($this->namespacePrefix . $normalizedKey);
209+
return (bool) $redis->exists($this->namespacePrefix . $normalizedKey);
210210
} catch (RedisResourceException $e) {
211211
throw new Exception\RuntimeException($redis->getLastError(), $e->getCode(), $e);
212212
}

test/Storage/Adapter/RedisTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99

1010
namespace ZendTest\Cache\Storage\Adapter;
1111

12+
use PHPUnit_Framework_MockObject_MockObject;
1213
use Zend\Cache;
1314
use Redis as RedisResource;
15+
use Zend\Cache\Storage\Adapter\Redis;
16+
use Zend\Cache\Storage\Adapter\RedisOptions;
17+
use Zend\Cache\Storage\Adapter\RedisResourceManager;
1418

1519
/**
1620
* @covers Zend\Cache\Storage\Adapter\Redis<extended>
@@ -337,4 +341,48 @@ public function testTouchItem()
337341
$this->assertTrue($this->_storage->touchItem($key));
338342
$this->assertEquals($ttl, ceil($this->_storage->getMetadata($key)['ttl']));
339343
}
344+
345+
public function testHasItemReturnsFalseIfRedisExistsReturnsZero()
346+
{
347+
$redis = $this->mockInitializedRedisResource();
348+
$redis->method('exists')->willReturn(0);
349+
$adapter = $this->createAdapterFromResource($redis);
350+
351+
$hasItem = $adapter->hasItem('does-not-exist');
352+
353+
$this->assertFalse($hasItem);
354+
}
355+
356+
public function testHasItemReturnsTrueIfRedisExistsReturnsNonZeroInt()
357+
{
358+
$redis = $this->mockInitializedRedisResource();
359+
$redis->method('exists')->willReturn(23);
360+
$adapter = $this->createAdapterFromResource($redis);
361+
362+
$hasItem = $adapter->hasItem('does-not-exist');
363+
364+
$this->assertTrue($hasItem);
365+
}
366+
367+
/**
368+
* @return Redis
369+
*/
370+
private function createAdapterFromResource(RedisResource $redis)
371+
{
372+
$resourceManager = new RedisResourceManager();
373+
$resourceId = 'my-resource';
374+
$resourceManager->setResource($resourceId, $redis);
375+
$options = new RedisOptions(['resource_manager' => $resourceManager, 'resource_id' => $resourceId]);
376+
return new Redis($options);
377+
}
378+
379+
/**
380+
* @return PHPUnit_Framework_MockObject_MockObject|RedisResource
381+
*/
382+
private function mockInitializedRedisResource()
383+
{
384+
$redis = $this->getMock(RedisResource::class);
385+
$redis->socket = true;
386+
return $redis;
387+
}
340388
}

0 commit comments

Comments
 (0)