7
7
8
8
class FactoryTest extends TestCase
9
9
{
10
- private $ loop ;
11
10
private $ tcp ;
12
11
private $ factory ;
13
12
@@ -16,18 +15,22 @@ class FactoryTest extends TestCase
16
15
*/
17
16
public function setUpFactory ()
18
17
{
19
- $ this -> loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
18
+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
20
19
$ this ->tcp = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
21
20
22
- $ this ->factory = new Factory ($ this -> loop , $ this ->tcp );
21
+ $ this ->factory = new Factory ($ loop , $ this ->tcp );
23
22
}
24
23
25
- /**
26
- * @doesNotPerformAssertions
27
- */
28
- public function testDefaultCtor ()
24
+ public function testDefaultCtorCreatesConnectorAutomatically ()
29
25
{
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 );
31
34
}
32
35
33
36
public function testCreateClientUsesDefaultPortForTcpConnection ()
@@ -97,6 +100,49 @@ public function testCreateClientWithAuthenticationWillSendLoginActionWithDecoded
97
100
$ clientConnected ($ client );
98
101
}
99
102
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
+
100
146
public function testCreateClientWithInvalidUrlWillRejectPromise ()
101
147
{
102
148
$ promise = $ this ->factory ->createClient ('/// ' );
0 commit comments