Skip to content

Commit e01f93d

Browse files
committed
Clean up unneeded references in test suite
1 parent 4a96220 commit e01f93d

File tree

3 files changed

+101
-62
lines changed

3 files changed

+101
-62
lines changed

src/SecureConnector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function connect($uri)
4343
$context = $this->context;
4444
$encryption = $this->streamEncryption;
4545
$connected = false;
46+
/** @var \React\Promise\PromiseInterface $promise */
4647
$promise = $this->connector->connect(
4748
\str_replace('tls://', '', $uri)
4849
)->then(function (ConnectionInterface $connection) use ($context, $encryption, $uri, &$promise, &$connected) {

tests/DnsConnectorTest.php

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,18 @@ public function testConnectRejectsIfGivenIpAndTcpConnectorRejectsWithRuntimeExce
9292
$this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80')->willReturn($promise);
9393

9494
$promise = $this->connector->connect('1.2.3.4:80');
95-
$promise->cancel();
9695

97-
$this->setExpectedException('RuntimeException', 'Connection to tcp://1.2.3.4:80 failed: Connection failed', 42);
98-
$this->throwRejection($promise);
96+
$exception = null;
97+
$promise->then(null, function ($reason) use (&$exception) {
98+
$exception = $reason;
99+
});
100+
101+
assert($exception instanceof \RuntimeException);
102+
$this->assertInstanceOf('RuntimeException', $exception);
103+
$this->assertEquals('Connection to tcp://1.2.3.4:80 failed: Connection failed', $exception->getMessage());
104+
$this->assertEquals(42, $exception->getCode());
105+
$this->assertNull($exception->getPrevious());
106+
$this->assertNotEquals('', $exception->getTraceAsString());
99107
}
100108

101109
public function testConnectRejectsIfGivenIpAndTcpConnectorRejectsWithInvalidArgumentException()
@@ -105,10 +113,18 @@ public function testConnectRejectsIfGivenIpAndTcpConnectorRejectsWithInvalidArgu
105113
$this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80')->willReturn($promise);
106114

107115
$promise = $this->connector->connect('1.2.3.4:80');
108-
$promise->cancel();
109116

110-
$this->setExpectedException('InvalidArgumentException', 'Invalid', 42);
111-
$this->throwRejection($promise);
117+
$exception = null;
118+
$promise->then(null, function ($reason) use (&$exception) {
119+
$exception = $reason;
120+
});
121+
122+
assert($exception instanceof \InvalidArgumentException);
123+
$this->assertInstanceOf('InvalidArgumentException', $exception);
124+
$this->assertEquals('Invalid', $exception->getMessage());
125+
$this->assertEquals(42, $exception->getCode());
126+
$this->assertNull($exception->getPrevious());
127+
$this->assertNotEquals('', $exception->getTraceAsString());
112128
}
113129

114130
public function testConnectRejectsWithOriginalHostnameInMessageAfterResolvingIfTcpConnectorRejectsWithRuntimeException()
@@ -139,10 +155,18 @@ public function testConnectRejectsWithOriginalExceptionAfterResolvingIfTcpConnec
139155
$this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($promise);
140156

141157
$promise = $this->connector->connect('example.com:80');
142-
$promise->cancel();
143158

144-
$this->setExpectedException('InvalidArgumentException', 'Invalid', 42);
145-
$this->throwRejection($promise);
159+
$exception = null;
160+
$promise->then(null, function ($reason) use (&$exception) {
161+
$exception = $reason;
162+
});
163+
164+
assert($exception instanceof \InvalidArgumentException);
165+
$this->assertInstanceOf('InvalidArgumentException', $exception);
166+
$this->assertEquals('Invalid', $exception->getMessage());
167+
$this->assertEquals(42, $exception->getCode());
168+
$this->assertNull($exception->getPrevious());
169+
$this->assertNotEquals('', $exception->getTraceAsString());
146170
}
147171

148172
public function testSkipConnectionIfDnsFails()
@@ -153,8 +177,17 @@ public function testSkipConnectionIfDnsFails()
153177

154178
$promise = $this->connector->connect('example.invalid:80');
155179

156-
$this->setExpectedException('RuntimeException', 'Connection to tcp://example.invalid:80 failed during DNS lookup: DNS error');
157-
$this->throwRejection($promise);
180+
$exception = null;
181+
$promise->then(null, function ($reason) use (&$exception) {
182+
$exception = $reason;
183+
});
184+
185+
assert($exception instanceof \RuntimeException);
186+
$this->assertInstanceOf('RuntimeException', $exception);
187+
$this->assertEquals('Connection to tcp://example.invalid:80 failed during DNS lookup: DNS error', $exception->getMessage());
188+
$this->assertEquals(0, $exception->getCode());
189+
$this->assertInstanceOf('RuntimeException', $exception->getPrevious());
190+
$this->assertNotEquals('', $exception->getTraceAsString());
158191
}
159192

160193
public function testRejectionExceptionUsesPreviousExceptionIfDnsFails()
@@ -179,12 +212,17 @@ public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
179212
$promise = $this->connector->connect('example.com:80');
180213
$promise->cancel();
181214

182-
$this->setExpectedException(
183-
'RuntimeException',
184-
'Connection to tcp://example.com:80 cancelled during DNS lookup (ECONNABORTED)',
185-
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
186-
);
187-
$this->throwRejection($promise);
215+
$exception = null;
216+
$promise->then(null, function ($reason) use (&$exception) {
217+
$exception = $reason;
218+
});
219+
220+
assert($exception instanceof \RuntimeException);
221+
$this->assertInstanceOf('RuntimeException', $exception);
222+
$this->assertEquals('Connection to tcp://example.com:80 cancelled during DNS lookup (ECONNABORTED)', $exception->getMessage());
223+
$this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode());
224+
$this->assertNull($exception->getPrevious());
225+
$this->assertNotEquals('', $exception->getTraceAsString());
188226
}
189227

190228
public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp()
@@ -349,14 +387,4 @@ public function testCancelDuringTcpConnectionShouldNotCreateAnyGarbageReferences
349387

350388
$this->assertEquals(0, gc_collect_cycles());
351389
}
352-
353-
private function throwRejection($promise)
354-
{
355-
$ex = null;
356-
$promise->then(null, function ($e) use (&$ex) {
357-
$ex = $e;
358-
});
359-
360-
throw $ex;
361-
}
362390
}

tests/SecureConnectorTest.php

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,18 @@ public function testConnectWillRejectWithOriginalMessageWhenUnderlyingConnectorR
102102
)));
103103

104104
$promise = $this->connector->connect('example.com:80');
105-
$promise->cancel();
106105

107-
$this->setExpectedException(
108-
'InvalidArgumentException',
109-
'Invalid',
110-
42
111-
);
112-
$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());
113117
}
114118

115119
public function testCancelDuringTcpConnectionCancelsTcpConnection()
@@ -154,8 +158,17 @@ public function testConnectionWillBeClosedAndRejectedIfConnectionIsNoStream()
154158

155159
$promise = $this->connector->connect('example.com:80');
156160

157-
$this->setExpectedException('UnexpectedValueException', 'Base connector does not use internal Connection class exposing stream resource');
158-
$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());
159172
}
160173

161174
public function testStreamEncryptionWillBeEnabledAfterConnecting()
@@ -169,10 +182,9 @@ public function testStreamEncryptionWillBeEnabledAfterConnecting()
169182
$ref->setAccessible(true);
170183
$ref->setValue($this->connector, $encryption);
171184

172-
$pending = new Promise\Promise(function () { }, function () { throw new \RuntimeException('Connection cancelled'); });
173185
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(Promise\resolve($connection));
174186

175-
$promise = $this->connector->connect('example.com:80');
187+
$this->connector->connect('example.com:80');
176188
}
177189

178190
public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConnection()
@@ -187,18 +199,21 @@ public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConn
187199
$ref->setAccessible(true);
188200
$ref->setValue($this->connector, $encryption);
189201

190-
$pending = new Promise\Promise(function () { }, function () { throw new \RuntimeException('Connection cancelled'); });
191202
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(Promise\resolve($connection));
192203

193204
$promise = $this->connector->connect('example.com:80');
194205

195-
try {
196-
$this->throwRejection($promise);
197-
} catch (\RuntimeException $e) {
198-
$this->assertEquals('Connection to tls://example.com:80 failed during TLS handshake: TLS error', $e->getMessage());
199-
$this->assertEquals(123, $e->getCode());
200-
$this->assertNull($e->getPrevious());
201-
}
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());
202217
}
203218

204219
public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnection()
@@ -221,12 +236,17 @@ public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnec
221236
$promise = $this->connector->connect('example.com:80');
222237
$promise->cancel();
223238

224-
$this->setExpectedException(
225-
'RuntimeException',
226-
'Connection to tls://example.com:80 cancelled during TLS handshake (ECONNABORTED)',
227-
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
228-
);
229-
$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());
230250
}
231251

232252
public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences()
@@ -276,14 +296,4 @@ public function testRejectionDuringTlsHandshakeShouldNotCreateAnyGarbageReferenc
276296

277297
$this->assertEquals(0, gc_collect_cycles());
278298
}
279-
280-
private function throwRejection($promise)
281-
{
282-
$ex = null;
283-
$promise->then(null, function ($e) use (&$ex) {
284-
$ex = $e;
285-
});
286-
287-
throw $ex;
288-
}
289299
}

0 commit comments

Comments
 (0)