Skip to content

Commit 0d8cf56

Browse files
WyriHaximusclue
authored andcommitted
Forward compatibility with react/promise 3
1 parent 2f505b7 commit 0d8cf56

13 files changed

+61
-38
lines changed

composer.json

Lines changed: 3 additions & 3 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 || ^2.6 || ^1.2.1",
34+
"react/promise-timer": "^1.9",
3535
"react/stream": "^1.2"
3636
},
3737
"require-dev": {
3838
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
3939
"react/async": "^4 || ^3 || ^2",
40-
"react/promise-stream": "^1.2"
40+
"react/promise-stream": "^1.4"
4141
},
4242
"autoload": {
4343
"psr-4": {

src/DnsConnector.php

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

55
use React\Dns\Resolver\ResolverInterface;
66
use React\Promise;
7-
use React\Promise\CancellablePromiseInterface;
7+
use React\Promise\PromiseInterface;
88

99
final class DnsConnector implements ConnectorInterface
1010
{
@@ -103,7 +103,7 @@ function ($_, $reject) use (&$promise, &$resolved, $uri) {
103103
}
104104

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

src/HappyEyeBallsConnectionBuilder.php

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

1212
/**
1313
* @internal
@@ -248,13 +248,13 @@ public function cleanUp()
248248
$this->connectQueue = array();
249249

250250
foreach ($this->connectionPromises as $connectionPromise) {
251-
if ($connectionPromise instanceof CancellablePromiseInterface) {
251+
if ($connectionPromise instanceof PromiseInterface && \method_exists($connectionPromise, 'cancel')) {
252252
$connectionPromise->cancel();
253253
}
254254
}
255255

256256
foreach ($this->resolverPromises as $resolverPromise) {
257-
if ($resolverPromise instanceof CancellablePromiseInterface) {
257+
if ($resolverPromise instanceof PromiseInterface && \method_exists($resolverPromise, 'cancel')) {
258258
$resolverPromise->cancel();
259259
}
260260
}

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: 6 additions & 6 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
}

tests/FunctionalConnectorTest.php

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

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

tests/FunctionalTcpServerTest.php

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

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

2325
$connector = new TcpConnector();
@@ -58,7 +60,9 @@ public function testConnectionForNewConnectionWhenResumedAfterPause()
5860
$server->resume();
5961

6062
$peer = new Promise(function ($resolve, $reject) use ($server) {
61-
$server->on('connection', $resolve);
63+
$server->on('connection', function () use ($resolve) {
64+
$resolve(null);
65+
});
6266
});
6367

6468
$connector = new TcpConnector();
@@ -213,7 +217,9 @@ public function testEmitsConnectionEvenIfClientConnectionIsCancelled()
213217
$server->on('connection', $this->expectCallableOnce());
214218

215219
$peer = new Promise(function ($resolve, $reject) use ($server) {
216-
$server->on('connection', $resolve);
220+
$server->on('connection', function () use ($resolve) {
221+
$resolve(null);
222+
});
217223
});
218224

219225
$connector = new TcpConnector();
@@ -238,7 +244,9 @@ public function testEmitsConnectionForNewIpv6Connection()
238244
$server->on('connection', $this->expectCallableOnce());
239245

240246
$peer = new Promise(function ($resolve, $reject) use ($server) {
241-
$server->on('connection', $resolve);
247+
$server->on('connection', function () use ($resolve) {
248+
$resolve(null);
249+
});
242250
});
243251

244252
$connector = new TcpConnector();

tests/HappyEyeBallsConnectorTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function testThatAnyOtherPendingConnectionAttemptsWillBeCanceledOnceAConn
103103
public function testPassByResolverIfGivenIp()
104104
{
105105
$this->resolver->expects($this->never())->method('resolveAll');
106-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\resolve()));
106+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\resolve(null)));
107107

108108
$this->connector->connect('127.0.0.1:80');
109109

@@ -113,7 +113,7 @@ public function testPassByResolverIfGivenIp()
113113
public function testPassByResolverIfGivenIpv6()
114114
{
115115
$this->resolver->expects($this->never())->method('resolveAll');
116-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80'))->will($this->returnValue(Promise\reject()));
116+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
117117

118118
$this->connector->connect('[::1]:80');
119119

@@ -123,7 +123,7 @@ public function testPassByResolverIfGivenIpv6()
123123
public function testPassThroughResolverIfGivenHost()
124124
{
125125
$this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('1.2.3.4'))));
126-
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject()));
126+
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
127127

128128
$this->connector->connect('google.com:80');
129129

@@ -133,7 +133,7 @@ public function testPassThroughResolverIfGivenHost()
133133
public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6()
134134
{
135135
$this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('::1'))));
136-
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject()));
136+
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
137137

138138
$this->connector->connect('google.com:80');
139139

@@ -143,7 +143,7 @@ public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6()
143143
public function testPassByResolverIfGivenCompleteUri()
144144
{
145145
$this->resolver->expects($this->never())->method('resolveAll');
146-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject()));
146+
$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'))));
147147

148148
$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
149149

@@ -153,7 +153,7 @@ public function testPassByResolverIfGivenCompleteUri()
153153
public function testPassThroughResolverIfGivenCompleteUri()
154154
{
155155
$this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('1.2.3.4'))));
156-
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(Promise\reject()));
156+
$this->tcp->expects($this->exactly(2))->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'))));
157157

158158
$this->connector->connect('scheme://google.com:80/path?query#fragment');
159159

@@ -163,7 +163,7 @@ public function testPassThroughResolverIfGivenCompleteUri()
163163
public function testPassThroughResolverIfGivenExplicitHost()
164164
{
165165
$this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('1.2.3.4'))));
166-
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject()));
166+
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
167167

168168
$this->connector->connect('scheme://google.com:80/?hostname=google.de');
169169

@@ -184,7 +184,7 @@ public function testIpv6ResolvesFirstSoIsTheFirstToConnect(array $ipv6, array $i
184184
$this->returnValue(Promise\resolve($ipv6)),
185185
$this->returnValue($deferred->promise())
186186
);
187-
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(']:80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
187+
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(']:80/?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
188188

189189
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
190190

@@ -209,7 +209,7 @@ public function testIpv6DoesntResolvesWhileIpv4DoesFirstSoIpv4Connects(array $ip
209209
$this->returnValue($deferred->promise()),
210210
$this->returnValue(Promise\resolve($ipv4))
211211
);
212-
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
212+
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
213213

214214
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
215215

tests/LimitingServerTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ public function testSocketDisconnectionWillRemoveFromList()
152152

153153
$peer = new Promise(function ($resolve, $reject) use ($server) {
154154
$server->on('connection', function (ConnectionInterface $connection) use ($resolve) {
155-
$connection->on('close', $resolve);
155+
$connection->on('close', function () use ($resolve) {
156+
$resolve(null);
157+
});
156158
});
157159
});
158160

@@ -171,7 +173,9 @@ public function testPausingServerWillEmitOnlyOneButAcceptTwoConnectionsDueToOper
171173
$server->on('error', $this->expectCallableNever());
172174

173175
$peer = new Promise(function ($resolve, $reject) use ($server) {
174-
$server->on('connection', $resolve);
176+
$server->on('connection', function () use ($resolve) {
177+
$resolve(null);
178+
});
175179
});
176180

177181
$first = stream_socket_client($server->getAddress());
@@ -197,7 +201,7 @@ public function testPausingServerWillEmitTwoConnectionsFromBacklog()
197201
++$connections;
198202

199203
if ($connections >= 2) {
200-
$resolve();
204+
$resolve(null);
201205
}
202206
});
203207
});

tests/SecureConnectorTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,12 @@ public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnec
231231
$ref->setAccessible(true);
232232
$ref->setValue($this->connector, $encryption);
233233

234-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(Promise\resolve($connection));
234+
$deferred = new Deferred();
235+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn($deferred->promise());
235236

236237
$promise = $this->connector->connect('example.com:80');
238+
$deferred->resolve($connection);
239+
237240
$promise->cancel();
238241

239242
$exception = null;

tests/ServerTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public function testEmitsConnectionForNewConnection()
122122
$server->on('connection', $this->expectCallableOnce());
123123

124124
$peer = new Promise(function ($resolve, $reject) use ($server) {
125-
$server->on('connection', $resolve);
125+
$server->on('connection', function () use ($resolve) {
126+
$resolve(null);
127+
});
126128
});
127129

128130
$client = stream_socket_client($server->getAddress());
@@ -150,7 +152,9 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer()
150152
$server->on('connection', $this->expectCallableOnce());
151153

152154
$peer = new Promise(function ($resolve, $reject) use ($server) {
153-
$server->on('connection', $resolve);
155+
$server->on('connection', function () use ($resolve) {
156+
$resolve(null);
157+
});
154158
});
155159

156160
$client = stream_socket_client($server->getAddress());

tests/SocketServerTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ public function testEmitsConnectionForNewConnection()
157157
$socket->on('connection', $this->expectCallableOnce());
158158

159159
$peer = new Promise(function ($resolve, $reject) use ($socket) {
160-
$socket->on('connection', $resolve);
160+
$socket->on('connection', function () use ($resolve) {
161+
$resolve(null);
162+
});
161163
});
162164

163165
$client = stream_socket_client($socket->getAddress());
@@ -185,7 +187,9 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer()
185187
$socket->on('connection', $this->expectCallableOnce());
186188

187189
$peer = new Promise(function ($resolve, $reject) use ($socket) {
188-
$socket->on('connection', $resolve);
190+
$socket->on('connection', function () use ($resolve) {
191+
$resolve(null);
192+
});
189193
});
190194

191195
$client = stream_socket_client($socket->getAddress());

tests/TimeoutConnectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testRejectsWithOriginalReasonWhenConnectorRejects()
6161

6262
public function testResolvesWhenConnectorResolves()
6363
{
64-
$promise = Promise\resolve();
64+
$promise = Promise\resolve(null);
6565

6666
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
6767
$connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise));

0 commit comments

Comments
 (0)