From dada1237823e4b5e61f36794c381b67617e8b258 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Mon, 25 Jan 2021 12:36:15 +0100 Subject: [PATCH 1/2] Run tests on PHPUnit 9 --- .travis.yml | 27 ++++++++++++--------------- composer.json | 2 +- tests/FactoryTest.php | 33 ++++++++++++--------------------- tests/SocketTest.php | 5 ++++- tests/TestCase.php | 36 +++++++++++++++++++++++++++++++++++- 5 files changed, 64 insertions(+), 39 deletions(-) diff --git a/.travis.yml b/.travis.yml index 41921e3..e1421bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,29 +1,26 @@ 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 diff --git a/composer.json b/composer.json index 2789048..057a85b 100644 --- a/composer.json +++ b/composer.json @@ -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" } } diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 508c212..ab3d7f7 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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()); @@ -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(); @@ -186,6 +176,7 @@ public function testCancelCreateClientWithUncancellableHostnameResolver() $promise = $this->factory->createClient('example.com:0'); $promise->cancel(); + $this->setExpectedException('RuntimeException'); Block\await($promise, $this->loop); } } diff --git a/tests/SocketTest.php b/tests/SocketTest.php index f938188..479b3ec 100644 --- a/tests/SocketTest.php +++ b/tests/SocketTest.php @@ -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()); diff --git a/tests/TestCase.php b/tests/TestCase.php index 15820e2..cade24a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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); + } + } } From e06cf84d6dd2d32bb99cfac598fdc8e5ac1ac882 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Mon, 25 Jan 2021 12:45:17 +0100 Subject: [PATCH 2/2] Update PHPUnit configuration schema for PHPUnit 9.3 --- .gitattributes | 1 + .travis.yml | 3 ++- phpunit.xml.dist | 22 +++++++++------------- phpunit.xml.legacy | 25 +++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 phpunit.xml.legacy diff --git a/.gitattributes b/.gitattributes index f2f51dd..64ab6e0 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,4 +3,5 @@ /.travis.yml export-ignore /examples export-ignore /phpunit.xml.dist export-ignore +/phpunit.xml.legacy export-ignore /tests export-ignore diff --git a/.travis.yml b/.travis.yml index e1421bc..5623330 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,4 +23,5 @@ install: - 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 diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f2c3ea9..b6b06a4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,23 +1,19 @@ - + + colors="true" + cacheResult="false"> ./tests/ - - + + ./src/ - - + + diff --git a/phpunit.xml.legacy b/phpunit.xml.legacy new file mode 100644 index 0000000..cfaa0ad --- /dev/null +++ b/phpunit.xml.legacy @@ -0,0 +1,25 @@ + + + + + + + ./tests/ + + + + + ./src/ + + +