@@ -54,6 +54,49 @@ public function testCreateClientUsesTlsConnectorWithTlsLocation()
5454 $ this ->factory ->createClient ('tls://ami.local:1234 ' );
5555 }
5656
57+ public function testCreateClientResolvesWithClientWhenConnectionResolves ()
58+ {
59+ $ connection = $ this ->getMockBuilder ('React\Socket\ConnectionInterface ' )->getMock ();
60+ $ this ->tcp ->expects ($ this ->once ())->method ('connect ' )->willReturn (\React \Promise \resolve ($ connection ));
61+
62+ $ promise = $ this ->factory ->createClient ('localhost ' );
63+
64+ $ client = null ;
65+ $ promise ->then (function ($ value ) use (&$ client ) {
66+ $ client = $ value ;
67+ });
68+
69+ $ this ->assertInstanceOf ('Clue\React\Ami\Client ' , $ client );
70+ }
71+
72+ public function testCreateClientWithAuthenticationWillSendLoginActionWithDecodedUserInfo ()
73+ {
74+ $ promiseAuthenticated = $ this ->getMockBuilder ('React\Promise\PromiseInterface ' )->getMock ();
75+
76+ $ clientConnected = null ;
77+ $ promiseClient = $ this ->getMockBuilder ('React\Promise\PromiseInterface ' )->getMock ();
78+ $ promiseClient ->expects ($ this ->once ())->method ('then ' )->with ($ this ->callback (function ($ callback ) use (&$ clientConnected ) {
79+ $ clientConnected = $ callback ;
80+ return true ;
81+ }))->willReturn ($ promiseAuthenticated );
82+
83+ $ promiseConnecting = $ this ->getMockBuilder ('React\Promise\PromiseInterface ' )->getMock ();
84+ $ promiseConnecting ->expects ($ this ->once ())->method ('then ' )->willReturn ($ promiseClient );
85+ $ this ->tcp ->expects ($ this ->once ())->method ('connect ' )->willReturn ($ promiseConnecting );
86+
87+ $ action = $ this ->getMockBuilder ('Clue\React\Ami\Protocol\Action ' )->getMock ();
88+ $ client = $ this ->getMockBuilder ('Clue\React\Ami\Client ' )->disableOriginalConstructor ()->getMock ();
89+ $ client ->expects ($ this ->once ())->method ('createAction ' )->with ('Login ' , array ('UserName ' => 'user@host ' , 'Secret ' => 'pass+word! ' , 'Events ' => null ))->willReturn ($ action );
90+ $ client ->expects ($ this ->once ())->method ('request ' )->with ($ action )->willReturn ($ promiseAuthenticated );
91+
92+ $ promise = $ this ->factory ->createClient ('user%40host:pass+word%21@localhost ' );
93+
94+ $ this ->assertSame ($ promiseAuthenticated , $ promise );
95+
96+ $ this ->assertNotNull ($ clientConnected );
97+ $ clientConnected ($ client );
98+ }
99+
57100 public function testCreateClientWithInvalidUrlWillRejectPromise ()
58101 {
59102 $ promise = $ this ->factory ->createClient ('/// ' );
0 commit comments