Skip to content

Commit b93de58

Browse files
committed
Forward compatibility with react/promise 3
1 parent dd3c325 commit b93de58

18 files changed

+130
-103
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ jobs:
99
name: PHPUnit (PHP ${{ matrix.php }} on ${{ matrix.os }})
1010
runs-on: ${{ matrix.os }}
1111
strategy:
12+
fail-fast: false
1213
matrix:
1314
os:
1415
- ubuntu-20.04

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
3131
"react/dns": "^1.8",
3232
"react/event-loop": "^1.2",
33-
"react/promise": "^2.6.0 || ^1.2.1",
34-
"react/promise-timer": "^1.8",
33+
"react/promise": "^3@dev || ^2.6 || ^1.2.1",
34+
"react/promise-timer": "^1.9",
3535
"react/stream": "^1.2"
3636
},
3737
"require-dev": {
3838
"clue/block-react": "^1.5",
3939
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
40-
"react/promise-stream": "^1.2"
40+
"react/promise-stream": "^1.4"
4141
},
4242
"autoload": {
4343
"psr-4": {
@@ -48,5 +48,6 @@
4848
"psr-4": {
4949
"React\\Tests\\Socket\\": "tests"
5050
}
51-
}
51+
},
52+
"minimum-stability": "dev"
5253
}

src/DnsConnector.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use React\Dns\Resolver\ResolverInterface;
66
use React\Promise;
7-
use React\Promise\CancellablePromiseInterface;
87

98
final class DnsConnector implements ConnectorInterface
109
{
@@ -103,7 +102,7 @@ function ($_, $reject) use (&$promise, &$resolved, $uri) {
103102
}
104103

105104
// (try to) cancel pending DNS lookup / connection attempt
106-
if ($promise instanceof CancellablePromiseInterface) {
105+
if ($promise instanceof Promise\PromiseInterface && \method_exists($promise, 'cancel')) {
107106
// overwrite callback arguments for PHP7+ only, so they do not show
108107
// up in the Exception trace and do not cause a possible cyclic reference.
109108
$_ = $reject = null;

src/HappyEyeBallsConnectionBuilder.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use React\EventLoop\LoopInterface;
88
use React\EventLoop\TimerInterface;
99
use React\Promise;
10-
use React\Promise\CancellablePromiseInterface;
1110

1211
/**
1312
* @internal
@@ -248,13 +247,13 @@ public function cleanUp()
248247
$this->connectQueue = array();
249248

250249
foreach ($this->connectionPromises as $connectionPromise) {
251-
if ($connectionPromise instanceof CancellablePromiseInterface) {
250+
if ($connectionPromise instanceof Promise\PromiseInterface && \method_exists($connectionPromise, 'cancel')) {
252251
$connectionPromise->cancel();
253252
}
254253
}
255254

256255
foreach ($this->resolverPromises as $resolverPromise) {
257-
if ($resolverPromise instanceof CancellablePromiseInterface) {
256+
if ($resolverPromise instanceof Promise\PromiseInterface && \method_exists($resolverPromise, 'cancel')) {
258257
$resolverPromise->cancel();
259258
}
260259
}

src/SecureConnector.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ public function connect($uri)
4343
$context = $this->context;
4444
$encryption = $this->streamEncryption;
4545
$connected = false;
46+
/** @var array<\React\Promise\PromiseInterface> $promises */
47+
$promises = array();
4648
/** @var \React\Promise\PromiseInterface $promise */
47-
$promise = $this->connector->connect(
49+
$promise = $promises[] = $this->connector->connect(
4850
\str_replace('tls://', '', $uri)
49-
)->then(function (ConnectionInterface $connection) use ($context, $encryption, $uri, &$promise, &$connected) {
51+
)->then(function (ConnectionInterface $connection) use ($context, $encryption, $uri, &$promises, &$connected) {
5052
// (unencrypted) TCP/IP connection succeeded
5153
$connected = true;
52-
5354
if (!$connection instanceof Connection) {
5455
$connection->close();
5556
throw new \UnexpectedValueException('Base connector does not use internal Connection class exposing stream resource');
@@ -61,7 +62,7 @@ public function connect($uri)
6162
}
6263

6364
// try to enable encryption
64-
return $promise = $encryption->enable($connection)->then(null, function ($error) use ($connection, $uri) {
65+
return $promises[] = $encryption->enable($connection)->then(null, function ($error) use ($connection, $uri) {
6566
// establishing encryption failed => close invalid connection and return error
6667
$connection->close();
6768

@@ -104,19 +105,21 @@ public function connect($uri)
104105
});
105106

106107
return new \React\Promise\Promise(
107-
function ($resolve, $reject) use ($promise) {
108+
function ($resolve, $reject) use (&$promise) {
108109
$promise->then($resolve, $reject);
109110
},
110-
function ($_, $reject) use (&$promise, $uri, &$connected) {
111+
function ($_, $reject) use ($promise, &$promises, $uri, &$connected) {
111112
if ($connected) {
112113
$reject(new \RuntimeException(
113114
'Connection to ' . $uri . ' cancelled during TLS handshake (ECONNABORTED)',
114115
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
115116
));
116117
}
117118

118-
$promise->cancel();
119-
$promise = null;
119+
foreach ($promises as $promise) {
120+
$promise->cancel();
121+
}
122+
$promises = array();
120123
}
121124
);
122125
}

src/StreamEncryption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function toggleCrypto($socket, Deferred $deferred, $toggle, $method)
115115
\restore_error_handler();
116116

117117
if (true === $result) {
118-
$deferred->resolve();
118+
$deferred->resolve(null);
119119
} else if (false === $result) {
120120
// overwrite callback arguments for PHP7+ only, so they do not show
121121
// up in the Exception trace and do not cause a possible cyclic reference.

tests/DnsConnectorTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,47 @@ public function setUpMocks()
2626
public function testPassByResolverIfGivenIp()
2727
{
2828
$this->resolver->expects($this->never())->method('resolve');
29-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\reject()));
29+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
3030

3131
$this->connector->connect('127.0.0.1:80');
3232
}
3333

3434
public function testPassThroughResolverIfGivenHost()
3535
{
3636
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
37-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject()));
37+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
3838

3939
$this->connector->connect('google.com:80');
4040
}
4141

4242
public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6()
4343
{
4444
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('::1')));
45-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject()));
45+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
4646

4747
$this->connector->connect('google.com:80');
4848
}
4949

5050
public function testPassByResolverIfGivenCompleteUri()
5151
{
5252
$this->resolver->expects($this->never())->method('resolve');
53-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject()));
53+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
5454

5555
$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
5656
}
5757

5858
public function testPassThroughResolverIfGivenCompleteUri()
5959
{
6060
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
61-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(Promise\reject()));
61+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
6262

6363
$this->connector->connect('scheme://google.com:80/path?query#fragment');
6464
}
6565

6666
public function testPassThroughResolverIfGivenExplicitHost()
6767
{
6868
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
69-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject()));
69+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
7070

7171
$this->connector->connect('scheme://google.com:80/?hostname=google.de');
7272
}
@@ -288,7 +288,7 @@ public function testRejectionDuringDnsLookupShouldNotCreateAnyGarbageReferences(
288288
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
289289
$this->tcp->expects($this->never())->method('connect');
290290

291-
$promise = $this->connector->connect('example.com:80');
291+
$promise = $this->connector->connect('example.com:80')->then(null, function () { });
292292
$dns->reject(new \RuntimeException('DNS failed'));
293293
unset($promise, $dns);
294294

@@ -309,7 +309,7 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferences()
309309
$tcp = new Deferred();
310310
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());
311311

312-
$promise = $this->connector->connect('example.com:80');
312+
$promise = $this->connector->connect('example.com:80')->then(null, function () { });
313313
$dns->resolve('1.2.3.4');
314314
$tcp->reject(new \RuntimeException('Connection failed'));
315315
unset($promise, $dns, $tcp);
@@ -334,7 +334,7 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferencesAg
334334
});
335335
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());
336336

337-
$promise = $this->connector->connect('example.com:80');
337+
$promise = $this->connector->connect('example.com:80')->then(null, function () { });
338338
$dns->resolve('1.2.3.4');
339339

340340
unset($promise, $dns, $tcp);
@@ -356,7 +356,7 @@ public function testCancelDuringDnsLookupShouldNotCreateAnyGarbageReferences()
356356
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
357357
$this->tcp->expects($this->never())->method('connect');
358358

359-
$promise = $this->connector->connect('example.com:80');
359+
$promise = $this->connector->connect('example.com:80')->then(null, function () { });
360360

361361
$promise->cancel();
362362
unset($promise, $dns);
@@ -379,7 +379,7 @@ public function testCancelDuringTcpConnectionShouldNotCreateAnyGarbageReferences
379379
});
380380
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp);
381381

382-
$promise = $this->connector->connect('example.com:80');
382+
$promise = $this->connector->connect('example.com:80')->then(function () { }, function () { });
383383
$dns->resolve('1.2.3.4');
384384

385385
$promise->cancel();

tests/FunctionalConnectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function testCancelPendingTlsConnectionDuringTlsHandshakeShouldCloseTcpCo
143143
$deferred = new Deferred();
144144
$server->on('connection', function (ConnectionInterface $connection) use ($promise, $deferred) {
145145
$connection->on('close', function () use ($deferred) {
146-
$deferred->resolve();
146+
$deferred->resolve(null);
147147
});
148148

149149
Loop::futureTick(function () use ($promise) {

tests/FunctionalTcpServerTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public function testEmitsConnectionForNewConnection()
1818
$server->on('connection', $this->expectCallableOnce());
1919

2020
$peer = new Promise(function ($resolve, $reject) use ($server) {
21-
$server->on('connection', $resolve);
21+
$server->on('connection', function () use ($resolve) {
22+
$resolve(null);
23+
});
2224
});
2325

2426
$connector = new TcpConnector();
@@ -57,7 +59,9 @@ public function testConnectionForNewConnectionWhenResumedAfterPause()
5759
$server->resume();
5860

5961
$peer = new Promise(function ($resolve, $reject) use ($server) {
60-
$server->on('connection', $resolve);
62+
$server->on('connection', function () use ($resolve) {
63+
$resolve(null);
64+
});
6165
});
6266

6367
$connector = new TcpConnector();
@@ -207,7 +211,9 @@ public function testEmitsConnectionEvenIfClientConnectionIsCancelled()
207211
$server->on('connection', $this->expectCallableOnce());
208212

209213
$peer = new Promise(function ($resolve, $reject) use ($server) {
210-
$server->on('connection', $resolve);
214+
$server->on('connection', function () use ($resolve) {
215+
$resolve(null);
216+
});
211217
});
212218

213219
$connector = new TcpConnector();
@@ -232,7 +238,9 @@ public function testEmitsConnectionForNewIpv6Connection()
232238
$server->on('connection', $this->expectCallableOnce());
233239

234240
$peer = new Promise(function ($resolve, $reject) use ($server) {
235-
$server->on('connection', $resolve);
241+
$server->on('connection', function () use ($resolve) {
242+
$resolve(null);
243+
});
236244
});
237245

238246
$connector = new TcpConnector();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy