diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 9fdbf49..605523c 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 1985596..c579e87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json index 7ca39e0..8089536 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/MessageTrait.php b/src/MessageTrait.php index 5b30d16..c21bbc9 100644 --- a/src/MessageTrait.php +++ b/src/MessageTrait.php @@ -2,7 +2,11 @@ namespace Http\Psr7Test; +use InvalidArgumentException; +use PHPUnit\Framework\AssertionFailedError; use Psr\Http\Message\MessageInterface; +use Throwable; +use TypeError; /** * Test MessageInterface. @@ -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() @@ -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) + )); + } } /** diff --git a/src/RequestIntegrationTest.php b/src/RequestIntegrationTest.php index 5b6d6a5..1b21290 100644 --- a/src/RequestIntegrationTest.php +++ b/src/RequestIntegrationTest.php @@ -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 @@ -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()], ]; } diff --git a/src/ResponseIntegrationTest.php b/src/ResponseIntegrationTest.php index 9abd790..7ab8534 100644 --- a/src/ResponseIntegrationTest.php +++ b/src/ResponseIntegrationTest.php @@ -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 @@ -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()], ]; } diff --git a/src/UriIntegrationTest.php b/src/UriIntegrationTest.php index c1181cf..e77e89a 100644 --- a/src/UriIntegrationTest.php +++ b/src/UriIntegrationTest.php @@ -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 @@ -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()