From d44bf2e57373833c329b8b2b0ed7091b3c7075de Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Thu, 20 Aug 2020 13:02:18 +0200 Subject: [PATCH 1/2] Run tests on PHPUnit 9 --- composer.json | 2 +- phpunit.xml.dist | 4 +++- tests/FunctionAwaitAllTest.php | 14 +++----------- tests/FunctionAwaitAnyTest.php | 8 ++------ tests/FunctionAwaitTest.php | 19 ++++--------------- tests/FunctionSleepTest.php | 4 ++-- tests/TestCase.php | 33 ++++++++++++++++++++++++++++++++- 7 files changed, 47 insertions(+), 37 deletions(-) diff --git a/composer.json b/composer.json index 50372f9..a24bb89 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,6 @@ "react/promise-timer": "^1.5" }, "require-dev": { - "phpunit/phpunit": "^6.4 || ^5.7 || ^4.8.35" + "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 40b9570..6ed22b2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,8 @@ - + ./tests/ diff --git a/tests/FunctionAwaitAllTest.php b/tests/FunctionAwaitAllTest.php index 24ef873..9532657 100644 --- a/tests/FunctionAwaitAllTest.php +++ b/tests/FunctionAwaitAllTest.php @@ -23,10 +23,6 @@ public function testAwaitAllAllResolved() $this->assertEquals(array('first' => 1, 'second' => 2), Block\awaitAll($all, $this->loop)); } - /** - * @expectedException Exception - * @expectedExceptionMessage test - */ public function testAwaitAllRejected() { $all = array( @@ -34,12 +30,10 @@ public function testAwaitAllRejected() $this->createPromiseRejected(new \Exception('test')) ); + $this->setExpectedException('Exception', 'test'); Block\awaitAll($all, $this->loop); } - /** - * @expectedException UnexpectedValueException - */ public function testAwaitAllRejectedWithFalseWillWrapInUnexpectedValueException() { $all = array( @@ -47,13 +41,10 @@ public function testAwaitAllRejectedWithFalseWillWrapInUnexpectedValueException( Promise\reject(false) ); + $this->setExpectedException('UnexpectedValueException'); Block\awaitAll($all, $this->loop); } - /** - * @expectedException Exception - * @expectedExceptionMessage first - */ public function testAwaitAllOnlyRejected() { $all = array( @@ -61,6 +52,7 @@ public function testAwaitAllOnlyRejected() $this->createPromiseRejected(new \Exception('second')) ); + $this->setExpectedException('Exception', 'first'); Block\awaitAll($all, $this->loop); } diff --git a/tests/FunctionAwaitAnyTest.php b/tests/FunctionAwaitAnyTest.php index a9a0d35..faded88 100644 --- a/tests/FunctionAwaitAnyTest.php +++ b/tests/FunctionAwaitAnyTest.php @@ -9,11 +9,9 @@ class FunctionAwaitAnyTest extends TestCase { - /** - * @expectedException UnderflowException - */ public function testAwaitAnyEmpty() { + $this->setExpectedException('UnderflowException'); Block\awaitAny(array(), $this->loop); } @@ -49,9 +47,6 @@ public function testAwaitAnyFirstResolvedConcurrently() $this->assertEquals(2, Block\awaitAny($all, $this->loop)); } - /** - * @expectedException UnderflowException - */ public function testAwaitAnyAllRejected() { $all = array( @@ -59,6 +54,7 @@ public function testAwaitAnyAllRejected() $this->createPromiseRejected(2) ); + $this->setExpectedException('UnderflowException'); Block\awaitAny($all, $this->loop); } diff --git a/tests/FunctionAwaitTest.php b/tests/FunctionAwaitTest.php index d9d40f8..d50736d 100644 --- a/tests/FunctionAwaitTest.php +++ b/tests/FunctionAwaitTest.php @@ -8,36 +8,27 @@ class FunctionAwaitTest extends TestCase { - /** - * @expectedException Exception - * @expectedExceptionMessage test - */ public function testAwaitOneRejected() { $promise = $this->createPromiseRejected(new \Exception('test')); + $this->setExpectedException('Exception', 'test'); Block\await($promise, $this->loop); } - /** - * @expectedException UnexpectedValueException - * @expectedExceptionMessage Promise rejected with unexpected value of type bool - */ public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException() { $promise = Promise\reject(false); + $this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool'); Block\await($promise, $this->loop); } - /** - * @expectedException UnexpectedValueException - * @expectedExceptionMessage Promise rejected with unexpected value of type NULL - */ public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException() { $promise = Promise\reject(null); + $this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL'); Block\await($promise, $this->loop); } @@ -73,13 +64,11 @@ public function testAwaitOneInterrupted() $this->assertEquals(2, Block\await($promise, $this->loop)); } - /** - * @expectedException React\Promise\Timer\TimeoutException - */ public function testAwaitOncePendingWillThrowOnTimeout() { $promise = new Promise\Promise(function () { }); + $this->setExpectedException('React\Promise\Timer\TimeoutException'); Block\await($promise, $this->loop, 0.001); } diff --git a/tests/FunctionSleepTest.php b/tests/FunctionSleepTest.php index 259f2ed..d3c69f0 100644 --- a/tests/FunctionSleepTest.php +++ b/tests/FunctionSleepTest.php @@ -12,7 +12,7 @@ public function testSleep() Block\sleep(0.2, $this->loop); $time = microtime(true) - $time; - $this->assertEquals(0.2, $time, '', 0.1); + $this->assertEqualsDelta(0.2, $time, 0.1); } public function testSleepSmallTimerWillBeCappedReasonably() @@ -21,6 +21,6 @@ public function testSleepSmallTimerWillBeCappedReasonably() Block\sleep(0.0000001, $this->loop); $time = microtime(true) - $time; - $this->assertEquals(0.1, $time, '', 0.1); + $this->assertEqualsDelta(0.1, $time, 0.1); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index bc36aab..6ef25bc 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,7 +9,10 @@ class TestCase extends BaseTestCase { protected $loop; - public function setUp() + /** + * @before + */ + public function setUpLoop() { $this->loop = \React\EventLoop\Factory::create(); } @@ -43,4 +46,32 @@ protected function createTimerInterrupt($delay = 0.01) $loop->stop(); }); } + + public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null) + { + if (method_exists($this, 'expectException')) { + // PHPUnit 5+ + $this->expectException($exception); + if ($exceptionMessage !== '') { + $this->expectExceptionMessage($exceptionMessage); + } + if ($exceptionCode !== null) { + $this->expectExceptionCode($exceptionCode); + } + } else { + // legacy PHPUnit 4 + parent::setExpectedException($exception, $exceptionMessage, $exceptionCode); + } + } + + public function assertEqualsDelta($expected, $actual, $delta) + { + if (method_exists($this, 'assertEqualsWithDelta')) { + // PHPUnit 7.5+ + $this->assertEqualsWithDelta($expected, $actual, $delta); + } else { + // legacy PHPUnit 4 - PHPUnit 7.5 + $this->assertEquals($expected, $actual, '', $delta); + } + } } From 7a03defdfb6268f6118e8422ebdcd5f58dbe97cc Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Thu, 20 Aug 2020 13:07:06 +0200 Subject: [PATCH 2/2] Update PHPUnit configuration schema for PHPUnit 9.3 --- .gitattributes | 1 + .travis.yml | 7 ++++--- phpunit.xml.dist | 13 ++++++++----- phpunit.xml.legacy | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 phpunit.xml.legacy diff --git a/.gitattributes b/.gitattributes index 0925d33..eccc763 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 3e74fb1..5623330 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 @@ -20,7 +20,8 @@ matrix: - 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 diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6ed22b2..7da7fcd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,9 @@ - + @@ -8,9 +11,9 @@ ./tests/ - - + + ./src/ - - + + diff --git a/phpunit.xml.legacy b/phpunit.xml.legacy new file mode 100644 index 0000000..feff95f --- /dev/null +++ b/phpunit.xml.legacy @@ -0,0 +1,18 @@ + + + + + + + ./tests/ + + + + + ./src/ + + +