From d1d904c9911e816b91136c26e91ef0845843e8cf Mon Sep 17 00:00:00 2001 From: Rancoud Date: Sun, 8 Dec 2024 15:32:14 +0100 Subject: [PATCH] chore: update tests for phpunit 10 and 11 (#239) --- tests/FactoryTest.php | 4 ++++ tests/ResponseTest.php | 8 +++++++- tests/ServerRequestTest.php | 7 +++++-- tests/UploadedFileTest.php | 15 +++++++++++---- tests/UriTest.php | 10 +++++++--- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 0fbd0fa..07c4029 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -4,6 +4,7 @@ namespace tests; +use PHPUnit\Framework\Attributes\RunInSeparateProcess; use PHPUnit\Framework\TestCase; use Psr\Http\Message\StreamInterface; use Rancoud\Http\Message\Factory\Factory; @@ -69,6 +70,7 @@ public function testCreateServerRequestFromArrayRaiseExceptionMethod(): void } /** @runInSeparateProcess */ + #[RunInSeparateProcess] public function testCreateServerRequestFromGlobalsWithRequestMethod(): void { $_SERVER = \array_merge($_SERVER, ['REQUEST_METHOD' => 'POST']); @@ -77,6 +79,7 @@ public function testCreateServerRequestFromGlobalsWithRequestMethod(): void } /** @runInSeparateProcess */ + #[RunInSeparateProcess] public function testCreateServerRequestFromGlobalsWithoutRequestMethod(): void { $r = (new Factory())->createServerRequestFromGlobals(); @@ -84,6 +87,7 @@ public function testCreateServerRequestFromGlobalsWithoutRequestMethod(): void } /** @runInSeparateProcess */ + #[RunInSeparateProcess] public function testCreateServerRequestFromGlobalsFakeNginx(): void { $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-gb,en;q=0.5'; diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index cd61609..dc64429 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -2,6 +2,9 @@ namespace tests; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\PreserveGlobalState; +use PHPUnit\Framework\Attributes\RunInSeparateProcess; use PHPUnit\Framework\TestCase; use Psr\Http\Message\StreamInterface; use Rancoud\Http\Message\Factory\Factory; @@ -10,6 +13,7 @@ /** * @backupGlobals disabled */ +#[PreserveGlobalState(false)] class ResponseTest extends TestCase { public function testDefaultConstructor(): void @@ -258,7 +262,7 @@ public function testSameInstanceWhenRemovingMissingHeader(): void static::assertSame($r, $r->withoutHeader('foo')); } - public function trimmedHeaderValues(): array + public static function trimmedHeaderValues(): array { return [ [new Response(200, ['OWS' => " \t \tFoo\t \t "])], @@ -353,6 +357,7 @@ public function testWithoutHeaderNameMustHaveCorrectType(): void * * @param Response $r */ + #[DataProvider('trimmedHeaderValues')] public function testHeaderValuesAreTrimmed(Response $r): void { static::assertSame(['OWS' => ['Foo']], $r->getHeaders()); @@ -363,6 +368,7 @@ public function testHeaderValuesAreTrimmed(Response $r): void /** * @runInSeparateProcess */ + #[RunInSeparateProcess] public function testSend(): void { $r = new Response( diff --git a/tests/ServerRequestTest.php b/tests/ServerRequestTest.php index ea987bf..ee3e917 100644 --- a/tests/ServerRequestTest.php +++ b/tests/ServerRequestTest.php @@ -4,6 +4,7 @@ namespace tests; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Rancoud\Http\Message\Factory\Factory; use Rancoud\Http\Message\ServerRequest; @@ -12,7 +13,7 @@ class ServerRequestTest extends TestCase { - public function dataNormalizeFiles(): array + public static function dataNormalizeFiles(): array { return [ 'Single file' => [ @@ -269,6 +270,7 @@ public function testConstruct(): void * @param $files * @param $expected */ + #[DataProvider('dataNormalizeFiles')] public function testNormalizeFiles($files, $expected): void { $result = (new Factory()) @@ -286,7 +288,7 @@ public function testNormalizeFilesRaisesException(): void (new Factory())->createServerRequestFromArrays(['REQUEST_METHOD' => 'POST'], [], [], [], [], ['test' => 'something']); } - public function dataGetUriFromGlobals(): array + public static function dataGetUriFromGlobals(): array { $server = [ 'PHP_SELF' => '/blog/article.php', @@ -353,6 +355,7 @@ public function dataGetUriFromGlobals(): array * @param $expected * @param $serverParams */ + #[DataProvider('dataGetUriFromGlobals')] public function testGetUriFromGlobals($expected, $serverParams): void { static::assertEqualsCanonicalizing(new Uri($expected), (new Factory())->createUriFromArray($serverParams)); diff --git a/tests/UploadedFileTest.php b/tests/UploadedFileTest.php index c109f6d..e195c92 100644 --- a/tests/UploadedFileTest.php +++ b/tests/UploadedFileTest.php @@ -2,6 +2,7 @@ namespace tests; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Rancoud\Http\Message\Stream; use Rancoud\Http\Message\UploadedFile; @@ -24,7 +25,7 @@ protected function tearDown(): void } } - public function invalidStreams(): array + public static function invalidStreams(): array { return [ 'null' => [null], @@ -42,6 +43,7 @@ public function invalidStreams(): array * * @param $streamOrFile */ + #[DataProvider('invalidStreams')] public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile): void { $this->expectException(\InvalidArgumentException::class); @@ -50,7 +52,7 @@ public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile): void new UploadedFile($streamOrFile, 0, \UPLOAD_ERR_OK); } - public function invalidErrorStatuses(): array + public static function invalidErrorStatuses(): array { return [ 'negative' => [-1], @@ -63,6 +65,7 @@ public function invalidErrorStatuses(): array * * @param $status */ + #[DataProvider('invalidErrorStatuses')] public function testRaisesExceptionOnInvalidErrorStatus($status): void { $this->expectException(\InvalidArgumentException::class); @@ -113,7 +116,7 @@ public function testSuccessful(): void static::assertSame($stream->__toString(), \file_get_contents($to)); } - public function invalidMovePaths(): array + public static function invalidMovePaths(): array { return [ 'empty' => [''], @@ -125,6 +128,7 @@ public function invalidMovePaths(): array * * @param $path */ + #[DataProvider('invalidMovePaths')] public function testMoveRaisesExceptionForInvalidPath($path): void { $stream = Stream::create('Foo bar!'); @@ -165,7 +169,7 @@ public function testCannotRetrieveStreamAfterMove(): void $upload->getStream(); } - public function nonOkErrorStatus(): array + public static function nonOkErrorStatus(): array { return [ 'UPLOAD_ERR_INI_SIZE' => [\UPLOAD_ERR_INI_SIZE], @@ -183,6 +187,7 @@ public function nonOkErrorStatus(): array * * @param $status */ + #[DataProvider('nonOkErrorStatus')] public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent($status): void { $uploadedFile = new UploadedFile('not ok', 0, $status); @@ -194,6 +199,7 @@ public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorSta * * @param $status */ + #[DataProvider('nonOkErrorStatus')] public function testMoveToRaisesExceptionWhenErrorStatusPresent($status): void { $uploadedFile = new UploadedFile('not ok', 0, $status); @@ -207,6 +213,7 @@ public function testMoveToRaisesExceptionWhenErrorStatusPresent($status): void * * @param $status */ + #[DataProvider('nonOkErrorStatus')] public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status): void { $uploadedFile = new UploadedFile('not ok', 0, $status); diff --git a/tests/UriTest.php b/tests/UriTest.php index 3a44278..3cc1920 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -4,6 +4,7 @@ namespace tests; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Rancoud\Http\Message\Uri; @@ -53,6 +54,7 @@ public function testCanTransformAndRetrievePartsIndividually(): void * * @param $input */ + #[DataProvider('getValidUris')] public function testValidUrisStayValid($input): void { $uri = new Uri($input); @@ -60,7 +62,7 @@ public function testValidUrisStayValid($input): void static::assertSame($input, (string) $uri); } - public function getValidUris(): array + public static function getValidUris(): array { return [ ['urn:path-rootless'], @@ -94,6 +96,7 @@ public function getValidUris(): array * * @param $invalidUri */ + #[DataProvider('getInvalidUris')] public function testInvalidUrisThrowException($invalidUri): void { $this->expectException(\InvalidArgumentException::class); @@ -102,7 +105,7 @@ public function testInvalidUrisThrowException($invalidUri): void new Uri($invalidUri); } - public function getInvalidUris(): array + public static function getInvalidUris(): array { return [ // parse_url() requires the host component which makes sense for http(s) @@ -313,7 +316,7 @@ public function testAuthorityWithUserInfoButWithoutHost(): void static::assertSame('', $uri->getAuthority()); } - public function uriComponentsEncodingProvider(): array + public static function uriComponentsEncodingProvider(): array { $unreserved = 'a-zA-Z0-9.-_~!$&\'()*+,;=:@'; @@ -344,6 +347,7 @@ public function uriComponentsEncodingProvider(): array * @param $fragment * @param $output */ + #[DataProvider('uriComponentsEncodingProvider')] public function testUriComponentsGetEncodedProperly($input, $path, $query, $fragment, $output): void { $uri = new Uri($input);