Skip to content

Commit

Permalink
chore: update tests for phpunit 10 and 11 (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
rancoud authored Dec 8, 2024
1 parent d191941 commit d1d904c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
4 changes: 4 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +70,7 @@ public function testCreateServerRequestFromArrayRaiseExceptionMethod(): void
}

/** @runInSeparateProcess */
#[RunInSeparateProcess]
public function testCreateServerRequestFromGlobalsWithRequestMethod(): void
{
$_SERVER = \array_merge($_SERVER, ['REQUEST_METHOD' => 'POST']);
Expand All @@ -77,13 +79,15 @@ public function testCreateServerRequestFromGlobalsWithRequestMethod(): void
}

/** @runInSeparateProcess */
#[RunInSeparateProcess]
public function testCreateServerRequestFromGlobalsWithoutRequestMethod(): void
{
$r = (new Factory())->createServerRequestFromGlobals();
static::assertSame('GET', $r->getMethod());
}

/** @runInSeparateProcess */
#[RunInSeparateProcess]
public function testCreateServerRequestFromGlobalsFakeNginx(): void
{
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-gb,en;q=0.5';
Expand Down
8 changes: 7 additions & 1 deletion tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,6 +13,7 @@
/**
* @backupGlobals disabled
*/
#[PreserveGlobalState(false)]
class ResponseTest extends TestCase
{
public function testDefaultConstructor(): void
Expand Down Expand Up @@ -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 "])],
Expand Down Expand Up @@ -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());
Expand All @@ -363,6 +368,7 @@ public function testHeaderValuesAreTrimmed(Response $r): void
/**
* @runInSeparateProcess
*/
#[RunInSeparateProcess]
public function testSend(): void
{
$r = new Response(
Expand Down
7 changes: 5 additions & 2 deletions tests/ServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,7 +13,7 @@

class ServerRequestTest extends TestCase
{
public function dataNormalizeFiles(): array
public static function dataNormalizeFiles(): array
{
return [
'Single file' => [
Expand Down Expand Up @@ -269,6 +270,7 @@ public function testConstruct(): void
* @param $files
* @param $expected
*/
#[DataProvider('dataNormalizeFiles')]
public function testNormalizeFiles($files, $expected): void
{
$result = (new Factory())
Expand All @@ -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',
Expand Down Expand Up @@ -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));
Expand Down
15 changes: 11 additions & 4 deletions tests/UploadedFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +25,7 @@ protected function tearDown(): void
}
}

public function invalidStreams(): array
public static function invalidStreams(): array
{
return [
'null' => [null],
Expand All @@ -42,6 +43,7 @@ public function invalidStreams(): array
*
* @param $streamOrFile
*/
#[DataProvider('invalidStreams')]
public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile): void
{
$this->expectException(\InvalidArgumentException::class);
Expand All @@ -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],
Expand All @@ -63,6 +65,7 @@ public function invalidErrorStatuses(): array
*
* @param $status
*/
#[DataProvider('invalidErrorStatuses')]
public function testRaisesExceptionOnInvalidErrorStatus($status): void
{
$this->expectException(\InvalidArgumentException::class);
Expand Down Expand Up @@ -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' => [''],
Expand All @@ -125,6 +128,7 @@ public function invalidMovePaths(): array
*
* @param $path
*/
#[DataProvider('invalidMovePaths')]
public function testMoveRaisesExceptionForInvalidPath($path): void
{
$stream = Stream::create('Foo bar!');
Expand Down Expand Up @@ -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],
Expand All @@ -183,6 +187,7 @@ public function nonOkErrorStatus(): array
*
* @param $status
*/
#[DataProvider('nonOkErrorStatus')]
public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent($status): void
{
$uploadedFile = new UploadedFile('not ok', 0, $status);
Expand All @@ -194,6 +199,7 @@ public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorSta
*
* @param $status
*/
#[DataProvider('nonOkErrorStatus')]
public function testMoveToRaisesExceptionWhenErrorStatusPresent($status): void
{
$uploadedFile = new UploadedFile('not ok', 0, $status);
Expand All @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rancoud\Http\Message\Uri;

Expand Down Expand Up @@ -53,14 +54,15 @@ public function testCanTransformAndRetrievePartsIndividually(): void
*
* @param $input
*/
#[DataProvider('getValidUris')]
public function testValidUrisStayValid($input): void
{
$uri = new Uri($input);

static::assertSame($input, (string) $uri);
}

public function getValidUris(): array
public static function getValidUris(): array
{
return [
['urn:path-rootless'],
Expand Down Expand Up @@ -94,6 +96,7 @@ public function getValidUris(): array
*
* @param $invalidUri
*/
#[DataProvider('getInvalidUris')]
public function testInvalidUrisThrowException($invalidUri): void
{
$this->expectException(\InvalidArgumentException::class);
Expand All @@ -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)
Expand Down Expand Up @@ -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.-_~!$&\'()*+,;=:@';

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d1d904c

Please sign in to comment.