@@ -131,6 +131,43 @@ public function testWillWriteAuthCommandIfRedisUnixUriContainsUserInfo()
131
131
$ this ->factory ->createClient ('redis+unix://hello:world@/tmp/redis.sock ' );
132
132
}
133
133
134
+ public function testWillResolveWhenAuthCommandReceivesOkResponseIfRedisUriContainsUserInfo ()
135
+ {
136
+ $ stream = $ this ->getMockBuilder ('React\Socket\Connection ' )->disableOriginalConstructor ()->setMethods (array ('write ' ))->getMock ();
137
+ $ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$4 \r\nauth \r\n$5 \r\nworld \r\n" );
138
+
139
+ $ this ->connector ->expects ($ this ->once ())->method ('connect ' )->willReturn (Promise \resolve ($ stream ));
140
+ $ promise = $ this ->factory ->createClient ('redis://:world@localhost ' );
141
+
142
+ $ stream ->emit ('data ' , array ("+OK \r\n" ));
143
+
144
+ $ promise ->then ($ this ->expectCallableOnceWith ($ this ->isInstanceOf ('Clue\React\Redis\Client ' )));
145
+ }
146
+
147
+ public function testWillRejectAndCloseAutomaticallyWhenAuthCommandReceivesErrorResponseIfRedisUriContainsUserInfo ()
148
+ {
149
+ $ stream = $ this ->getMockBuilder ('React\Socket\Connection ' )->disableOriginalConstructor ()->setMethods (array ('write ' , 'close ' ))->getMock ();
150
+ $ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$4 \r\nauth \r\n$5 \r\nworld \r\n" );
151
+ $ stream ->expects ($ this ->once ())->method ('close ' );
152
+
153
+ $ this ->connector ->expects ($ this ->once ())->method ('connect ' )->willReturn (Promise \resolve ($ stream ));
154
+ $ promise = $ this ->factory ->createClient ('redis://:world@localhost ' );
155
+
156
+ $ stream ->emit ('data ' , array ("-ERR invalid password \r\n" ));
157
+
158
+ $ promise ->then (null , $ this ->expectCallableOnceWith (
159
+ $ this ->logicalAnd (
160
+ $ this ->isInstanceOf ('RuntimeException ' ),
161
+ $ this ->callback (function (\Exception $ e ) {
162
+ return $ e ->getMessage () === 'Connection to Redis server failed because AUTH command failed ' ;
163
+ }),
164
+ $ this ->callback (function (\Exception $ e ) {
165
+ return $ e ->getPrevious ()->getMessage () === 'ERR invalid password ' ;
166
+ })
167
+ )
168
+ ));
169
+ }
170
+
134
171
public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter ()
135
172
{
136
173
$ stream = $ this ->getMockBuilder ('React\Socket\ConnectionInterface ' )->getMock ();
@@ -140,19 +177,63 @@ public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter
140
177
$ this ->factory ->createClient ('redis+unix:///tmp/redis.sock?db=demo ' );
141
178
}
142
179
180
+ public function testWillResolveWhenSelectCommandReceivesOkResponseIfRedisUriContainsPath ()
181
+ {
182
+ $ stream = $ this ->getMockBuilder ('React\Socket\Connection ' )->disableOriginalConstructor ()->setMethods (array ('write ' ))->getMock ();
183
+ $ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$6 \r\nselect \r\n$3 \r\n123 \r\n" );
184
+
185
+ $ this ->connector ->expects ($ this ->once ())->method ('connect ' )->willReturn (Promise \resolve ($ stream ));
186
+ $ promise = $ this ->factory ->createClient ('redis://localhost/123 ' );
187
+
188
+ $ stream ->emit ('data ' , array ("+OK \r\n" ));
189
+
190
+ $ promise ->then ($ this ->expectCallableOnceWith ($ this ->isInstanceOf ('Clue\React\Redis\Client ' )));
191
+ }
192
+
193
+ public function testWillRejectAndCloseAutomaticallyWhenSelectCommandReceivesErrorResponseIfRedisUriContainsPath ()
194
+ {
195
+ $ stream = $ this ->getMockBuilder ('React\Socket\Connection ' )->disableOriginalConstructor ()->setMethods (array ('write ' , 'close ' ))->getMock ();
196
+ $ stream ->expects ($ this ->once ())->method ('write ' )->with ("*2 \r\n$6 \r\nselect \r\n$3 \r\n123 \r\n" );
197
+ $ stream ->expects ($ this ->once ())->method ('close ' );
198
+
199
+ $ this ->connector ->expects ($ this ->once ())->method ('connect ' )->willReturn (Promise \resolve ($ stream ));
200
+ $ promise = $ this ->factory ->createClient ('redis://localhost/123 ' );
201
+
202
+ $ stream ->emit ('data ' , array ("-ERR DB index is out of range \r\n" ));
203
+
204
+ $ promise ->then (null , $ this ->expectCallableOnceWith (
205
+ $ this ->logicalAnd (
206
+ $ this ->isInstanceOf ('RuntimeException ' ),
207
+ $ this ->callback (function (\Exception $ e ) {
208
+ return $ e ->getMessage () === 'Connection to Redis server failed because SELECT command failed ' ;
209
+ }),
210
+ $ this ->callback (function (\Exception $ e ) {
211
+ return $ e ->getPrevious ()->getMessage () === 'ERR DB index is out of range ' ;
212
+ })
213
+ )
214
+ ));
215
+ }
216
+
143
217
public function testWillRejectIfConnectorRejects ()
144
218
{
145
219
$ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('127.0.0.1:2 ' )->willReturn (Promise \reject (new \RuntimeException ()));
146
220
$ promise = $ this ->factory ->createClient ('redis://127.0.0.1:2 ' );
147
221
148
- $ this ->expectPromiseReject ($ promise );
222
+ $ promise ->then (null , $ this ->expectCallableOnceWith (
223
+ $ this ->logicalAnd (
224
+ $ this ->isInstanceOf ('RuntimeException ' ),
225
+ $ this ->callback (function (\Exception $ e ) {
226
+ return $ e ->getMessage () === 'Connection to Redis server failed because underlying transport connection failed ' ;
227
+ })
228
+ )
229
+ ));
149
230
}
150
231
151
232
public function testWillRejectIfTargetIsInvalid ()
152
233
{
153
234
$ promise = $ this ->factory ->createClient ('http://invalid target ' );
154
235
155
- $ this ->expectPromiseReject ( $ promise );
236
+ $ promise -> then ( null , $ this ->expectCallableOnceWith ( $ this -> isInstanceOf ( ' InvalidArgumentException ' )) );
156
237
}
157
238
158
239
public function testCancelWillRejectPromise ()
@@ -173,6 +254,15 @@ public function testCancelWillCancelConnectorWhenConnectionIsPending()
173
254
174
255
$ promise = $ this ->factory ->createClient ('redis://127.0.0.1:2 ' );
175
256
$ promise ->cancel ();
257
+
258
+ $ promise ->then (null , $ this ->expectCallableOnceWith (
259
+ $ this ->logicalAnd (
260
+ $ this ->isInstanceOf ('RuntimeException ' ),
261
+ $ this ->callback (function (\Exception $ e ) {
262
+ return $ e ->getMessage () === 'Connection to Redis server cancelled ' ;
263
+ })
264
+ )
265
+ ));
176
266
}
177
267
178
268
public function testCancelWillCloseConnectionWhenConnectionWaitsForSelect ()
@@ -185,6 +275,15 @@ public function testCancelWillCloseConnectionWhenConnectionWaitsForSelect()
185
275
186
276
$ promise = $ this ->factory ->createClient ('redis://127.0.0.1:2/123 ' );
187
277
$ promise ->cancel ();
278
+
279
+ $ promise ->then (null , $ this ->expectCallableOnceWith (
280
+ $ this ->logicalAnd (
281
+ $ this ->isInstanceOf ('RuntimeException ' ),
282
+ $ this ->callback (function (\Exception $ e ) {
283
+ return $ e ->getMessage () === 'Connection to Redis server cancelled ' ;
284
+ })
285
+ )
286
+ ));
188
287
}
189
288
190
289
public function testCreateClientWithTimeoutParameterWillStartTimerAndRejectOnExplicitTimeout ()
@@ -205,9 +304,9 @@ public function testCreateClientWithTimeoutParameterWillStartTimerAndRejectOnExp
205
304
206
305
$ promise ->then (null , $ this ->expectCallableOnceWith (
207
306
$ this ->logicalAnd (
208
- $ this ->isInstanceOf ('Exception ' ),
307
+ $ this ->isInstanceOf ('RuntimeException ' ),
209
308
$ this ->callback (function (\Exception $ e ) {
210
- return $ e ->getMessage () === 'Connection to database server timed out after 0 seconds ' ;
309
+ return $ e ->getMessage () === 'Connection to Redis server timed out after 0 seconds ' ;
211
310
})
212
311
)
213
312
));
0 commit comments