Skip to content

Commit c3b237c

Browse files
committed
Forward compatibility with react/promise 3
1 parent 28fac70 commit c3b237c

File tree

4 files changed

+48
-63
lines changed

4 files changed

+48
-63
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
3131
"react/dns": "^1.1",
3232
"react/event-loop": "^1.0 || ^0.5",
33-
"react/promise": "^2.6.0 || ^1.2.1",
33+
"react/promise": "dev-master as 2.999.9999",
3434
"react/promise-timer": "^1.4.0",
3535
"react/stream": "^1.1"
3636
},

tests/DnsConnectorTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,49 @@ 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

31-
$this->connector->connect('127.0.0.1:80');
31+
$this->connector->connect('127.0.0.1:80')->then(null, function () {});
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

39-
$this->connector->connect('google.com:80');
39+
$this->connector->connect('google.com:80')->then(null, function () {});
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

47-
$this->connector->connect('google.com:80');
47+
$this->connector->connect('google.com:80')->then(null, function () {});
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

55-
$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
55+
$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment')->then(null, function () {});
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

63-
$this->connector->connect('scheme://google.com:80/path?query#fragment');
63+
$this->connector->connect('scheme://google.com:80/path?query#fragment')->then(null, function () {});
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

71-
$this->connector->connect('scheme://google.com:80/?hostname=google.de');
71+
$this->connector->connect('scheme://google.com:80/?hostname=google.de')->then(null, function () {});
7272
}
7373

7474
public function testRejectsImmediatelyIfUriIsInvalid()
@@ -134,7 +134,7 @@ public function testRejectionExceptionUsesPreviousExceptionIfDnsFails()
134134

135135
public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
136136
{
137-
$pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
137+
$pending = new Promise\Promise(function () { }, function () { });
138138
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->will($this->returnValue($pending));
139139
$this->tcp->expects($this->never())->method('connect');
140140

@@ -157,7 +157,7 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp()
157157

158158
public function testCancelDuringTcpConnectionCancelsTcpConnectionAfterDnsIsResolved()
159159
{
160-
$pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
160+
$pending = new Promise\Promise(function () { }, function () { });
161161
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn(Promise\resolve('1.2.3.4'));
162162
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($pending);
163163

@@ -196,7 +196,7 @@ public function testRejectionDuringDnsLookupShouldNotCreateAnyGarbageReferences(
196196
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
197197
$this->tcp->expects($this->never())->method('connect');
198198

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

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

220-
$promise = $this->connector->connect('example.com:80');
220+
$promise = $this->connector->connect('example.com:80')->then(null, function () { });
221221
$dns->resolve('1.2.3.4');
222222
$tcp->reject(new \RuntimeException('Connection failed'));
223223
unset($promise, $dns, $tcp);
@@ -242,7 +242,7 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferencesAg
242242
});
243243
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());
244244

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

248248
unset($promise, $dns, $tcp);
@@ -264,7 +264,7 @@ public function testCancelDuringDnsLookupShouldNotCreateAnyGarbageReferences()
264264
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
265265
$this->tcp->expects($this->never())->method('connect');
266266

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

269269
$promise->cancel();
270270
unset($promise, $dns);

tests/HappyEyeBallsConnectionBuilderTest.php

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,9 @@ public function testConnectWillRejectWhenBothDnsLookupsReject()
5353
$parts = parse_url($uri);
5454

5555
$builder = new HappyEyeBallsConnectionBuilder($loop, $connector, $resolver, $uri, $host, $parts);
56-
5756
$promise = $builder->connect();
5857

59-
$exception = null;
60-
$promise->then(null, function ($e) use (&$exception) {
61-
$exception = $e;
62-
});
63-
64-
$this->assertInstanceOf('RuntimeException', $exception);
65-
$this->assertEquals('Connection to tcp://reactphp.org:80 failed during DNS lookup: DNS lookup error', $exception->getMessage());
58+
$this->setExpectedException('RuntimeException', 'Connection to tcp://reactphp.org:80 failed during DNS lookup: DNS lookup error');
6659
}
6760

6861
public function testConnectWillRejectWhenBothDnsLookupsRejectWithDifferentMessages()
@@ -89,16 +82,10 @@ public function testConnectWillRejectWhenBothDnsLookupsRejectWithDifferentMessag
8982

9083
$builder = new HappyEyeBallsConnectionBuilder($loop, $connector, $resolver, $uri, $host, $parts);
9184

85+
$this->setExpectedException('RuntimeException', 'Connection to tcp://reactphp.org:80 failed during DNS lookup. Last error for IPv6: DNS6 error. Previous error for IPv4: DNS4 error');
86+
9287
$promise = $builder->connect();
9388
$deferred->reject(new \RuntimeException('DNS6 error'));
94-
95-
$exception = null;
96-
$promise->then(null, function ($e) use (&$exception) {
97-
$exception = $e;
98-
});
99-
100-
$this->assertInstanceOf('RuntimeException', $exception);
101-
$this->assertEquals('Connection to tcp://reactphp.org:80 failed during DNS lookup. Last error for IPv6: DNS6 error. Previous error for IPv4: DNS4 error', $exception->getMessage());
10289
}
10390

10491
public function testConnectWillStartDelayTimerWhenIpv4ResolvesAndIpv6IsPending()
@@ -249,8 +236,10 @@ public function testConnectWillStartConnectingWithAttemptTimerButWithoutResoluti
249236

250237
$builder = new HappyEyeBallsConnectionBuilder($loop, $connector, $resolver, $uri, $host, $parts);
251238

252-
$builder->connect();
253-
$deferred->reject(new \RuntimeException());
239+
$this->setExpectedException('RuntimeException', 'reject');
240+
241+
$promise = $builder->connect();
242+
$deferred->reject(new \RuntimeException('reject'));
254243
}
255244

256245
public function testConnectWillStartConnectingWithAttemptTimerWhenIpv6AndIpv4ResolvesAndWillStartNextConnectionAttemptWithoutAttemptTimerImmediatelyWhenFirstConnectionAttemptFails()
@@ -294,8 +283,8 @@ public function testConnectWillStartConnectingWithAlternatingIPv6AndIPv4WhenReso
294283
{
295284
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
296285
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
297-
$loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer);
298-
$loop->expects($this->once())->method('cancelTimer')->with($timer);
286+
$loop->expects($this->exactly(3))->method('addTimer')->with(0.1, $this->anything())->willReturn($timer);
287+
$loop->expects($this->exactly(3))->method('cancelTimer')->with($timer);
299288

300289
$deferred = new Deferred();
301290
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
@@ -326,17 +315,17 @@ public function testConnectWillStartConnectingWithAlternatingIPv6AndIPv4WhenReso
326315

327316
$builder = new HappyEyeBallsConnectionBuilder($loop, $connector, $resolver, $uri, $host, $parts);
328317

329-
$builder->connect();
318+
$promise = $builder->connect();
330319

331-
$deferred->reject(new \RuntimeException());
320+
$deferred->reject(new \RuntimeException('reject'));
332321
}
333322

334323
public function testConnectWillStartConnectingWithAttemptTimerWhenOnlyIpv6ResolvesAndWillStartNextConnectionAttemptWithoutAttemptTimerImmediatelyWhenFirstConnectionAttemptFails()
335324
{
336325
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
337326
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
338-
$loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer);
339-
$loop->expects($this->once())->method('cancelTimer')->with($timer);
327+
$loop->expects($this->exactly(2))->method('addTimer')->with(0.1, $this->anything())->willReturn($timer);
328+
$loop->expects($this->exactly(2))->method('cancelTimer')->with($timer);
340329

341330
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
342331
$connector->expects($this->exactly(2))->method('connect')->withConsecutive(
@@ -376,7 +365,7 @@ public function testConnectWillStartConnectingAndWillStartNextConnectionWithoutN
376365
$loop->expects($this->never())->method('cancelTimer');
377366

378367
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
379-
$connector->expects($this->exactly(2))->method('connect')->willReturn(new Promise(function () { }));
368+
$connector->expects($this->once())->method('connect')->willReturn(new Promise(function () { }));
380369

381370
$deferred = new Deferred();
382371
$resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
@@ -394,8 +383,10 @@ public function testConnectWillStartConnectingAndWillStartNextConnectionWithoutN
394383

395384
$builder = new HappyEyeBallsConnectionBuilder($loop, $connector, $resolver, $uri, $host, $parts);
396385

386+
$this->setExpectedException('RuntimeException', 'reject');
387+
397388
$builder->connect();
398-
$deferred->reject(new \RuntimeException());
389+
$deferred->reject(new \RuntimeException('reject'));
399390

400391
$this->assertNotNull($timer);
401392
$timer();
@@ -467,16 +458,10 @@ public function testConnectWillRejectWhenOnlyTcp6ConnectionRejectsAndCancelNextA
467458

468459
$builder = new HappyEyeBallsConnectionBuilder($loop, $connector, $resolver, $uri, $host, $parts);
469460

461+
// $this->setExpectedException('RuntimeException', 'Connection to tcp://reactphp.org:80 failed: Last error for IPv6: Connection refused. Previous error for IPv4: DNS failed');
462+
470463
$promise = $builder->connect();
471464
$deferred->reject(new \RuntimeException('Connection refused'));
472-
473-
$exception = null;
474-
$promise->then(null, function ($e) use (&$exception) {
475-
$exception = $e;
476-
});
477-
478-
$this->assertInstanceOf('RuntimeException', $exception);
479-
$this->assertEquals('Connection to tcp://reactphp.org:80 failed: Last error for IPv6: Connection refused. Previous error for IPv4: DNS failed', $exception->getMessage());
480465
}
481466

482467
public function testConnectWillRejectWhenOnlyTcp4ConnectionRejectsAndWillNeverStartNextAttemptTimer()

0 commit comments

Comments
 (0)