44
55use Phake ;
66use Clue \React \Redis \Client ;
7+ use React \EventLoop \Factory ;
8+ use React \Promise \FulfilledPromise ;
79use React \Promise \PromiseInterface ;
10+ use React \Promise \RejectedPromise ;
811use WyriHaximus \React \Cache \Redis ;
12+ use function Clue \React \Block \await ;
913
1014class 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