@@ -131,6 +131,43 @@ public function testWillWriteAuthCommandIfRedisUnixUriContainsUserInfo()
131131 $ this ->factory ->createClient ('redis+unix://hello:world@/tmp/redis.sock ' );
132132 }
133133
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+
134171 public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter ()
135172 {
136173 $ stream = $ this ->getMockBuilder ('React\Socket\ConnectionInterface ' )->getMock ();
@@ -140,19 +177,63 @@ public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter
140177 $ this ->factory ->createClient ('redis+unix:///tmp/redis.sock?db=demo ' );
141178 }
142179
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+
143217 public function testWillRejectIfConnectorRejects ()
144218 {
145219 $ this ->connector ->expects ($ this ->once ())->method ('connect ' )->with ('127.0.0.1:2 ' )->willReturn (Promise \reject (new \RuntimeException ()));
146220 $ promise = $ this ->factory ->createClient ('redis://127.0.0.1:2 ' );
147221
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+ ));
149230 }
150231
151232 public function testWillRejectIfTargetIsInvalid ()
152233 {
153234 $ promise = $ this ->factory ->createClient ('http://invalid target ' );
154235
155- $ this ->expectPromiseReject ( $ promise );
236+ $ promise -> then ( null , $ this ->expectCallableOnceWith ( $ this -> isInstanceOf ( ' InvalidArgumentException ' )) );
156237 }
157238
158239 public function testCancelWillRejectPromise ()
@@ -173,6 +254,15 @@ public function testCancelWillCancelConnectorWhenConnectionIsPending()
173254
174255 $ promise = $ this ->factory ->createClient ('redis://127.0.0.1:2 ' );
175256 $ 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+ ));
176266 }
177267
178268 public function testCancelWillCloseConnectionWhenConnectionWaitsForSelect ()
@@ -185,6 +275,15 @@ public function testCancelWillCloseConnectionWhenConnectionWaitsForSelect()
185275
186276 $ promise = $ this ->factory ->createClient ('redis://127.0.0.1:2/123 ' );
187277 $ 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+ ));
188287 }
189288
190289 public function testCreateClientWithTimeoutParameterWillStartTimerAndRejectOnExplicitTimeout ()
@@ -205,9 +304,9 @@ public function testCreateClientWithTimeoutParameterWillStartTimerAndRejectOnExp
205304
206305 $ promise ->then (null , $ this ->expectCallableOnceWith (
207306 $ this ->logicalAnd (
208- $ this ->isInstanceOf ('Exception ' ),
307+ $ this ->isInstanceOf ('RuntimeException ' ),
209308 $ 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 ' ;
211310 })
212311 )
213312 ));
0 commit comments