6
6
use Clue \React \Redis \Io \StreamingClient ;
7
7
use Clue \Tests \React \Redis \TestCase ;
8
8
use PHPUnit \Framework \MockObject \MockObject ;
9
+ use React \EventLoop \Loop ;
9
10
use React \EventLoop \LoopInterface ;
10
11
use React \EventLoop \TimerInterface ;
11
12
use React \Promise \Deferred ;
16
17
17
18
class FactoryStreamingClientTest extends TestCase
18
19
{
19
- /** @var MockObject */
20
- private $ loop ;
21
-
22
20
/** @var MockObject */
23
21
private $ connector ;
24
22
25
23
/** @var Factory */
26
24
private $ factory ;
27
25
28
- public function setUp (): void
29
- {
30
- $ this ->loop = $ this ->createMock (LoopInterface::class);
31
- $ this ->connector = $ this ->createMock (ConnectorInterface::class);
26
+ /** @var LoopInterface */
27
+ public static $ loop ;
32
28
33
- assert ( $ this -> loop instanceof LoopInterface);
34
- assert ( $ this -> connector instanceof ConnectorInterface);
35
- $ this -> factory = new Factory ( $ this -> loop , $ this -> connector );
29
+ public static function setUpBeforeClass (): void
30
+ {
31
+ self :: $ loop = Loop:: get ( );
36
32
}
37
33
38
- public function testConstructWithoutLoopAssignsLoopAutomatically (): void
34
+ public static function tearDownAfterClass (): void
39
35
{
40
- $ factory = new Factory ();
36
+ Loop::set (self ::$ loop );
37
+ }
41
38
42
- $ ref = new \ ReflectionProperty ( $ factory , ' loop ' );
43
- $ ref -> setAccessible ( true );
44
- $ loop = $ ref -> getValue ( $ factory );
39
+ public function setUp (): void
40
+ {
41
+ $ this -> connector = $ this -> createMock (ConnectorInterface::class );
45
42
46
- $ this ->assertInstanceOf (LoopInterface::class, $ loop );
43
+ assert ($ this ->connector instanceof ConnectorInterface);
44
+ $ this ->factory = new Factory ($ this ->connector );
47
45
}
48
46
49
47
/**
50
48
* @doesNotPerformAssertions
51
49
*/
52
50
public function testCtor (): void
53
51
{
54
- assert ($ this ->loop instanceof LoopInterface);
55
- $ this ->factory = new Factory ($ this ->loop );
52
+ $ this ->factory = new Factory ();
56
53
}
57
54
58
55
public function testWillConnectWithDefaultPort (): void
@@ -87,6 +84,10 @@ public function testWillWriteSelectCommandIfTargetContainsPath(): void
87
84
$ stream = $ this ->createMock (ConnectionInterface::class);
88
85
$ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$6 \r\nselect \r\n$4 \r\ndemo \r\n" );
89
86
87
+ $ loop = $ this ->createMock (LoopInterface::class);
88
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' );
89
+ Loop::set ($ loop );
90
+
90
91
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->willReturn (resolve ($ stream ));
91
92
$ this ->factory ->createClient ('redis://127.0.0.1/demo ' );
92
93
}
@@ -96,6 +97,10 @@ public function testWillWriteSelectCommandIfTargetContainsDbQueryParameter(): vo
96
97
$ stream = $ this ->createMock (ConnectionInterface::class);
97
98
$ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$6 \r\nselect \r\n$1 \r\n4 \r\n" );
98
99
100
+ $ loop = $ this ->createMock (LoopInterface::class);
101
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' );
102
+ Loop::set ($ loop );
103
+
99
104
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->willReturn (resolve ($ stream ));
100
105
$ this ->factory ->createClient ('redis://127.0.0.1?db=4 ' );
101
106
}
@@ -105,6 +110,10 @@ public function testWillWriteAuthCommandIfRedisUriContainsUserInfo(): void
105
110
$ stream = $ this ->createMock (ConnectionInterface::class);
106
111
$ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$4 \r\nauth \r\n$5 \r\nworld \r\n" );
107
112
113
+ $ loop = $ this ->createMock (LoopInterface::class);
114
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' );
115
+ Loop::set ($ loop );
116
+
108
117
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('example.com:6379 ' )->willReturn (resolve ($ stream ));
109
118
$ this ->
factory ->
createClient (
'redis://hello:[email protected] ' );
110
119
}
@@ -114,6 +123,10 @@ public function testWillWriteAuthCommandIfRedisUriContainsEncodedUserInfo(): voi
114
123
$ stream = $ this ->createMock (ConnectionInterface::class);
115
124
$ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$4 \r\nauth \r\n$5 \r\nh@llo \r\n" );
116
125
126
+ $ loop = $ this ->createMock (LoopInterface::class);
127
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' );
128
+ Loop::set ($ loop );
129
+
117
130
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('example.com:6379 ' )->willReturn (resolve ($ stream ));
118
131
$ this ->
factory ->
createClient (
'redis://:h%[email protected] ' );
119
132
}
@@ -123,6 +136,10 @@ public function testWillWriteAuthCommandIfTargetContainsPasswordQueryParameter()
123
136
$ stream = $ this ->createMock (ConnectionInterface::class);
124
137
$ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$4 \r\nauth \r\n$6 \r\nsecret \r\n" );
125
138
139
+ $ loop = $ this ->createMock (LoopInterface::class);
140
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' );
141
+ Loop::set ($ loop );
142
+
126
143
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('example.com:6379 ' )->willReturn (resolve ($ stream ));
127
144
$ this ->factory ->createClient ('redis://example.com?password=secret ' );
128
145
}
@@ -132,6 +149,10 @@ public function testWillWriteAuthCommandIfTargetContainsEncodedPasswordQueryPara
132
149
$ stream = $ this ->createMock (ConnectionInterface::class);
133
150
$ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$4 \r\nauth \r\n$5 \r\nh@llo \r\n" );
134
151
152
+ $ loop = $ this ->createMock (LoopInterface::class);
153
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' );
154
+ Loop::set ($ loop );
155
+
135
156
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('example.com:6379 ' )->willReturn (resolve ($ stream ));
136
157
$ this ->factory ->createClient ('redis://example.com?password=h%40llo ' );
137
158
}
@@ -141,6 +162,10 @@ public function testWillWriteAuthCommandIfRedissUriContainsUserInfo(): void
141
162
$ stream = $ this ->createMock (ConnectionInterface::class);
142
163
$ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$4 \r\nauth \r\n$5 \r\nworld \r\n" );
143
164
165
+ $ loop = $ this ->createMock (LoopInterface::class);
166
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' );
167
+ Loop::set ($ loop );
168
+
144
169
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('tls://example.com:6379 ' )->willReturn (resolve ($ stream ));
145
170
$ this ->
factory ->
createClient (
'rediss://hello:[email protected] ' );
146
171
}
@@ -150,6 +175,10 @@ public function testWillWriteAuthCommandIfRedisUnixUriContainsPasswordQueryParam
150
175
$ stream = $ this ->createMock (ConnectionInterface::class);
151
176
$ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$4 \r\nauth \r\n$5 \r\nworld \r\n" );
152
177
178
+ $ loop = $ this ->createMock (LoopInterface::class);
179
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' );
180
+ Loop::set ($ loop );
181
+
153
182
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('unix:///tmp/redis.sock ' )->willReturn (resolve ($ stream ));
154
183
$ this ->factory ->createClient ('redis+unix:///tmp/redis.sock?password=world ' );
155
184
}
@@ -168,6 +197,10 @@ public function testWillWriteAuthCommandIfRedisUnixUriContainsUserInfo(): void
168
197
$ stream = $ this ->createMock (ConnectionInterface::class);
169
198
$ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$4 \r\nauth \r\n$5 \r\nworld \r\n" );
170
199
200
+ $ loop = $ this ->createMock (LoopInterface::class);
201
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' );
202
+ Loop::set ($ loop );
203
+
171
204
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('unix:///tmp/redis.sock ' )->willReturn (resolve ($ stream ));
172
205
$ this ->factory ->createClient ('redis+unix://hello:world@/tmp/redis.sock ' );
173
206
}
@@ -275,6 +308,10 @@ public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter
275
308
$ stream = $ this ->createMock (ConnectionInterface::class);
276
309
$ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$6 \r\nselect \r\n$4 \r\ndemo \r\n" );
277
310
311
+ $ loop = $ this ->createMock (LoopInterface::class);
312
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' );
313
+ Loop::set ($ loop );
314
+
278
315
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('unix:///tmp/redis.sock ' )->willReturn (resolve ($ stream ));
279
316
$ this ->factory ->createClient ('redis+unix:///tmp/redis.sock?db=demo ' );
280
317
}
@@ -584,8 +621,11 @@ public function testCancelWillCloseConnectionWhenConnectionWaitsForSelect(): voi
584
621
585
622
public function testCreateClientWithTimeoutParameterWillStartTimerAndRejectOnExplicitTimeout (): void
586
623
{
624
+ $ loop = $ this ->createMock (LoopInterface::class);
625
+ Loop::set ($ loop );
626
+
587
627
$ timeout = null ;
588
- $ this -> loop ->expects ($ this ->once ())->method ('addTimer ' )->with (0 , $ this ->callback (function ($ cb ) use (&$ timeout ) {
628
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' )->with (0 , $ this ->callback (function ($ cb ) use (&$ timeout ) {
589
629
$ timeout = $ cb ;
590
630
return true ;
591
631
}));
@@ -613,7 +653,10 @@ public function testCreateClientWithTimeoutParameterWillStartTimerAndRejectOnExp
613
653
614
654
public function testCreateClientWithNegativeTimeoutParameterWillNotStartTimer (): void
615
655
{
616
- $ this ->loop ->expects ($ this ->never ())->method ('addTimer ' );
656
+ $ loop = $ this ->createMock (LoopInterface::class);
657
+ Loop::set ($ loop );
658
+
659
+ $ loop ->expects ($ this ->never ())->method ('addTimer ' );
617
660
618
661
$ deferred = new Deferred ();
619
662
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('127.0.0.1:2 ' )->willReturn ($ deferred ->promise ());
@@ -623,7 +666,9 @@ public function testCreateClientWithNegativeTimeoutParameterWillNotStartTimer():
623
666
624
667
public function testCreateClientWithoutTimeoutParameterWillStartTimerWithDefaultTimeoutFromIni (): void
625
668
{
626
- $ this ->loop ->expects ($ this ->once ())->method ('addTimer ' )->with (42 , $ this ->anything ());
669
+ $ loop = $ this ->createMock (LoopInterface::class);
670
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' )->with (42 , $ this ->anything ());
671
+ Loop::set ($ loop );
627
672
628
673
$ deferred = new Deferred ();
629
674
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('127.0.0.1:2 ' )->willReturn ($ deferred ->promise ());
@@ -637,9 +682,11 @@ public function testCreateClientWithoutTimeoutParameterWillStartTimerWithDefault
637
682
638
683
public function testCreateClientWillCancelTimerWhenConnectionResolves (): void
639
684
{
685
+ $ loop = $ this ->createMock (LoopInterface::class);
640
686
$ timer = $ this ->createMock (TimerInterface::class);
641
- $ this ->loop ->expects ($ this ->once ())->method ('addTimer ' )->willReturn ($ timer );
642
- $ this ->loop ->expects ($ this ->once ())->method ('cancelTimer ' )->with ($ timer );
687
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' )->willReturn ($ timer );
688
+ $ loop ->expects ($ this ->once ())->method ('cancelTimer ' )->with ($ timer );
689
+ Loop::set ($ loop );
643
690
644
691
$ deferred = new Deferred ();
645
692
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('127.0.0.1:6379 ' )->willReturn ($ deferred ->promise ());
@@ -652,9 +699,11 @@ public function testCreateClientWillCancelTimerWhenConnectionResolves(): void
652
699
653
700
public function testCreateClientWillCancelTimerWhenConnectionRejects (): void
654
701
{
702
+ $ loop = $ this ->createMock (LoopInterface::class);
655
703
$ timer = $ this ->createMock (TimerInterface::class);
656
- $ this ->loop ->expects ($ this ->once ())->method ('addTimer ' )->willReturn ($ timer );
657
- $ this ->loop ->expects ($ this ->once ())->method ('cancelTimer ' )->with ($ timer );
704
+ $ loop ->expects ($ this ->once ())->method ('addTimer ' )->willReturn ($ timer );
705
+ $ loop ->expects ($ this ->once ())->method ('cancelTimer ' )->with ($ timer );
706
+ Loop::set ($ loop );
658
707
659
708
$ deferred = new Deferred ();
660
709
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('127.0.0.1:6379 ' )->willReturn ($ deferred ->promise ());
0 commit comments