@@ -109,8 +109,14 @@ public function testConnectWithInvalidUriWillRejectWithoutConnecting()
109
109
$ factory = new Factory ($ loop , $ connector );
110
110
$ promise = $ factory ->createConnection ('/// ' );
111
111
112
- $ this ->assertInstanceof ('React\Promise\PromiseInterface ' , $ promise );
113
- $ promise ->then (null , $ this ->expectCallableOnce ());
112
+ $ promise ->then (null , $ this ->expectCallableOnceWith (
113
+ $ this ->logicalAnd (
114
+ $ this ->isInstanceOf ('InvalidArgumentException ' ),
115
+ $ this ->callback (function (\InvalidArgumentException $ e ) {
116
+ return $ e ->getMessage () === 'Invalid MySQL URI given ' ;
117
+ })
118
+ )
119
+ ));
114
120
}
115
121
116
122
public function testConnectWithInvalidCharsetWillRejectWithoutConnecting ()
@@ -149,7 +155,7 @@ public function testConnectWithInvalidPassRejectsWithAuthenticationError()
149
155
$ this ->logicalAnd (
150
156
$ this ->isInstanceOf ('Exception ' ),
151
157
$ this ->callback (function (\Exception $ e ) {
152
- return !!preg_match ("/^Access denied for user '.*?'@'.*?' \(using password: YES\)$/ " , $ e ->getMessage ());
158
+ return !!preg_match ("/^Connection to mysql:\/\/[^ ]* failed during authentication: Access denied for user '.*?'@'.*?' \(using password: YES\)$/ " , $ e ->getMessage ());
153
159
})
154
160
)
155
161
));
@@ -180,15 +186,17 @@ public function testConnectWillRejectOnExplicitTimeoutDespiteValidAuth()
180
186
{
181
187
$ factory = new Factory ();
182
188
183
- $ uri = $ this ->getConnectionString () . '?timeout=0 ' ;
189
+ $ uri = ' mysql:// ' . $ this ->getConnectionString () . '?timeout=0 ' ;
184
190
185
191
$ promise = $ factory ->createConnection ($ uri );
186
192
193
+ $ uri = preg_replace ('/:[^:]*@/ ' , ':***@ ' , $ uri );
194
+
187
195
$ promise ->then (null , $ this ->expectCallableOnceWith (
188
196
$ this ->logicalAnd (
189
197
$ this ->isInstanceOf ('Exception ' ),
190
- $ this ->callback (function (\Exception $ e ) {
191
- return $ e ->getMessage () === 'Connection to database server timed out after 0 seconds ' ;
198
+ $ this ->callback (function (\Exception $ e ) use ( $ uri ) {
199
+ return $ e ->getMessage () === 'Connection to ' . $ uri . ' timed out after 0 seconds ' ;
192
200
})
193
201
)
194
202
));
@@ -200,18 +208,20 @@ public function testConnectWillRejectOnDefaultTimeoutFromIniDespiteValidAuth()
200
208
{
201
209
$ factory = new Factory ();
202
210
203
- $ uri = $ this ->getConnectionString ();
211
+ $ uri = ' mysql:// ' . $ this ->getConnectionString ();
204
212
205
213
$ old = ini_get ('default_socket_timeout ' );
206
214
ini_set ('default_socket_timeout ' , '0 ' );
207
215
$ promise = $ factory ->createConnection ($ uri );
208
216
ini_set ('default_socket_timeout ' , $ old );
209
217
218
+ $ uri = preg_replace ('/:[^:]*@/ ' , ':***@ ' , $ uri );
219
+
210
220
$ promise ->then (null , $ this ->expectCallableOnceWith (
211
221
$ this ->logicalAnd (
212
222
$ this ->isInstanceOf ('Exception ' ),
213
- $ this ->callback (function (\Exception $ e ) {
214
- return $ e ->getMessage () === 'Connection to database server timed out after 0 seconds ' ;
223
+ $ this ->callback (function (\Exception $ e ) use ( $ uri ) {
224
+ return $ e ->getMessage () === 'Connection to ' . $ uri . ' timed out after 0 seconds ' ;
215
225
})
216
226
)
217
227
));
@@ -382,22 +392,75 @@ public function testConnectWithValidAuthCanCloseAndAbortPing()
382
392
Loop::run ();
383
393
}
384
394
385
- public function testCancelConnectWillCancelPendingConnection ()
395
+ public function testlConnectWillRejectWhenUnderlyingConnectorRejects ()
396
+ {
397
+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
398
+ $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
399
+ $ connector ->expects ($ this ->once ())->method ('connect ' )->willReturn (\React \Promise \reject (new \RuntimeException ('Failed ' , 123 )));
400
+
401
+ $ factory = new Factory ($ loop , $ connector );
402
+ $ promise =
$ factory->
createConnection (
'user:[email protected] ' );
403
+
404
+ $ promise ->then (null , $ this ->expectCallableOnceWith ($ this ->isInstanceOf ('RuntimeException ' )));
405
+ $ promise ->then (null , $ this ->expectCallableOnceWith ($ this ->callback (function ($ e ) {
406
+ return ($ e ->getMessage () === 'Connection to mysql://user:***@127.0.0.1 failed: Failed ' );
407
+ })));
408
+ $ promise ->then (null , $ this ->expectCallableOnceWith ($ this ->callback (function ($ e ) {
409
+ return ($ e ->getCode () === 123 );
410
+ })));
411
+ }
412
+
413
+ public function provideUris ()
414
+ {
415
+ return [
416
+ [
417
+ 'localhost ' ,
418
+ 'mysql://localhost '
419
+ ],
420
+ [
421
+ 'mysql://localhost ' ,
422
+ 'mysql://localhost '
423
+ ],
424
+ [
425
+ 'mysql://user:pass@localhost ' ,
426
+ 'mysql://user:***@localhost '
427
+ ],
428
+ [
429
+ 'mysql://user:@localhost ' ,
430
+ 'mysql://user:***@localhost '
431
+ ],
432
+ [
433
+ 'mysql://user@localhost ' ,
434
+ 'mysql://user@localhost '
435
+ ]
436
+ ];
437
+ }
438
+
439
+ /**
440
+ * @dataProvider provideUris
441
+ * @param string $uri
442
+ * @param string $safe
443
+ */
444
+ public function testCancelConnectWillCancelPendingConnection ($ uri , $ safe )
386
445
{
387
446
$ pending = new Promise (function () { }, $ this ->expectCallableOnce ());
388
447
$ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
389
448
$ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
390
449
$ connector ->expects ($ this ->once ())->method ('connect ' )->willReturn ($ pending );
391
450
392
451
$ factory = new Factory ($ loop , $ connector );
393
- $ promise = $ factory ->createConnection (' 127.0.0.1 ' );
452
+ $ promise = $ factory ->createConnection ($ uri );
394
453
395
454
$ promise ->cancel ();
396
455
397
- $ promise ->then (null , $ this ->expectCallableOnceWith ($ this ->isInstanceOf ('RuntimeException ' )));
398
- $ promise ->then (null , $ this ->expectCallableOnceWith ($ this ->callback (function ($ e ) {
399
- return ($ e ->getMessage () === 'Connection to database server cancelled ' );
400
- })));
456
+ $ promise ->then (null , $ this ->expectCallableOnceWith (
457
+ $ this ->logicalAnd (
458
+ $ this ->isInstanceOf ('RuntimeException ' ),
459
+ $ this ->callback (function (\RuntimeException $ e ) use ($ safe ) {
460
+ return $ e ->getMessage () === 'Connection to ' . $ safe . ' cancelled ' ;
461
+ })
462
+ )
463
+ ));
401
464
}
402
465
403
466
public function testCancelConnectWillCancelPendingConnectionWithRuntimeException ()
@@ -416,7 +479,7 @@ public function testCancelConnectWillCancelPendingConnectionWithRuntimeException
416
479
417
480
$ promise ->then (null , $ this ->expectCallableOnceWith ($ this ->isInstanceOf ('RuntimeException ' )));
418
481
$ promise ->then (null , $ this ->expectCallableOnceWith ($ this ->callback (function ($ e ) {
419
- return ($ e ->getMessage () === 'Connection to database server cancelled ' );
482
+ return ($ e ->getMessage () === 'Connection to mysql://127.0.0.1 cancelled ' );
420
483
})));
421
484
}
422
485
@@ -436,7 +499,7 @@ public function testCancelConnectDuringAuthenticationWillCloseConnection()
436
499
437
500
$ promise ->then (null , $ this ->expectCallableOnceWith ($ this ->isInstanceOf ('RuntimeException ' )));
438
501
$ promise ->then (null , $ this ->expectCallableOnceWith ($ this ->callback (function ($ e ) {
439
- return ($ e ->getMessage () === 'Connection to database server cancelled ' );
502
+ return ($ e ->getMessage () === 'Connection to mysql://127.0.0.1 cancelled ' );
440
503
})));
441
504
}
442
505
0 commit comments