Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
106 changes: 103 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -35,7 +36,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);
});
}

/**
Expand Down
34 changes: 28 additions & 6 deletions tests/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand Down