Skip to content

Commit 978c111

Browse files
committed
Merge pull request #1 from WyriHaximus/fix-check-if-has-key
get needs a check if the key exists
2 parents 0ab7e02 + 5b1ac31 commit 978c111

File tree

4 files changed

+140
-11
lines changed

4 files changed

+140
-11
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"phpunit/phpunit": "^5.2.3",
1717
"squizlabs/php_codesniffer": "^2.6",
1818
"vectorface/dunit": "~2.0",
19-
"phake/phake": "^2.3"
19+
"phake/phake": "^2.3",
20+
"clue/block-react": "^1.1"
2021
},
2122
"autoload": {
2223
"psr-4": {

composer.lock

Lines changed: 103 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Redis.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Clue\React\Redis\Client;
66
use React\Cache\CacheInterface;
77
use React\Promise\PromiseInterface;
8+
use function React\Promise\reject;
89

910
class Redis implements CacheInterface
1011
{
@@ -35,7 +36,12 @@ public function __construct(Client $client, $prefix = 'reach:cache:')
3536
*/
3637
public function get($key)
3738
{
38-
return $this->client->get($this->prefix . $key);
39+
return $this->client->exists($this->prefix . $key)->then(function ($result) use ($key) {
40+
if ($result == false) {
41+
return reject();
42+
}
43+
return $this->client->get($this->prefix . $key);
44+
});
3945
}
4046

4147
/**

tests/RedisTest.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
use Phake;
66
use Clue\React\Redis\Client;
7+
use React\EventLoop\Factory;
8+
use React\Promise\FulfilledPromise;
79
use React\Promise\PromiseInterface;
10+
use React\Promise\RejectedPromise;
811
use WyriHaximus\React\Cache\Redis;
12+
use function Clue\React\Block\await;
913

1014
class RedisTest extends \PHPUnit_Framework_TestCase
1115
{
@@ -24,12 +28,30 @@ public function testGet()
2428
{
2529
$prefix = 'root:';
2630
$key = 'key';
27-
$promise = Phake::mock(PromiseInterface::class);
28-
Phake::when($this->client)->get($prefix . $key)->thenReturn($promise);
29-
$result = (new Redis($this->client, $prefix))->get($key);
30-
$this->assertInstanceOf(PromiseInterface::class, $result);
31-
$this->assertSame($promise, $result);
32-
Phake::verify($this->client)->get($prefix . $key);
31+
$value = 'value';
32+
Phake::when($this->client)->exists($prefix . $key)->thenReturn(new FulfilledPromise(1));
33+
Phake::when($this->client)->get($prefix . $key)->thenReturn(new FulfilledPromise($value));
34+
$promise = (new Redis($this->client, $prefix))->get($key);
35+
$this->assertInstanceOf(PromiseInterface::class, $promise);
36+
$result = await($promise, Factory::create());
37+
$this->assertSame($value, $result);
38+
Phake::inOrder(
39+
Phake::verify($this->client)->exists($prefix . $key),
40+
Phake::verify($this->client)->get($prefix . $key)
41+
);
42+
}
43+
44+
public function testGetNonExistant()
45+
{
46+
$prefix = 'root:';
47+
$key = 'key';
48+
Phake::when($this->client)->exists($prefix . $key)->thenReturn(new FulfilledPromise(0));
49+
Phake::when($this->client)->get($prefix . $key)->thenReturn(new RejectedPromise());
50+
$promise = (new Redis($this->client, $prefix))->get($key);
51+
$this->assertInstanceOf(PromiseInterface::class, $promise);
52+
$this->assertInstanceOf(RejectedPromise::class, $promise);
53+
Phake::verify($this->client)->exists($prefix . $key);
54+
Phake::verify($this->client, Phake::never())->get($prefix . $key);
3355
}
3456

3557
public function testSet()

0 commit comments

Comments
 (0)