8000 Merge pull request #243 from SimonFrings/tests · reactphp/socket@ce608c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce608c8

Browse files
authored
Merge pull request #243 from SimonFrings/tests
Update PHPUnit configuration schema for PHPUnit 9.3 and replace deprecated at() Mocks
2 parents acdcab8 + 7564640 commit ce608c8

7 files changed

+103
-52
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/.travis.yml export-ignore
44
/examples export-ignore
55
/phpunit.xml.dist export-ignore
6+
/phpunit.xml.legacy export-ignore
67
/tests export-ignore

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: php
33
# lock distro so new future defaults will not break the build
44
dist: trusty
55

6-
matrix:
6+
jobs:
77
include:
88
- php: 5.3
99
dist: precise
@@ -38,10 +38,9 @@ matrix:
3838
- os: osx
3939
- os: windows
4040

41-
sudo: false
42-
4341
install:
44-
- composer install --no-interaction
42+
- composer install
4543

4644
script:
47-
- vendor/bin/phpunit --coverage-text
45+
- if [[ "$TRAVIS_PHP_VERSION" > "7.2" ]]; then vendor/bin/phpunit --coverage-text; fi
46+
- if [[ "$TRAVIS_PHP_VERSION" < "7.3" ]]; then vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy; fi

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"require-dev": {
1616
"clue/block-react": "^1.2",
17-
"phpunit/phpunit": "^9.0 || ^5.7 || ^4.8.35",
17+
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
1818
"react/promise-stream": "^1.2"
1919
},
2020
"autoload": {

phpunit.xml.dist

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit bootstrap="vendor/autoload.php" colors="true">
3+
<!-- PHPUnit configuration file with new format for PHPUnit 9.3+ -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
6+
bootstrap="vendor/autoload.php"
7+
colors="true"
8+
cacheResult="false">
49
<testsuites>
510
<testsuite name="React Test Suite">
611
<directory>./tests/</directory>
712
</testsuite>
813
</testsuites>
9-
10-
<filter>
11-
<whitelist>
14+
<coverage>
15+
<include>
1216
<directory>./src/</directory>
13-
</whitelist>
14-
</filter>
17+
</include>
18+
</coverage>
1519
</phpunit>

phpunit.xml.legacy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- PHPUnit configuration file with old format for PHPUnit 9.2 or older -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
6+
bootstrap="vendor/autoload.php"
7+
colors="true">
8+
<testsuites>
9+
<testsuite name="React Test Suite">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
<filter>
14+
<whitelist>
15+
<directory>./src/</directory>
16+
</whitelist>
17+
</filter>
18+
</phpunit>

tests/HappyEyeBallsConnectionBuilderTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,47 @@ public function testConnectWillStartConnectingWithAttemptTimerWhenIpv6AndIpv4Res
290290
$deferred->reject(new \RuntimeException());
291291
}
292292

293+
public function testConnectWillStartConnectingWithAlternatingIPv6AndIPv4WhenResolverReturnsMultipleIPAdresses()
294+
{
295+
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
296+
$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);
299+
300+
$deferred = new Deferred();
301+
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
302+
$connector->expects($this->exactly(4))->method('connect')->withConsecutive(
303+
array('tcp://[::1]:80?hostname=reactphp.org'),
304+
array('tcp://127.0.0.1:80?hostname=reactphp.org'),
305+
array('tcp://[::2]:80?hostname=reactphp.org'),
306+
array('tcp://127.0.0.2:80?hostname=reactphp.org')
307+
)->willReturnOnConsecutiveCalls(
308+
$deferred->promise(),
309+
$deferred->promise(),
310+
$deferred->promise(),
311+
new Promise(function () { })
312+
);
313+
314+
$resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
315+
$resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
316+
array('reactphp.org', Message::TYPE_AAAA),
317+
array('reactphp.org', Message::TYPE_A)
318+
)->willReturnOnConsecutiveCalls(
319+
\React\Promise\resolve(array('::1', '::2')),
320+
\React\Promise\resolve(array('127.0.0.1', '127.0.0.2'))
321+
);
322+
323+
$uri = 'tcp://reactphp.org:80';
324+
$host = 'reactphp.org';
325+
$parts = parse_url($uri);
326+
327+
$builder = new HappyEyeBallsConnectionBuilder($loop, $connector, $resolver, $uri, $host, $parts);
328+
329+
$builder->connect();
330+
331+
$deferred->reject(new \RuntimeException());
332+
}
333+
293334
public function testConnectWillStartConnectingWithAttemptTimerWhenOnlyIpv6ResolvesAndWillStartNextConnectionAttemptWithoutAttemptTimerImmediatelyWhenFirstConnectionAttemptFails()
294335
{
295336
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();

tests/HappyEyeBallsConnectorTest.php

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,13 @@ public function testIpv6ResolvesFirstSoIsTheFirstToConnect(array $ipv6, array $i
148148
{
149149
$deferred = new Deferred();
150150

151-
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(Promise\resolve($ipv6)));
152-
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue($deferred->promise()));
151+
$this->resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
152+
array('google.com', Message::TYPE_AAAA),
153+
array('google.com', Message::TYPE_A)
154+
)->willReturnOnConsecutiveCalls(
155+
$this->returnValue(Promise\resolve($ipv6)),
156+
$this->returnValue($deferred->promise())
157+
);
153158
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(']:80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
154159

155160
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
@@ -168,8 +173,13 @@ public function testIpv6DoesntResolvesWhileIpv4DoesFirstSoIpv4Connects(array $ip
168173
{
169174
$deferred = new Deferred();
170175

171-
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue($deferred->promise()));
172-
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(Promise\resolve($ipv4)));
176+
$this->resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
177+
array('google.com', Message::TYPE_AAAA),
178+
array('google.com', Message::TYPE_A)
179+
)->willReturnOnConsecutiveCalls(
180+
$this->returnValue($deferred->promise()),
181+
$this->returnValue(Promise\resolve($ipv4))
182+
);
173183
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
174184

175185
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
@@ -181,38 +191,6 @@ public function testIpv6DoesntResolvesWhileIpv4DoesFirstSoIpv4Connects(array $ip
181191
$this->loop->run();
182192
}
183193

184-
/**
185-
* @dataProvider provideIpvAddresses
186-
*/
187-
public function testAttemptsToConnectBothIpv6AndIpv4AddressesAlternatingIpv6AndIpv4AddressesWhenMoreThenOneIsResolvedPerFamily(array $ipv6, array $ipv4)
188-
{
189-
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(
190-
Promise\Timer\resolve(0.1, $this->loop)->then(function () use ($ipv6) {
191-
return Promise\resolve($ipv6);
192-
})
193-
));
194-
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(
195-
Promise\Timer\resolve(0.1, $this->loop)->then(function () use ($ipv4) {
196-
return Promise\resolve($ipv4);
197-
})
198-
));
199-
200-
$i = 0;
201-
while (count($ipv6) > 0 || count($ipv4) > 0) {
202-
if (count($ipv6) > 0) {
203-
$this->tcp->expects($this->at($i++))->method('connect')->with($this->equalTo('scheme://[' . array_shift($ipv6) . ']:80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
204-
}
205-
if (count($ipv4) > 0) {
206-
$this->tcp->expects($this->at($i++))->method('connect')->with($this->equalTo('scheme://' . array_shift($ipv4) . ':80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
207-
}
208-
}
209-
210-
211-
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
212-
213-
$this->loop->run();
214-
}
215-
216194
public function testRejectsImmediatelyIfUriIsInvalid()
217195
{
218196
$this->resolver->expects($this->never())->method('resolveAll');
@@ -297,8 +275,13 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp()
297275
*/
298276
public function testShouldConnectOverIpv4WhenIpv6LookupFails(array $ipv6, array $ipv4)
299277
{
300-
$this->resolver->expects($this->at(0))->method('resolveAll')->with($this->equalTo('example.com'), Message::TYPE_AAAA)->willReturn(Promise\reject(new \Exception('failure')));
301-
$this->resolver->expects($this->at(1))->method('resolveAll')->with($this->equalTo('example.com'), Message::TYPE_A)->willReturn(Promise\resolve($ipv4));
278+
$this->resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
279+
array($this->equalTo('example.com'), Message::TYPE_AAAA),
280+
array($this->equalTo('example.com'), Message::TYPE_A)
281+
)->willReturnOnConsecutiveCalls(
282+
Promise\reject(new \Exception('failure')),
283+
Promise\resolve($ipv4)
284+
);
302285
$this->tcp->expects($this->exactly(1))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn(Promise\resolve($this->connection));
303286

304287
$promise = $this->connector->connect('example.com:80');;
@@ -316,8 +299,13 @@ public function testShouldConnectOverIpv6WhenIpv4LookupFails(array $ipv6, array
316299
$ipv6[] = '1:2:3:4';
317300
}
318301

319-
$this->resolver->expects($this->at(0))->method('resolveAll')->with($this->equalTo('example.com'), Message::TYPE_AAAA)->willReturn(Promise\resolve($ipv6));
320-
$this->resolver->expects($this->at(1))->method('resolveAll')->with($this->equalTo('example.com'), Message::TYPE_A)->willReturn(Promise\reject(new \Exception('failure')));
302+
$this->resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
303+
array($this->equalTo('example.com'), Message::TYPE_AAAA),
304+
array($this->equalTo('example.com'), Message::TYPE_A)
305+
)->willReturnOnConsecutiveCalls(
306+
Promise\resolve($ipv6),
307+
Promise\reject(new \Exception('failure'))
308+
);
321309
$this->tcp->expects($this->exactly(1))->method('connect')->with($this->equalTo('[1:2:3:4]:80?hostname=example.com'))->willReturn(Promise\resolve($this->connection));
322310

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

0 commit comments

Comments
 (0)
0