Skip to content

Adapt tests to work with v1.1 and v2.0 of PSR-7 #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ jobs:
# and finally require the implementation to test with source flag (to get integration test cases that might be excluded in git-attributes)
run: |
composer remove --dev guzzlehttp/psr7 laminas/laminas-diactoros nyholm/psr7 ringcentral/psr7 slim/psr7 --no-update
composer update --no-interaction --no-progress
composer require ${{ inputs.package }} --no-interaction --no-progress --prefer-source

- name: Execute tests
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
These validate that usernames and passwords which contain reserved characters (defined by RFC3986) are being encoded
so that the URI does not contain these reserved characters at any time.

- Adds support for testing against PSR-7 1.1 and 2.0. In particular, it adapts tests that were verifying invalid parameters threw `InvalidArgumentException` previously now either throw that OR (more correctly) raise a `TypeError`.

## [1.2.0] - 2022-12-01

### Added
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"require": {
"php": "^7.2 || ^8.0",
"phpunit/phpunit": "^8.0 || ^9.3",
"psr/http-message": "^1.0"
"psr/http-message": "^1.0 || ^2.0"
},
"require-dev": {
"guzzlehttp/psr7": "^1.7 || ^2.0",
"laminas/laminas-diactoros": "^2.1",
"nyholm/psr7": "^1.0",
"ringcentral/psr7": "^1.2",
"slim/psr7": "dev-master"
"slim/psr7": "^1.4"
},
"extra": {
"branch-alias": {
Expand Down
46 changes: 40 additions & 6 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Http\Psr7Test;

use InvalidArgumentException;
use PHPUnit\Framework\AssertionFailedError;
use Psr\Http\Message\MessageInterface;
use Throwable;
use TypeError;

/**
* Test MessageInterface.
Expand Down Expand Up @@ -136,9 +140,24 @@ public function testWithHeaderInvalidArguments($name, $value)
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$this->expectException(\InvalidArgumentException::class);
$initialMessage = $this->getMessage();
$initialMessage->withHeader($name, $value);

try {
$initialMessage = $this->getMessage();
$initialMessage->withHeader($name, $value);
$this->fail('withHeader() should have raised exception on invalid argument');
} catch (AssertionFailedError $e) {
// invalid argument not caught
throw $e;
} catch (TypeError|InvalidArgumentException $e) {
// valid
$this->assertTrue($e instanceof Throwable);
} catch (Throwable $e) {
// invalid
$this->fail(sprintf(
'Unexpected exception (%s) thrown from withHeader(); expected TypeError or InvalidArgumentException',
gettype($e)
));
}
}

public function getInvalidHeaderArguments()
Expand Down Expand Up @@ -174,9 +193,24 @@ public function testWithAddedHeaderInvalidArguments($name, $value)
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$this->expectException(\InvalidArgumentException::class);
$initialMessage = $this->getMessage();
$initialMessage->withAddedHeader($name, $value);

try {
$initialMessage = $this->getMessage();
$initialMessage->withAddedHeader($name, $value);
$this->fail('withAddedHeader() should have raised exception on invalid argument');
} catch (AssertionFailedError $e) {
// invalid argument not caught
throw $e;
} catch (TypeError|InvalidArgumentException $e) {
// valid
$this->assertTrue($e instanceof Throwable);
} catch (Throwable $e) {
// invalid
$this->fail(sprintf(
'Unexpected exception (%s) thrown from withAddedHeader(); expected TypeError or InvalidArgumentException',
gettype($e)
));
}
}

/**
Expand Down
32 changes: 24 additions & 8 deletions src/RequestIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Http\Psr7Test;

use InvalidArgumentException;
use PHPUnit\Framework\AssertionFailedError;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Throwable;
use TypeError;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
Expand Down Expand Up @@ -96,19 +100,31 @@ public function testMethodWithInvalidArguments($method)
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$this->expectException(\InvalidArgumentException::class);
$this->request->withMethod($method);
try {
$this->request->withMethod($method);
$this->fail('withMethod() should have raised exception on invalid argument');
} catch (AssertionFailedError $e) {
// invalid argument not caught
throw $e;
} catch (InvalidArgumentException|TypeError $e) {
// valid
$this->assertTrue($e instanceof Throwable);
} catch (Throwable $e) {
// invalid
$this->fail(sprintf(
'Unexpected exception (%s) thrown from withMethod(); expected TypeError or InvalidArgumentException',
gettype($e)
));
}
}

public function getInvalidMethods()
{
return [
[null],
[1],
[1.01],
[false],
[['foo']],
[new \stdClass()],
'null' => [null],
'false' => [false],
'array' => [['foo']],
'object' => [new \stdClass()],
];
}

Expand Down
33 changes: 25 additions & 8 deletions src/ResponseIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Http\Psr7Test;

use InvalidArgumentException;
use PHPUnit\Framework\AssertionFailedError;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use TypeError;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
Expand Down Expand Up @@ -58,19 +62,32 @@ public function testStatusCodeInvalidArgument($statusCode)
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$this->expectException(\InvalidArgumentException::class);
$this->response->withStatus($statusCode);
try {
$this->response->withStatus($statusCode);
$this->fail('withStatus() should have raised exception on invalid argument');
} catch (AssertionFailedError $e) {
// invalid argument not caught
throw $e;
} catch (InvalidArgumentException|TypeError $e) {
// valid
$this->assertTrue($e instanceof Throwable);
} catch (Throwable $e) {
// invalid
$this->fail(sprintf(
'Unexpected exception (%s) thrown from withStatus(); expected TypeError or InvalidArgumentException',
gettype($e)
));
}
}

public function getInvalidStatusCodeArguments()
{
return [
[true],
['foobar'],
[99],
[600],
[200.34],
[new \stdClass()],
'true' => [true],
'string' => ['foobar'],
'too-low' => [99],
'too-high' => [600],
'object' => [new \stdClass()],
];
}

Expand Down
22 changes: 20 additions & 2 deletions src/UriIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Http\Psr7Test;

use InvalidArgumentException;
use PHPUnit\Framework\AssertionFailedError;
use Psr\Http\Message\UriInterface;
use Throwable;
use TypeError;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
Expand Down Expand Up @@ -47,8 +51,22 @@ public function testWithSchemeInvalidArguments($schema)
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$this->expectException(\InvalidArgumentException::class);
$this->createUri('/')->withScheme($schema);
try {
$this->createUri('/')->withScheme($schema);
$this->fail('withScheme() should have raised exception on invalid argument');
} catch (AssertionFailedError $e) {
// invalid argument not caught
throw $e;
} catch (InvalidArgumentException|TypeError $e) {
// valid
$this->assertTrue($e instanceof Throwable);
} catch (Throwable $e) {
// invalid
$this->fail(sprintf(
'Unexpected exception (%s) thrown from withScheme(); expected TypeError or InvalidArgumentException',
gettype($e)
));
}
}

public function getInvalidSchemaArguments()
Expand Down