@@ -80,14 +80,18 @@ public function testConnectWillRejectWithTlsUriWhenUnderlyingConnectorRejects()
8080 )));
8181
8282 $ promise = $ this ->connector ->connect ('example.com:80 ' );
83- $ promise ->cancel ();
8483
85- $ this ->setExpectedException (
86- 'RuntimeException ' ,
87- 'Connection to tls://example.com:80 failed: Connection refused (ECONNREFUSED) ' ,
88- defined ('SOCKET_ECONNREFUSED ' ) ? SOCKET_ECONNREFUSED : 111
89- );
90- $ this ->throwRejection ($ promise );
84+ $ exception = null ;
85+ $ promise ->then (null , function ($ reason ) use (&$ exception ) {
86+ $ exception = $ reason ;
87+ });
88+
89+ assert ($ exception instanceof \RuntimeException);
90+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception );
91+ $ this ->assertEquals ('Connection to tls://example.com:80 failed: Connection refused (ECONNREFUSED) ' , $ exception ->getMessage ());
92+ $ this ->assertEquals (defined ('SOCKET_ECONNREFUSED ' ) ? SOCKET_ECONNREFUSED : 111 , $ exception ->getCode ());
93+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception ->getPrevious ());
94+ $ this ->assertNotEquals ('' , $ exception ->getTraceAsString ());
9195 }
9296
9397 public function testConnectWillRejectWithOriginalMessageWhenUnderlyingConnectorRejectsWithInvalidArgumentException ()
@@ -98,14 +102,18 @@ public function testConnectWillRejectWithOriginalMessageWhenUnderlyingConnectorR
98102 )));
99103
100104 $ promise = $ this ->connector ->connect ('example.com:80 ' );
101- $ promise ->cancel ();
102105
103- $ this ->setExpectedException (
104- 'InvalidArgumentException ' ,
105- 'Invalid ' ,
106- 42
107- );
108- $ this ->throwRejection ($ promise );
106+ $ exception = null ;
107+ $ promise ->then (null , function ($ reason ) use (&$ exception ) {
108+ $ exception = $ reason ;
109+ });
110+
111+ assert ($ exception instanceof \InvalidArgumentException);
112+ $ this ->assertInstanceOf ('InvalidArgumentException ' , $ exception );
113+ $ this ->assertEquals ('Invalid ' , $ exception ->getMessage ());
114+ $ this ->assertEquals (42 , $ exception ->getCode ());
115+ $ this ->assertNull ($ exception ->getPrevious ());
116+ $ this ->assertNotEquals ('' , $ exception ->getTraceAsString ());
109117 }
110118
111119 public function testCancelDuringTcpConnectionCancelsTcpConnection ()
@@ -128,12 +136,17 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionAndRejectsWithT
128136 $ promise = $ this ->connector ->connect ('example.com:80 ' );
129137 $ promise ->cancel ();
130138
131- $ this ->setExpectedException (
132- 'RuntimeException ' ,
133- 'Connection to tls://example.com:80 cancelled (ECONNABORTED) ' ,
134- defined ('SOCKET_ECONNABORTED ' ) ? SOCKET_ECONNABORTED : 103
135- );
136- $ this ->throwRejection ($ promise );
139+ $ exception = null ;
140+ $ promise ->then (null , function ($ reason ) use (&$ exception ) {
141+ $ exception = $ reason ;
142+ });
143+
144+ assert ($ exception instanceof \RuntimeException);
145+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception );
146+ $ this ->assertEquals ('Connection to tls://example.com:80 cancelled (ECONNABORTED) ' , $ exception ->getMessage ());
147+ $ this ->assertEquals (defined ('SOCKET_ECONNABORTED ' ) ? SOCKET_ECONNABORTED : 103 , $ exception ->getCode ());
148+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception ->getPrevious ());
149+ $ this ->assertNotEquals ('' , $ exception ->getTraceAsString ());
137150 }
138151
139152 public function testConnectionWillBeClosedAndRejectedIfConnectionIsNoStream ()
@@ -145,8 +158,17 @@ public function testConnectionWillBeClosedAndRejectedIfConnectionIsNoStream()
145158
146159 $ promise = $ this ->connector ->connect ('example.com:80 ' );
147160
148- $ this ->setExpectedException ('UnexpectedValueException ' , 'Base connector does not use internal Connection class exposing stream resource ' );
149- $ this ->throwRejection ($ promise );
161+ $ exception = null ;
162+ $ promise ->then (null , function ($ reason ) use (&$ exception ) {
163+ $ exception = $ reason ;
164+ });
165+
166+ assert ($ exception instanceof \UnexpectedValueException);
167+ $ this ->assertInstanceOf ('UnexpectedValueException ' , $ exception );
168+ $ this ->assertEquals ('Base connector does not use internal Connection class exposing stream resource ' , $ exception ->getMessage ());
169+ $ this ->assertEquals (0 , $ exception ->getCode ());
170+ $ this ->assertNull ($ exception ->getPrevious ());
171+ $ this ->assertNotEquals ('' , $ exception ->getTraceAsString ());
150172 }
151173
152174 public function testStreamEncryptionWillBeEnabledAfterConnecting ()
@@ -160,10 +182,9 @@ public function testStreamEncryptionWillBeEnabledAfterConnecting()
160182 $ ref ->setAccessible (true );
161183 $ ref ->setValue ($ this ->connector , $ encryption );
162184
163- $ pending = new Promise \Promise (function () { }, function () { throw new \RuntimeException ('Connection cancelled ' ); });
164185 $ this ->tcp ->expects ($ this ->once ())->method ('connect ' )->with ($ this ->equalTo ('example.com:80 ' ))->willReturn (Promise \resolve ($ connection ));
165186
166- $ promise = $ this ->connector ->connect ('example.com:80 ' );
187+ $ this ->connector ->connect ('example.com:80 ' );
167188 }
168189
169190 public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConnection ()
@@ -178,18 +199,21 @@ public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConn
178199 $ ref ->setAccessible (true );
179200 $ ref ->setValue ($ this ->connector , $ encryption );
180201
181- $ pending = new Promise \Promise (function () { }, function () { throw new \RuntimeException ('Connection cancelled ' ); });
182202 $ this ->tcp ->expects ($ this ->once ())->method ('connect ' )->with ($ this ->equalTo ('example.com:80 ' ))->willReturn (Promise \resolve ($ connection ));
183203
184204 $ promise = $ this ->connector ->connect ('example.com:80 ' );
185205
186- try {
187- $ this ->throwRejection ($ promise );
188- } catch (\RuntimeException $ e ) {
189- $ this ->assertEquals ('Connection to tls://example.com:80 failed during TLS handshake: TLS error ' , $ e ->getMessage ());
190- $ this ->assertEquals (123 , $ e ->getCode ());
191- $ this ->assertNull ($ e ->getPrevious ());
192- }
206+ $ exception = null ;
207+ $ promise ->then (null , function ($ reason ) use (&$ exception ) {
208+ $ exception = $ reason ;
209+ });
210+
211+ assert ($ exception instanceof \RuntimeException);
212+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception );
213+ $ this ->assertEquals ('Connection to tls://example.com:80 failed during TLS handshake: TLS error ' , $ exception ->getMessage ());
214+ $ this ->assertEquals (123 , $ exception ->getCode ());
215+ $ this ->assertNull ($ exception ->getPrevious ());
216+ $ this ->assertNotEquals ('' , $ exception ->getTraceAsString ());
193217 }
194218
195219 public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnection ()
@@ -212,12 +236,17 @@ public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnec
212236 $ promise = $ this ->connector ->connect ('example.com:80 ' );
213237 $ promise ->cancel ();
214238
215- $ this ->setExpectedException (
216- 'RuntimeException ' ,
217- 'Connection to tls://example.com:80 cancelled during TLS handshake (ECONNABORTED) ' ,
218- defined ('SOCKET_ECONNABORTED ' ) ? SOCKET_ECONNABORTED : 103
219- );
220- $ this ->throwRejection ($ promise );
239+ $ exception = null ;
240+ $ promise ->then (null , function ($ reason ) use (&$ exception ) {
241+ $ exception = $ reason ;
242+ });
243+
244+ assert ($ exception instanceof \RuntimeException);
245+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception );
246+ $ this ->assertEquals ('Connection to tls://example.com:80 cancelled during TLS handshake (ECONNABORTED) ' , $ exception ->getMessage ());
247+ $ this ->assertEquals (defined ('SOCKET_ECONNABORTED ' ) ? SOCKET_ECONNABORTED : 103 , $ exception ->getCode ());
248+ $ this ->assertNull ($ exception ->getPrevious ());
249+ $ this ->assertNotEquals ('' , $ exception ->getTraceAsString ());
221250 }
222251
223252 public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences ()
@@ -267,14 +296,4 @@ public function testRejectionDuringTlsHandshakeShouldNotCreateAnyGarbageReferenc
267296
268297 $ this ->assertEquals (0 , gc_collect_cycles ());
269298 }
270-
271- private function throwRejection ($ promise )
272- {
273- $ ex = null ;
274- $ promise ->then (null , function ($ e ) use (&$ ex ) {
275- $ ex = $ e ;
276- });
277-
278- throw $ ex ;
279- }
280299}
0 commit comments