Skip to content
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

Run tests on PHPUnit 9 and update PHPUnit configuration schema for PHPUnit 9.3 #37

Merged
merged 2 commits into from
Jan 26, 2021
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
30 changes: 14 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
language: php

php:
# - 5.3 # requires old distro, see below
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- hhvm # ignore errors, see below

# lock distro so new future defaults will not break the build
dist: trusty

matrix:
jobs:
include:
- php: 5.3
dist: precise
- php: 5.4
- php: 5.5
- php: 5.6
- php: 7.0
- php: 7.1
- php: 7.2
- php: 7.3
- php: 7.4
- php: hhvm-3.18
allow_failures:
- php: hhvm

sudo: false
- php: hhvm-3.18

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 @@ -45,6 +45,6 @@
},
"require-dev": {
"clue/block-react": "~1.0",
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.0 || ^4.8.35"
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
}
}
22 changes: 9 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
<!-- 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="Datagram Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<coverage>
<include>
<directory>./src/</directory>
</whitelist>
</filter>
</include>
</coverage>
</phpunit>
25 changes: 25 additions & 0 deletions phpunit.xml.legacy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Datagram Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
33 changes: 12 additions & 21 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class FactoryTest extends TestCase
private $resolver;
private $factory;

public function setUp()
/**
* @before
*/
public function setUpFactory()
{
$this->loop = \React\EventLoop\Factory::create();
$this->resolver = $this->createResolverMock();
Expand All @@ -31,7 +34,7 @@ public function testCreateClient()

$this->assertEquals('127.0.0.1:12345', $capturedClient->getRemoteAddress());

$this->assertContains('127.0.0.1:', $capturedClient->getLocalAddress());
$this->assertContainsString('127.0.0.1:', $capturedClient->getLocalAddress());
$this->assertNotEquals('127.0.0.1:12345', $capturedClient->getLocalAddress());

$capturedClient->close();
Expand All @@ -50,7 +53,7 @@ public function testCreateClientLocalhost()

$this->assertEquals('127.0.0.1:12345', $capturedClient->getRemoteAddress());

$this->assertContains('127.0.0.1:', $capturedClient->getLocalAddress());
$this->assertContainsString('127.0.0.1:', $capturedClient->getLocalAddress());
$this->assertNotEquals('127.0.0.1:12345', $capturedClient->getLocalAddress());

$capturedClient->close();
Expand Down Expand Up @@ -83,7 +86,7 @@ public function testCreateClientIpv6()

$this->assertEquals('[::1]:12345', $capturedClient->getRemoteAddress());

$this->assertContains('[::1]:', $capturedClient->getLocalAddress());
$this->assertContainsString('[::1]:', $capturedClient->getLocalAddress());
$this->assertNotEquals('[::1]:12345', $capturedClient->getLocalAddress());

$capturedClient->close();
Expand Down Expand Up @@ -133,37 +136,26 @@ public function testCreateClientWithHostnameWillUseResolver()
$client->close();
}

/**
* @expectedException RuntimeException
*/
public function testCreateClientWithHostnameWillRejectIfResolverRejects()
{
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\reject(new \RuntimeException('test')));

$this->setExpectedException('RuntimeException');
Block\await($this->factory->createClient('example.com:0'), $this->loop);
}

/**
* @expectedException Exception
* @expectedExceptionMessage Unable to create client socket
*/
public function testCreateClientWithInvalidHostnameWillReject()
{
$this->setExpectedException('Exception', 'Unable to create client socket');
Block\await($this->factory->createClient('/////'), $this->loop);
}

/**
* @expectedException Exception
* @expectedExceptionMessage Unable to create server socket
*/
public function testCreateServerWithInvalidHostnameWillReject()
{
$this->setExpectedException('Exception', 'Unable to create server socket');
Block\await($this->factory->createServer('/////'), $this->loop);
}

/**
* @expectedException RuntimeException
*/
public function testCancelCreateClientWithCancellableHostnameResolver()
{
$promise = new Promise\Promise(function () { }, $this->expectCallableOnce());
Expand All @@ -172,12 +164,10 @@ public function testCancelCreateClientWithCancellableHostnameResolver()
$promise = $this->factory->createClient('example.com:0');
$promise->cancel();

$this->setExpectedException('RuntimeException');
Block\await($promise, $this->loop);
}

/**
* @expectedException RuntimeException
*/
public function testCancelCreateClientWithUncancellableHostnameResolver()
{
$promise = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
Expand All @@ -186,6 +176,7 @@ public function testCancelCreateClientWithUncancellableHostnameResolver()
$promise = $this->factory->createClient('example.com:0');
$promise->cancel();

$this->setExpectedException('RuntimeException');
Block\await($promise, $this->loop);
}
}
5 changes: 4 additions & 1 deletion tests/SocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class SocketTest extends TestCase
private $loop;
private $factory;

public function setUp()
/**
* @before
*/
public function setUpFactory()
{
$this->loop = \React\EventLoop\Factory::create();
$this->factory = new \React\Datagram\Factory($this->loop, $this->createResolverMock());
Expand Down
36 changes: 35 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,45 @@ protected function expectCallableNever()

protected function createCallableMock()
{
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {
// PHPUnit 8.5+
return $this->getMockBuilder('stdClass')->addMethods(array('__invoke'))->getMock();
} else {
// legacy PHPUnit 4 - PHPUnit 8.4
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
}
}

protected function createResolverMock()
{
return $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
}

public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
{
if (method_exists($this, 'expectException')) {
// PHPUnit 5.2+
$this->expectException($exception);
if ($exceptionMessage !== '') {
$this->expectExceptionMessage($exceptionMessage);
}
if ($exceptionCode !== null) {
$this->expectExceptionCode($exceptionCode);
}
} else {
// legacy PHPUnit 4 - PHPUnit 5.1
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
}
}

public function assertContainsString($needle, $haystack)
{
if (method_exists($this, 'assertStringContainsString')) {
// PHPUnit 7.5+
$this->assertStringContainsString($needle, $haystack);
} else {
// legacy PHPUnit 4 - PHPUnit 7.5
$this->assertContains($needle, $haystack);
}
}
}