77
88class FactoryTest extends TestCase
99{
10- private $ loop ;
1110 private $ tcp ;
1211 private $ factory ;
1312
@@ -16,18 +15,22 @@ class FactoryTest extends TestCase
1615 */
1716 public function setUpFactory ()
1817 {
19- $ this -> loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
18+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
2019 $ this ->tcp = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
2120
22- $ this ->factory = new Factory ($ this -> loop , $ this ->tcp );
21+ $ this ->factory = new Factory ($ loop , $ this ->tcp );
2322 }
2423
25- /**
26- * @doesNotPerformAssertions
27- */
28- public function testDefaultCtor ()
24+ public function testDefaultCtorCreatesConnectorAutomatically ()
2925 {
30- $ this ->factory = new Factory ($ this ->loop );
26+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
27+ $ this ->factory = new Factory ($ loop );
28+
29+ $ ref = new \ReflectionProperty ($ this ->factory , 'connector ' );
30+ $ ref ->setAccessible (true );
31+ $ connector = $ ref ->getValue ($ this ->factory );
32+
33+ $ this ->assertInstanceOf ('React\Socket\Connector ' , $ connector );
3134 }
3235
3336 public function testCreateClientUsesDefaultPortForTcpConnection ()
@@ -97,6 +100,49 @@ public function testCreateClientWithAuthenticationWillSendLoginActionWithDecoded
97100 $ clientConnected ($ client );
98101 }
99102
103+ public function testCreateClientWithAuthenticationResolvesWhenAuthenticationSucceeds ()
104+ {
105+ $ action = $ this ->getMockBuilder ('Clue\React\Ami\Protocol\Action ' )->getMock ();
106+ $ client = $ this ->getMockBuilder ('Clue\React\Ami\Client ' )->disableOriginalConstructor ()->getMock ();
107+ $ client ->expects ($ this ->once ())->method ('createAction ' )->willReturn ($ action );
108+ $ client ->expects ($ this ->once ())->method ('request ' )->with ($ action )->willReturn (\React \Promise \resolve ('ignored ' ));
109+
110+ $ promiseConnecting = $ this ->getMockBuilder ('React\Promise\PromiseInterface ' )->getMock ();
111+ $ promiseConnecting ->expects ($ this ->once ())->method ('then ' )->willReturn (\React \Promise \resolve ($ client ));
112+ $ this ->tcp ->expects ($ this ->once ())->method ('connect ' )->willReturn ($ promiseConnecting );
113+
114+ $ promise = $ this ->factory ->createClient ('user%40host:pass+word%21@localhost ' );
115+
116+ $ client = null ;
117+ $ promise ->then (function ($ value ) use (&$ client ) {
118+ $ client = $ value ;
119+ });
120+
121+ $ this ->assertInstanceOf ('Clue\React\Ami\Client ' , $ client );
122+ }
123+
124+ public function testCreateClientWithAuthenticationWillCloseClientAndRejectWhenLoginRequestRejects ()
125+ {
126+ $ error = new \RuntimeException ();
127+ $ action = $ this ->getMockBuilder ('Clue\React\Ami\Protocol\Action ' )->getMock ();
128+ $ client = $ this ->getMockBuilder ('Clue\React\Ami\Client ' )->disableOriginalConstructor ()->getMock ();
129+ $ client ->expects ($ this ->once ())->method ('createAction ' )->willReturn ($ action );
130+ $ client ->expects ($ this ->once ())->method ('request ' )->with ($ action )->willReturn (\React \Promise \reject ($ error ));
131+ $ client ->expects ($ this ->once ())->method ('close ' );
132+
133+ $ promiseConnecting = $ this ->getMockBuilder ('React\Promise\PromiseInterface ' )->getMock ();
134+ $ promiseConnecting ->expects ($ this ->once ())->method ('then ' )->willReturn (\React \Promise \resolve ($ client ));
135+ $ this ->tcp ->expects ($ this ->once ())->method ('connect ' )->willReturn ($ promiseConnecting );
136+
137+ $ promise = $ this ->factory ->createClient ('user%40host:pass+word%21@localhost ' );
138+
139+ $ exception = null ;
140+ $ promise ->then (null , function ($ reason ) use (&$ exception ) {
141+ $ exception = $ reason ;
142+ });
143+ $ this ->assertSame ($ error , $ exception );
144+ }
145+
100146 public function testCreateClientWithInvalidUrlWillRejectPromise ()
101147 {
102148 $ promise = $ this ->factory ->createClient ('/// ' );
0 commit comments