From 367c2b0ec74e275d046f3516edee3fbe7431f2ff Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Mon, 27 Feb 2023 17:51:17 +0100 Subject: [PATCH] Add support for PHPUnit10 and remove support for PHPUnit8 * Normalize class import and class alias * Remove PHP7.2 from github workflows * Migrate phpunit.xml.dist configuration file * Using FQN for PSR interfaces --- .github/workflows/guzzle.yml | 2 +- .github/workflows/laminas-legacy.yml | 2 +- .github/workflows/nyholm.yml | 2 +- .github/workflows/ringcentral.yml | 2 +- .gitignore | 1 + composer.json | 4 +-- phpunit.xml.dist | 47 +++++++++++--------------- src/BaseTest.php | 28 ++++++++++------ src/MessageTrait.php | 9 ++--- src/RequestIntegrationTest.php | 2 +- src/ResponseIntegrationTest.php | 2 +- src/ServerRequestIntegrationTest.php | 4 +-- src/StreamIntegrationTest.php | 2 +- src/UploadedFileIntegrationTest.php | 9 ++--- src/UriIntegrationTest.php | 50 ++++++++++++++++------------ tests/Guzzle/RequestTest.php | 2 +- tests/Guzzle/ResponseTest.php | 2 +- 17 files changed, 82 insertions(+), 88 deletions(-) diff --git a/.github/workflows/guzzle.yml b/.github/workflows/guzzle.yml index 69a128a..15034e5 100644 --- a/.github/workflows/guzzle.yml +++ b/.github/workflows/guzzle.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ] + php: [ '7.3', '7.4', '8.0', '8.1', '8.2' ] uses: ./.github/workflows/integration.yml with: php: ${{ matrix.php }} diff --git a/.github/workflows/laminas-legacy.yml b/.github/workflows/laminas-legacy.yml index 3e75638..415c98f 100644 --- a/.github/workflows/laminas-legacy.yml +++ b/.github/workflows/laminas-legacy.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ '7.2', '7.3', '7.4' ] + php: [ '7.3', '7.4' ] uses: ./.github/workflows/integration.yml with: php: ${{ matrix.php }} diff --git a/.github/workflows/nyholm.yml b/.github/workflows/nyholm.yml index 199d7be..477263c 100644 --- a/.github/workflows/nyholm.yml +++ b/.github/workflows/nyholm.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ] + php: [ '7.3', '7.4', '8.0', '8.1', '8.2' ] uses: ./.github/workflows/integration.yml with: php: ${{ matrix.php }} diff --git a/.github/workflows/ringcentral.yml b/.github/workflows/ringcentral.yml index 12523ef..d50ccb1 100644 --- a/.github/workflows/ringcentral.yml +++ b/.github/workflows/ringcentral.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ] + php: [ '7.3', '7.4', '8.0', '8.1', '8.2' ] uses: ./.github/workflows/integration.yml with: php: ${{ matrix.php }} diff --git a/.gitignore b/.gitignore index 3a2f53d..77a33ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /.tmp /.phpunit.result.cache +/.phpunit.cache /behat.yml /build/ /composer.lock diff --git a/composer.json b/composer.json index 02b5705..666e830 100644 --- a/composer.json +++ b/composer.json @@ -14,8 +14,8 @@ } ], "require": { - "php": "^7.2 || ^8.0", - "phpunit/phpunit": "^8.0 || ^9.3", + "php": "^7.3 || ^8.0", + "phpunit/phpunit": "^9.3 || ^10.0", "psr/http-message": "^1.0 || ^2.0" }, "require-dev": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1f1884c..f828cf8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,48 +1,39 @@ - - + + + + ./src + + ./tests/Guzzle/ - ./tests/RingCentral/ - - ./tests/Slim/ + ./tests/Slim/ - - ./tests/Laminas/ + ./tests/Laminas/ - - ./vendor/nyholm/psr7/tests/Integration/ + ./vendor/nyholm/psr7/tests/Integration/ ./vendor/httpsoft/http-message/tests/Integration/ - - - - ./ - - ./Tests - ./vendor - - - diff --git a/src/BaseTest.php b/src/BaseTest.php index aeeb8c4..f126eca 100644 --- a/src/BaseTest.php +++ b/src/BaseTest.php @@ -13,9 +13,15 @@ use Laminas\Diactoros\UploadedFile as LaminasUploadedFile; use Nyholm\Psr7\Factory\Psr17Factory as NyholmFactory; use PHPUnit\Framework\TestCase; -use Psr\Http\Message\UriInterface; use RingCentral\Psr7\Uri as RingCentralUri; use function RingCentral\Psr7\stream_for as ring_central_stream_for; +use GuzzleHttp\Psr7\Utils as GuzzleUtils; +use Http\Message\StreamFactory as HttplugStreamFactory; +use Http\Message\UriFactory as HttplugUriFactory; +use Psr\Http\Message\StreamFactoryInterface as PsrStreamFactoryInterface; +use Psr\Http\Message\UploadedFileFactoryInterface as PsrUploadedFileFactoryInterface; +use Psr\Http\Message\UriFactoryInterface as PsrUriFactoryInterface; +use Psr\Http\Message\UriInterface as PsrUriInterface; use Slim\Psr7\Uri as SlimUri; use Slim\Psr7\Factory\UriFactory as SlimUriFactory; use Slim\Psr7\Factory\StreamFactory as SlimStreamFactory; @@ -36,18 +42,18 @@ protected function buildUri($uri) if (defined('URI_FACTORY')) { $factoryClass = URI_FACTORY; $factory = new $factoryClass(); - if ($factory instanceof \Http\Message\UriFactory) { + if ($factory instanceof HttplugUriFactory) { return $factory->createUri($uri); } - if ($factory instanceof \Psr\Http\Message\UriFactoryInterface) { - if ($uri instanceof UriInterface) { + if ($factory instanceof PsrUriFactoryInterface) { + if ($uri instanceof PsrUriInterface) { return $uri; } return $factory->createUri($uri); } - throw new \RuntimeException('Constant "URI_FACTORY" must be a reference to a Http\Message\UriFactory or \Psr\Http\Message\UriFactoryInterface'); + throw new \RuntimeException('Constant "URI_FACTORY" must be a reference to a '.HttplugUriFactory::class.' or '.PsrUriFactoryInterface::class); } if (class_exists(HttpSoftUri::class)) { @@ -86,10 +92,10 @@ protected function buildStream($data) if (defined('STREAM_FACTORY')) { $factoryClass = STREAM_FACTORY; $factory = new $factoryClass(); - if ($factory instanceof \Http\Message\StreamFactory) { + if ($factory instanceof HttplugStreamFactory) { return $factory->createStream($data); } - if ($factory instanceof \Psr\Http\Message\StreamFactoryInterface) { + if ($factory instanceof PsrStreamFactoryInterface) { if (is_string($data)) { return $factory->createStream($data); } @@ -97,11 +103,11 @@ protected function buildStream($data) return $factory->createStreamFromResource($data); } - throw new \RuntimeException('Constant "STREAM_FACTORY" must be a reference to a Http\Message\StreamFactory or \Psr\Http\Message\StreamFactoryInterface'); + throw new \RuntimeException('Constant "STREAM_FACTORY" must be a reference to a '.HttplugStreamFactory::class.' or '.PsrStreamFactoryInterface::class); } if (class_exists(GuzzleStream::class)) { - return \GuzzleHttp\Psr7\Utils::streamFor($data); + return GuzzleUtils::streamFor($data); } $factory = null; @@ -137,8 +143,8 @@ protected function buildUploadableFile($data) if (defined('UPLOADED_FILE_FACTORY')) { $factoryClass = UPLOADED_FILE_FACTORY; $factory = new $factoryClass(); - if (!$factory instanceof \Psr\Http\Message\UploadedFileFactoryInterface) { - throw new \RuntimeException('Constant "UPLOADED_FILE_FACTORY" must be a reference to a Psr\Http\Message\UploadedFileFactoryInterface'); + if (!$factory instanceof PsrUploadedFileFactoryInterface) { + throw new \RuntimeException('Constant "UPLOADED_FILE_FACTORY" must be a reference to a '.PsrUploadedFileFactoryInterface::class); } $stream = $this->buildStream($data); diff --git a/src/MessageTrait.php b/src/MessageTrait.php index c21bbc9..506d5f1 100644 --- a/src/MessageTrait.php +++ b/src/MessageTrait.php @@ -160,7 +160,7 @@ public function testWithHeaderInvalidArguments($name, $value) } } - public function getInvalidHeaderArguments() + public static function getInvalidHeaderArguments() { return [ [[], 'foo'], @@ -288,11 +288,6 @@ public function testBody() private function assertMatchesRegexp(string $pattern, string $string, string $message = ''): void { - // @TODO remove when package require phpunit 9.1 - if (function_exists('PHPUnit\Framework\assertMatchesRegularExpression')) { - $this->assertMatchesRegularExpression($pattern, $string, $message); - } else { - $this->assertRegExp($pattern, $string, $message); - } + $this->assertMatchesRegularExpression($pattern, $string, $message); } } diff --git a/src/RequestIntegrationTest.php b/src/RequestIntegrationTest.php index 1b21290..f847093 100644 --- a/src/RequestIntegrationTest.php +++ b/src/RequestIntegrationTest.php @@ -118,7 +118,7 @@ public function testMethodWithInvalidArguments($method) } } - public function getInvalidMethods() + public static function getInvalidMethods() { return [ 'null' => [null], diff --git a/src/ResponseIntegrationTest.php b/src/ResponseIntegrationTest.php index 7ab8534..415ccc4 100644 --- a/src/ResponseIntegrationTest.php +++ b/src/ResponseIntegrationTest.php @@ -80,7 +80,7 @@ public function testStatusCodeInvalidArgument($statusCode) } } - public function getInvalidStatusCodeArguments() + public static function getInvalidStatusCodeArguments() { return [ 'true' => [true], diff --git a/src/ServerRequestIntegrationTest.php b/src/ServerRequestIntegrationTest.php index 19065db..d06a8f1 100644 --- a/src/ServerRequestIntegrationTest.php +++ b/src/ServerRequestIntegrationTest.php @@ -99,7 +99,7 @@ public function testGetParsedBody($value) $this->assertEquals($value, $new->getParsedBody()); } - public function validParsedBodyParams() + public static function validParsedBodyParams() { return [ [null], @@ -125,7 +125,7 @@ public function testGetParsedBodyInvalid($value) } } - public function invalidParsedBodyParams() + public static function invalidParsedBodyParams() { return [ [4711], diff --git a/src/StreamIntegrationTest.php b/src/StreamIntegrationTest.php index 804ebd7..a218541 100644 --- a/src/StreamIntegrationTest.php +++ b/src/StreamIntegrationTest.php @@ -315,7 +315,7 @@ public function testGetContentsError() fclose($resource); - $this->expectException(\RuntimeException::class); + $this->expectException(class_exists(\Throwable::class) ? \Throwable::class : \RuntimeException::class); $stream->getContents(); } } diff --git a/src/UploadedFileIntegrationTest.php b/src/UploadedFileIntegrationTest.php index 7be3774..89c8436 100644 --- a/src/UploadedFileIntegrationTest.php +++ b/src/UploadedFileIntegrationTest.php @@ -102,13 +102,8 @@ public function testGetSize() $file = $this->createSubject(); $size = $file->getSize(); - if (false !== $size) { - // @TODO remove when package require phpunit 9.1 - if (function_exists('PHPUnit\Framework\assertMatchesRegularExpression')) { - $this->assertMatchesRegularExpression('|^[0-9]+$|', (string) $size); - } else { - $this->assertRegExp('|^[0-9]+$|', (string) $size); - } + if (null !== $size) { + $this->assertMatchesRegularExpression('|^[0-9]+$|', (string) $size); } else { $this->assertNull($size); } diff --git a/src/UriIntegrationTest.php b/src/UriIntegrationTest.php index e77e89a..7c76378 100644 --- a/src/UriIntegrationTest.php +++ b/src/UriIntegrationTest.php @@ -69,7 +69,7 @@ public function testWithSchemeInvalidArguments($schema) } } - public function getInvalidSchemaArguments() + public static function getInvalidSchemaArguments() { return [ [true], @@ -155,7 +155,7 @@ public function testPort() /** * @dataProvider getPaths */ - public function testPath(UriInterface $uri, $expected) + public function testPath(UriInterface $uri, string $expected) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); @@ -164,22 +164,24 @@ public function testPath(UriInterface $uri, $expected) $this->assertSame($expected, $uri->getPath()); } - public function getPaths() + public static function getPaths() { + $test = new static('uriprovider'); + return [ - [$this->createUri('http://www.foo.com/'), '/'], - [$this->createUri('http://www.foo.com'), ''], - [$this->createUri('foo/bar'), 'foo/bar'], - [$this->createUri('http://www.foo.com/foo bar'), '/foo%20bar'], - [$this->createUri('http://www.foo.com/foo%20bar'), '/foo%20bar'], - [$this->createUri('http://www.foo.com/foo%2fbar'), '/foo%2fbar'], + [$test->createUri('http://www.foo.com/'), '/'], + [$test->createUri('http://www.foo.com'), ''], + [$test->createUri('foo/bar'), 'foo/bar'], + [$test->createUri('http://www.foo.com/foo bar'), '/foo%20bar'], + [$test->createUri('http://www.foo.com/foo%20bar'), '/foo%20bar'], + [$test->createUri('http://www.foo.com/foo%2fbar'), '/foo%2fbar'], ]; } /** * @dataProvider getQueries */ - public function testQuery(UriInterface $uri, $expected) + public function testQuery(UriInterface $uri, string $expected) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); @@ -188,21 +190,23 @@ public function testQuery(UriInterface $uri, $expected) $this->assertSame($expected, $uri->getQuery()); } - public function getQueries() + public static function getQueries() { + $test = new static('uriprovider'); + return [ - [$this->createUri('http://www.foo.com'), ''], - [$this->createUri('http://www.foo.com?'), ''], - [$this->createUri('http://www.foo.com?foo=bar'), 'foo=bar'], - [$this->createUri('http://www.foo.com?foo=bar%26baz'), 'foo=bar%26baz'], - [$this->createUri('http://www.foo.com?foo=bar&baz=biz'), 'foo=bar&baz=biz'], + [$test->createUri('http://www.foo.com'), ''], + [$test->createUri('http://www.foo.com?'), ''], + [$test->createUri('http://www.foo.com?foo=bar'), 'foo=bar'], + [$test->createUri('http://www.foo.com?foo=bar%26baz'), 'foo=bar%26baz'], + [$test->createUri('http://www.foo.com?foo=bar&baz=biz'), 'foo=bar&baz=biz'], ]; } /** * @dataProvider getFragments */ - public function testFragment(UriInterface $uri, $expected) + public function testFragment(UriInterface $uri, string $expected) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); @@ -211,13 +215,15 @@ public function testFragment(UriInterface $uri, $expected) $this->assertEquals($expected, $uri->getFragment()); } - public function getFragments() + public static function getFragments() { + $test = new static('uriprovider'); + return [ - [$this->createUri('http://www.foo.com'), ''], - [$this->createUri('http://www.foo.com#'), ''], - [$this->createUri('http://www.foo.com#foo'), 'foo'], - [$this->createUri('http://www.foo.com#foo%20bar'), 'foo%20bar'], + [$test->createUri('http://www.foo.com'), ''], + [$test->createUri('http://www.foo.com#'), ''], + [$test->createUri('http://www.foo.com#foo'), 'foo'], + [$test->createUri('http://www.foo.com#foo%20bar'), 'foo%20bar'], ]; } diff --git a/tests/Guzzle/RequestTest.php b/tests/Guzzle/RequestTest.php index ca94250..a89abdc 100644 --- a/tests/Guzzle/RequestTest.php +++ b/tests/Guzzle/RequestTest.php @@ -16,7 +16,7 @@ public function createSubject() return new Request('GET', '/'); } - public function getInvalidHeaderArguments() + public static function getInvalidHeaderArguments() { $testCases = parent::getInvalidHeaderArguments(); diff --git a/tests/Guzzle/ResponseTest.php b/tests/Guzzle/ResponseTest.php index 8f051ef..e04017f 100644 --- a/tests/Guzzle/ResponseTest.php +++ b/tests/Guzzle/ResponseTest.php @@ -12,7 +12,7 @@ public function createSubject() return new Response(); } - public function getInvalidHeaderArguments() + public static function getInvalidHeaderArguments() { $testCases = parent::getInvalidHeaderArguments();