From 194679b332cd46f9ba31d018197a2335bb3c4e74 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 9 May 2016 15:38:55 +0200 Subject: [PATCH 1/3] get needs a check if the key exists --- src/Redis.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Redis.php b/src/Redis.php index 4cb6abd..10839a8 100644 --- a/src/Redis.php +++ b/src/Redis.php @@ -35,7 +35,12 @@ public function __construct(Client $client, $prefix = 'reach:cache:') */ public function get($key) { - return $this->client->get($this->prefix . $key); + return $this->client->exists($this->prefix . $key)->then(function ($result) use ($key) { + if ($result == false) { + return reject(); + } + return $this->client->get($this->prefix . $key); + }); } /** From bab29544b8bac05f33ec8d07a68dc99982b694af Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 9 May 2016 17:20:52 +0200 Subject: [PATCH 2/3] Updated test reflecting fix --- composer.json | 3 +- composer.lock | 106 ++++++++++++++++++++++++++++++++++++++++++-- tests/RedisTest.php | 34 +++++++++++--- 3 files changed, 133 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 4e3c14b..effd4e0 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "phpunit/phpunit": "^5.2.3", "squizlabs/php_codesniffer": "^2.6", "vectorface/dunit": "~2.0", - "phake/phake": "^2.3" + "phake/phake": "^2.3", + "clue/block-react": "^1.1" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index a1bb88e..2c0e9d7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "fb1d374f319910a46f62790ad3043c39", - "content-hash": "3a5cd508f798e9b025de0ddfa03901bd", + "hash": "ab11af98c62576179c631074989de223", + "content-hash": "a322d06b575ff20639757c6382804edb", "packages": [ { "name": "clue/redis-protocol", @@ -442,6 +442,54 @@ } ], "packages-dev": [ + { + "name": "clue/block-react", + "version": "v1.1.0", + "source": { + "type": "git", + "url": "https://github.com/clue/php-block-react.git", + "reference": "ed70f8d497dd265e30bc7dd19cf86b2e149b1ecf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/clue/php-block-react/zipball/ed70f8d497dd265e30bc7dd19cf86b2e149b1ecf", + "reference": "ed70f8d497dd265e30bc7dd19cf86b2e149b1ecf", + "shasum": "" + }, + "require": { + "php": ">=5.3", + "react/event-loop": "0.4.*|0.3.*", + "react/promise": "~2.1|~1.2", + "react/promise-timer": "~1.0" + }, + "type": "library", + "autoload": { + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christian Lück", + "email": "christian@lueck.tv" + } + ], + "description": "Integrate async React PHP components into your blocking environment", + "homepage": "https://github.com/clue/php-block-react", + "keywords": [ + "async", + "blocking", + "event loop", + "promise", + "reactphp", + "synchronous" + ], + "time": "2016-03-09 15:10:22" + }, { "name": "doctrine/instantiator", "version": "1.0.5", @@ -1079,6 +1127,56 @@ ], "time": "2016-04-20 14:39:26" }, + { + "name": "react/promise-timer", + "version": "v1.1.0", + "source": { + "type": "git", + "url": "https://github.com/reactphp/promise-timer.git", + "reference": "6a46050d801688e08d1a550b97b1f3735505c62a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/reactphp/promise-timer/zipball/6a46050d801688e08d1a550b97b1f3735505c62a", + "reference": "6a46050d801688e08d1a550b97b1f3735505c62a", + "shasum": "" + }, + "require": { + "php": ">=5.3", + "react/event-loop": "~0.4.0|~0.3.0", + "react/promise": "~2.1|~1.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "React\\Promise\\Timer\\": "src/" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christian Lück", + "email": "christian@lueck.tv" + } + ], + "description": "Trivial timeout implementation for Promises", + "homepage": "https://github.com/react/promise-timer", + "keywords": [ + "async", + "event-loop", + "promise", + "reactphp", + "timeout", + "timer" + ], + "time": "2016-02-29 19:03:45" + }, { "name": "sebastian/code-unit-reverse-lookup", "version": "1.0.0", @@ -1777,6 +1875,8 @@ "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "php": "^5.6||^7.0" + }, "platform-dev": [] } diff --git a/tests/RedisTest.php b/tests/RedisTest.php index 3310492..d49aedc 100644 --- a/tests/RedisTest.php +++ b/tests/RedisTest.php @@ -4,8 +4,12 @@ use Phake; use Clue\React\Redis\Client; +use React\EventLoop\Factory; +use React\Promise\FulfilledPromise; use React\Promise\PromiseInterface; +use React\Promise\RejectedPromise; use WyriHaximus\React\Cache\Redis; +use function Clue\React\Block\await; class RedisTest extends \PHPUnit_Framework_TestCase { @@ -24,12 +28,30 @@ public function testGet() { $prefix = 'root:'; $key = 'key'; - $promise = Phake::mock(PromiseInterface::class); - Phake::when($this->client)->get($prefix . $key)->thenReturn($promise); - $result = (new Redis($this->client, $prefix))->get($key); - $this->assertInstanceOf(PromiseInterface::class, $result); - $this->assertSame($promise, $result); - Phake::verify($this->client)->get($prefix . $key); + $value = 'value'; + Phake::when($this->client)->exists($prefix . $key)->thenReturn(new FulfilledPromise(1)); + Phake::when($this->client)->get($prefix . $key)->thenReturn(new FulfilledPromise($value)); + $promise = (new Redis($this->client, $prefix))->get($key); + $this->assertInstanceOf(PromiseInterface::class, $promise); + $result = await($promise, Factory::create()); + $this->assertSame($value, $result); + Phake::inOrder( + Phake::verify($this->client)->exists($prefix . $key), + Phake::verify($this->client)->get($prefix . $key) + ); + } + + public function testGetNonExistant() + { + $prefix = 'root:'; + $key = 'key'; + Phake::when($this->client)->exists($prefix . $key)->thenReturn(new FulfilledPromise(0)); + Phake::when($this->client)->get($prefix . $key)->thenReturn(new RejectedPromise()); + $promise = (new Redis($this->client, $prefix))->get($key); + $this->assertInstanceOf(PromiseInterface::class, $promise); + $this->assertInstanceOf(RejectedPromise::class, $promise); + Phake::verify($this->client)->exists($prefix . $key); + Phake::verify($this->client, Phake::never())->get($prefix . $key); } public function testSet() From 5b1ac31a948debd2c64416525ad921a53d12bcba Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 9 May 2016 17:43:21 +0200 Subject: [PATCH 3/3] Missing use function reject --- src/Redis.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Redis.php b/src/Redis.php index 10839a8..0c046ec 100644 --- a/src/Redis.php +++ b/src/Redis.php @@ -5,6 +5,7 @@ use Clue\React\Redis\Client; use React\Cache\CacheInterface; use React\Promise\PromiseInterface; +use function React\Promise\reject; class Redis implements CacheInterface {