@@ -80,14 +80,18 @@ public function testConnectWillRejectWithTlsUriWhenUnderlyingConnectorRejects()
80
80
)));
81
81
82
82
$ promise = $ this ->connector ->connect ('example.com:80 ' );
83
- $ promise ->cancel ();
84
83
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 ());
91
95
}
92
96
93
97
public function testConnectWillRejectWithOriginalMessageWhenUnderlyingConnectorRejectsWithInvalidArgumentException ()
@@ -98,14 +102,18 @@ public function testConnectWillRejectWithOriginalMessageWhenUnderlyingConnectorR
98
102
)));
99
103
100
104
$ promise = $ this ->connector ->connect ('example.com:80 ' );
101
- $ promise ->cancel ();
102
105
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 ());
109
117
}
110
118
111
119
public function testCancelDuringTcpConnectionCancelsTcpConnection ()
@@ -128,12 +136,17 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionAndRejectsWithT
128
136
$ promise = $ this ->connector ->connect ('example.com:80 ' );
129
137
$ promise ->cancel ();
130
138
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 ());
137
150
}
138
151
139
152
public function testConnectionWillBeClosedAndRejectedIfConnectionIsNoStream ()
@@ -145,8 +158,17 @@ public function testConnectionWillBeClosedAndRejectedIfConnectionIsNoStream()
145
158
146
159
$ promise = $ this ->connector ->connect ('example.com:80 ' );
147
160
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 ());
150
172
}
151
173
152
174
public function testStreamEncryptionWillBeEnabledAfterConnecting ()
@@ -160,10 +182,9 @@ public function testStreamEncryptionWillBeEnabledAfterConnecting()
160
182
$ ref ->setAccessible (true );
161
183
$ ref ->setValue ($ this ->connector , $ encryption );
162
184
163
- $ pending = new Promise \Promise (function () { }, function () { throw new \RuntimeException ('Connection cancelled ' ); });
164
185
$ this ->tcp ->expects ($ this ->once ())->method ('connect ' )->with ($ this ->equalTo ('example.com:80 ' ))->willReturn (Promise \resolve ($ connection ));
165
186
166
- $ promise = $ this ->connector ->connect ('example.com:80 ' );
187
+ $ this ->connector ->connect ('example.com:80 ' );
167
188
}
168
189
169
190
public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConnection ()
@@ -178,18 +199,21 @@ public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConn
178
199
$ ref ->setAccessible (true );
179
200
$ ref ->setValue ($ this ->connector , $ encryption );
180
201
181
- $ pending = new Promise \Promise (function () { }, function () { throw new \RuntimeException ('Connection cancelled ' ); });
182
202
$ this ->tcp ->expects ($ this ->once ())->method ('connect ' )->with ($ this ->equalTo ('example.com:80 ' ))->willReturn (Promise \resolve ($ connection ));
183
203
184
204
$ promise = $ this ->connector ->connect ('example.com:80 ' );
185
205
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 ());
193
217
}
194
218
195
219
public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnection ()
@@ -212,12 +236,17 @@ public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnec
212
236
$ promise = $ this ->connector ->connect ('example.com:80 ' );
213
237
$ promise ->cancel ();
214
238
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 ());
221
250
}
222
251
223
252
public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences ()
@@ -267,14 +296,4 @@ public function testRejectionDuringTlsHandshakeShouldNotCreateAnyGarbageReferenc
267
296
268
297
$ this ->assertEquals (0 , gc_collect_cycles ());
269
298
}
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
- }
280
299
}
0 commit comments