Skip to content

Commit 4a96220

Browse files
committed
Fix invalid references in exception stack trace
1 parent f474156 commit 4a96220

File tree

4 files changed

+50
-28
lines changed

4 files changed

+50
-28
lines changed

src/DnsConnector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ function ($resolve, $reject) use (&$promise, &$resolved, $uri, $connector, $host
7373

7474
// Exception trace arguments are not available on some PHP 7.4 installs
7575
// @codeCoverageIgnoreStart
76-
foreach ($trace as &$one) {
76+
foreach ($trace as $ti => $one) {
7777
if (isset($one['args'])) {
78-
foreach ($one['args'] as &$arg) {
78+
foreach ($one['args'] as $ai => $arg) {
7979
if ($arg instanceof \Closure) {
80-
$arg = 'Object(' . \get_class($arg) . ')';
80+
$trace[$ti]['args'][$ai] = 'Object(' . \get_class($arg) . ')';
8181
}
8282
}
8383
}

src/SecureConnector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ public function connect($uri)
8686

8787
// Exception trace arguments are not available on some PHP 7.4 installs
8888
// @codeCoverageIgnoreStart
89-
foreach ($trace as &$one) {
89+
foreach ($trace as $ti => $one) {
9090
if (isset($one['args'])) {
91-
foreach ($one['args'] as &$arg) {
91+
foreach ($one['args'] as $ai => $arg) {
9292
if ($arg instanceof \Closure) {
93-
$arg = 'Object(' . \get_class($arg) . ')';
93+
$trace[$ti]['args'][$ai] = 'Object(' . \get_class($arg) . ')';
9494
}
9595
}
9696
}

tests/DnsConnectorTest.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,18 @@ public function testConnectRejectsWithOriginalHostnameInMessageAfterResolvingIfT
118118
$this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($promise);
119119

120120
$promise = $this->connector->connect('example.com:80');
121-
$promise->cancel();
122121

123-
$this->setExpectedException('RuntimeException', 'Connection to tcp://example.com:80 failed: Connection to tcp://1.2.3.4:80 failed: Connection failed', 42);
124-
$this->throwRejection($promise);
122+
$exception = null;
123+
$promise->then(null, function ($reason) use (&$exception) {
124+
$exception = $reason;
125+
});
126+
127+
assert($exception instanceof \RuntimeException);
128+
$this->assertInstanceOf('RuntimeException', $exception);
129+
$this->assertEquals('Connection to tcp://example.com:80 failed: Connection to tcp://1.2.3.4:80 failed: Connection failed', $exception->getMessage());
130+
$this->assertEquals(42, $exception->getCode());
131+
$this->assertInstanceOf('RuntimeException', $exception->getPrevious());
132+
$this->assertNotEquals('', $exception->getTraceAsString());
125133
}
126134

127135
public function testConnectRejectsWithOriginalExceptionAfterResolvingIfTcpConnectorRejectsWithInvalidArgumentException()
@@ -216,12 +224,17 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionWithTcpRejectio
216224

217225
$promise->cancel();
218226

219-
$this->setExpectedException(
220-
'RuntimeException',
221-
'Connection cancelled',
222-
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
223-
);
224-
$this->throwRejection($promise);
227+
$exception = null;
228+
$promise->then(null, function ($reason) use (&$exception) {
229+
$exception = $reason;
230+
});
231+
232+
assert($exception instanceof \RuntimeException);
233+
$this->assertInstanceOf('RuntimeException', $exception);
234+
$this->assertEquals('Connection to tcp://example.com:80 failed: Connection cancelled', $exception->getMessage());
235+
$this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode());
236+
$this->assertInstanceOf('RuntimeException', $exception->getPrevious());
237+
$this->assertNotEquals('', $exception->getTraceAsString());
225238
}
226239

227240
public function testRejectionDuringDnsLookupShouldNotCreateAnyGarbageReferences()

tests/SecureConnectorTest.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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()
@@ -128,12 +132,17 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionAndRejectsWithT
128132
$promise = $this->connector->connect('example.com:80');
129133
$promise->cancel();
130134

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);
135+
$exception = null;
136+
$promise->then(null, function ($reason) use (&$exception) {
137+
$exception = $reason;
138+
});
139+
140+
assert($exception instanceof \RuntimeException);
141+
$this->assertInstanceOf('RuntimeException', $exception);
142+
$this->assertEquals('Connection to tls://example.com:80 cancelled (ECONNABORTED)', $exception->getMessage());
143+
$this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode());
144+
$this->assertInstanceOf('RuntimeException', $exception->getPrevious());
145+
$this->assertNotEquals('', $exception->getTraceAsString());
137146
}
138147

139148
public function testConnectionWillBeClosedAndRejectedIfConnectionIsNoStream()

0 commit comments

Comments
 (0)