8000 Update PHPUnit configuration schema for PHPUnit 9.3 and replace deprecated at() Mocks by SimonFrings · Pull Request #243 · reactphp/socket · GitHub
[go: up one dir, main page]

Skip to content
Dismiss alert

Update PHPUnit configuration schema for PHPUnit 9.3 and replace deprecated at() Mocks #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/.travis.yml export-ignore
/examples export-ignore
/phpunit.xml.dist export-ignore
/phpunit.xml.legacy export-ignore
/tests export-ignore
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ language: php
# lock distro so new future defaults will not break the build
dist: trusty

matrix:
jobs:
include:
- php: 5.3
dist: precise
Expand Down Expand Up @@ -38,10 +38,9 @@ matrix:
- os: osx
- os: windows

sudo: false

install:
- composer install --no-interaction
- composer install

script:
- vendor/bin/phpunit --coverage-text
- if [[ "$TRAVIS_PHP_VERSION" > "7.2" ]]; then vendor/bin/phpunit --coverage-text; fi
- if [[ "$TRAVIS_PHP_VERSION" < "7.3" ]]; then vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy; fi
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"require-dev": {
"clue/block-react": "^1.2",
"phpunit/phpunit": "^9.0 || ^5.7 || ^4.8.35",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/promise-stream": "^1.2"
},
"autoload": {
Expand Down
16 changes: 10 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<!-- PHPUnit configuration file with new format for PHPUnit 9.3+ -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
cacheResult="false">
<testsuites>
<testsuite name="React Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<coverage>
<include>
<directory>./src/</directory>
</whitelist>
</filter>
</include>
</coverage>
</phpunit>
18 changes: 18 additions & 0 deletions phpunit.xml.legacy
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- PHPUnit configuration file with old format for PHPUnit 9.2 or older -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="React Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
41 changes: 41 additions & 0 deletions tests/HappyEyeBallsConnectionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,47 @@ public function testConnectWillStartConnectingWithAttemptTimerWhenIpv6AndIpv4Res
$deferred->reject(new \RuntimeException());
}

public function testConnectWillStartConnectingWithAlternatingIPv6AndIPv4WhenResolverReturnsMultipleIPAdresses()
{
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer);
$loop->expects($this->once())->method('cancelTimer')->with($timer);

$deferred = new Deferred();
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$connector->expects($this->exactly(4))->method('connect')->withConsecutive(
array('tcp://[::1]:80?hostname=reactphp.org'),
array('tcp://127.0.0.1:80?hostname=reactphp.org'),
array('tcp://[::2]:80?hostname=reactphp.org'),
array('tcp://127.0.0.2:80?hostname=reactphp.org')
)->willReturnOnConsecutiveCalls(
$deferred->promise(),
$deferred->promise(),
$deferred->promise(),
new Promise(function () { })
);

$resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
$resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
array('reactphp.org', Message::TYPE_AAAA),
array('reactphp.org', Message::TYPE_A)
)->willReturnOnConsecutiveCalls(
\React\Promise\resolve(array('::1', '::2')),
\React\Promise\resolve(array('127.0.0.1', '127.0.0.2'))
);

$uri = 'tcp://reactphp.org:80';
$host = 'reactphp.org';
$parts = parse_url($uri);

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

$builder->connect();

$deferred->reject(new \RuntimeException());
}

public function testConnectWillStartConnectingWithAttemptTimerWhenOnlyIpv6ResolvesAndWillStartNextConnectionAttemptWithoutAttemptTimerImmediatelyWhenFirstConnectionAttemptFails()
{
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
Expand Down
68 changes: 28 additions & 40 deletions tests/HappyEyeBallsConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ public function testIpv6ResolvesFirstSoIsTheFirstToConnect(array $ipv6, array $i
{
$deferred = new Deferred();

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

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

$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue($deferred->promise()));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(Promise\resolve($ipv4)));
$this->resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
array('google.com', Message::TYPE_AAAA),
array('google.com', Message::TYPE_A)
)->willReturnOnConsecutiveCalls(
$this->returnValue($deferred->promise()),
$this->returnValue(Promise\resolve($ipv4))
);
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));

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

/**
* @dataProvider provideIpvAddresses
*/
public function testAttemptsToConnectBothIpv6AndIpv4AddressesAlternatingIpv6AndIpv4AddressesWhenMoreThenOneIsResolvedPerFamily(array $ipv6, array $ipv4)
{
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(
Promise\Timer\resolve(0.1, $this->loop)->then(function () use ($ipv6) {
return Promise\resolve($ipv6);
})
));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(
Promise\Timer\resolve(0.1, $this->loop)->then(function () use ($ipv4) {
return Promise\resolve($ipv4);
})
));

$i = 0;
while (count($ipv6) > 0 || count($ipv4) > 0) {
if (count($ipv6) > 0) {
$this->tcp->expects($this->at($i++))->method('connect')->with($this->equalTo('scheme://[' . array_shift($ipv6) . ']:80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
}
if (count($ipv4) > 0) {
$this->tcp->expects($this->at($i++))->method('connect')->with($this->equalTo('scheme://' . array_shift($ipv4) . ':80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
}
}


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

$this->loop->run();
}

public function testRejectsImmediatelyIfUriIsInvalid()
{
$this->resolver->expects($this->never())->method('resolveAll');
Expand Down Expand Up @@ -297,8 +275,13 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp()
*/
public function testShouldConnectOverIpv4WhenIpv6LookupFails(array $ipv6, array $ipv4)
{
$this->resolver->expects($this->at(0))->method('resolveAll')->with($this->equalTo('example.com'), Message::TYPE_AAAA)->willReturn(Promise\reject(new \Exception('failure')));
$this->resolver->expects($this->at(1))->method('resolveAll')->with($this->equalTo('example.com'), Message::TYPE_A)->willReturn(Promise\resolve($ipv4));
$this->resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
array($this->equalTo('example.com'), Message::TYPE_AAAA),
array($this->equalTo('example.com'), Message::TYPE_A)
)->willReturnOnConsecutiveCalls(
Promise\reject(new \Exception('failure')),
Promise\resolve($ipv4)
);
$this->tcp->expects($this->exactly(1))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn(Promise\resolve($this->connection));

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

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

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