From 1889b0e12b456fda1083dfcfae379831c5bf0451 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Wed, 14 Oct 2020 13:12:10 +0200 Subject: [PATCH 1/2] Update PHPUnit configuration schema for PHPUnit 9.3 --- .gitattributes | 1 + .travis.yml | 9 ++++----- composer.json | 2 +- phpunit.xml.dist | 15 ++++++++++----- phpunit.xml.legacy | 18 ++++++++++++++++++ tests/AbstractTestCase.php | 22 ++++++++++++++++------ 6 files changed, 50 insertions(+), 17 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 0a5430e..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 @@ -19,10 +19,9 @@ matrix: allow_failures: - php: hhvm-3.18 -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 diff --git a/composer.json b/composer.json index 0a3ce33..e34a14c 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "ringcentral/psr7": "^1.2" }, "require-dev": { - "phpunit/phpunit": "^9.0 || ^7.0 || ^5.0 || ^4.8", + "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8", "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", "react/http": "^1.0", "clue/block-react": "^1.1" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ecb0fad..dbe6515 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,19 @@ - + + ./tests/ - - + + ./src/ - - + + diff --git a/phpunit.xml.legacy b/phpunit.xml.legacy new file mode 100644 index 0000000..c21d571 --- /dev/null +++ b/phpunit.xml.legacy @@ -0,0 +1,18 @@ + + + + + + + ./tests/ + + + + + ./src/ + + + diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index f6102fd..85eb676 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -52,19 +52,29 @@ protected function expectCallableOnceWithException($class, $message, $code) */ 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(); + } } - public function setExpectedException($exception, $message = '', $code = 0) + public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null) { if (method_exists($this, 'expectException')) { + // PHPUnit 5.2+ $this->expectException($exception); - if ($message !== '') { - $this->expectExceptionMessage($message); + if ($exceptionMessage !== '') { + $this->expectExceptionMessage($exceptionMessage); + } + if ($exceptionCode !== null) { + $this->expectExceptionCode($exceptionCode); } - $this->expectExceptionCode($code); } else { - parent::setExpectedException($exception, $message, $code); + // legacy PHPUnit 4 - PHPUnit 5.1 + parent::setExpectedException($exception, $exceptionMessage, $exceptionCode); } } From afc2f40b49f2a13cb7dcfe41ec83753d3a9eff58 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Wed, 14 Oct 2020 13:15:29 +0200 Subject: [PATCH 2/2] Use Number Codes if Error Constants are undefined --- tests/FunctionalTest.php | 21 ++++----------------- tests/ProxyConnectorTest.php | 14 +++++++------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 4cf1101..fc9aebe 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -40,7 +40,7 @@ public function testNonListeningSocketRejectsConnection() $this->setExpectedException( 'RuntimeException', 'Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', - SOCKET_ECONNREFUSED + defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); Block\await($promise, $this->loop, 3.0); } @@ -54,7 +54,7 @@ public function testPlainGoogleDoesNotAcceptConnectMethod() $this->setExpectedException( 'RuntimeException', 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)', - SOCKET_ECONNREFUSED + defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); Block\await($promise, $this->loop, 3.0); } @@ -73,7 +73,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod() $this->setExpectedException( 'RuntimeException', 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)', - SOCKET_ECONNREFUSED + defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); Block\await($promise, $this->loop, 3.0); } @@ -87,7 +87,7 @@ public function testSecureGoogleDoesNotAcceptPlainStream() $this->setExpectedException( 'RuntimeException', 'Connection to tcp://google.com:80 failed because connection to proxy was lost while waiting for response (ECONNRESET)', - SOCKET_ECONNRESET + defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104 ); Block\await($promise, $this->loop, 3.0); } @@ -108,17 +108,4 @@ public function testCancelWhileConnectingShouldNotCreateGarbageCycles() $this->assertEquals(0, gc_collect_cycles()); } - - public function setExpectedException($exception, $message = '', $code = 0) - { - if (method_exists($this, 'expectException')) { - $this->expectException($exception); - if ($message !== null) { - $this->expectExceptionMessage($message); - } - $this->expectExceptionCode($code); - } else { - parent::setExpectedException($exception, $message, $code); - } - } } diff --git a/tests/ProxyConnectorTest.php b/tests/ProxyConnectorTest.php index 1ae2f09..654699a 100644 --- a/tests/ProxyConnectorTest.php +++ b/tests/ProxyConnectorTest.php @@ -294,7 +294,7 @@ public function testRejectsWithPreviousIfConnectorRejects() $promise->then(null, $this->expectCallableOnceWithException( 'RuntimeException', 'Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', - SOCKET_ECONNREFUSED + defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 )); $promise->then(null, $this->expectCallableOnceWith($this->callback(function (\Exception $e) use ($previous) { @@ -319,7 +319,7 @@ public function testRejectsAndClosesIfStreamWritesNonHttp() $promise->then(null, $this->expectCallableOnceWithException( 'RuntimeException', 'Connection to tcp://google.com:80 failed because proxy returned invalid response (EBADMSG)', - SOCKET_EBADMSG + defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG: 71 )); } @@ -340,7 +340,7 @@ public function testRejectsAndClosesIfStreamWritesTooMuchData() $promise->then(null, $this->expectCallableOnceWithException( 'RuntimeException', 'Connection to tcp://google.com:80 failed because proxy response headers exceed maximum of 8 KiB (EMSGSIZE)', - SOCKET_EMSGSIZE + defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 90 )); } @@ -361,7 +361,7 @@ public function testRejectsAndClosesIfStreamReturnsProyAuthenticationRequired() $promise->then(null, $this->expectCallableOnceWithException( 'RuntimeException', 'Connection to tcp://google.com:80 failed because proxy denied access with HTTP error code 407 (Proxy Authentication Required) (EACCES)', - SOCKET_EACCES + defined('SOCKET_EACCES') ? SOCKET_EACCES : 13 )); } @@ -382,7 +382,7 @@ public function testRejectsAndClosesIfStreamReturnsNonSuccess() $promise->then(null, $this->expectCallableOnceWithException( 'RuntimeException', 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 403 (Not allowed) (ECONNREFUSED)', - SOCKET_ECONNREFUSED + defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 )); } @@ -402,7 +402,7 @@ public function testRejectsWithPreviousExceptionIfStreamEmitsError() $promise->then(null, $this->expectCallableOnceWithException( 'RuntimeException', 'Connection to tcp://google.com:80 failed because connection to proxy caused a stream error (EIO)', - SOCKET_EIO + defined('SOCKET_EIO') ? SOCKET_EIO : 5 )); $promise->then(null, $this->expectCallableOnceWith($this->callback(function (\Exception $e) use ($previous) { @@ -471,7 +471,7 @@ public function testCancelPromiseWhileConnectionIsReadyWillCloseOpenConnectionAn $promise->then(null, $this->expectCallableOnceWithException( 'RuntimeException', 'Connection to tcp://google.com:80 cancelled while waiting for proxy (ECONNABORTED)', - SOCKET_ECONNABORTED + defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103 )); }