4
4
5
5
use Phake ;
6
6
use Clue \React \Redis \Client ;
7
+ use React \EventLoop \Factory ;
8
+ use React \Promise \FulfilledPromise ;
7
9
use React \Promise \PromiseInterface ;
10
+ use React \Promise \RejectedPromise ;
8
11
use WyriHaximus \React \Cache \Redis ;
12
+ use function Clue \React \Block \await ;
9
13
10
14
class RedisTest extends \PHPUnit_Framework_TestCase
11
15
{
@@ -24,12 +28,30 @@ public function testGet()
24
28
{
25
29
$ prefix = 'root: ' ;
26
30
$ 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 );
33
55
}
34
56
35
57
public function testSet ()
0 commit comments