From 521283f03aa6141e52d020554ee7076521969f3e Mon Sep 17 00:00:00 2001 From: osteel Date: Fri, 16 Jun 2023 17:18:47 +0100 Subject: [PATCH] reverted use of union types --- src/Adapters/AdapterInterface.php | 8 +-- src/Adapters/HttpFoundationAdapter.php | 11 +++- src/Validator.php | 64 +++++++++----------- src/ValidatorInterface.php | 60 +++++++++--------- tests/Adapters/HttpFoundationAdapterTest.php | 9 +++ 5 files changed, 76 insertions(+), 76 deletions(-) diff --git a/src/Adapters/AdapterInterface.php b/src/Adapters/AdapterInterface.php index 58289bb..a9eae07 100644 --- a/src/Adapters/AdapterInterface.php +++ b/src/Adapters/AdapterInterface.php @@ -5,17 +5,13 @@ namespace Osteel\OpenApi\Testing\Adapters; use Psr\Http\Message\MessageInterface; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; interface AdapterInterface { /** * Convert a HTTP message to a PSR-7 HTTP message. * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to convert + * @param object $message the HTTP message to convert */ - public function convert(Request|Response|ResponseInterface|ServerRequestInterface $message): MessageInterface; + public function convert(object $message): MessageInterface; } diff --git a/src/Adapters/HttpFoundationAdapter.php b/src/Adapters/HttpFoundationAdapter.php index 87b1c45..1d750de 100644 --- a/src/Adapters/HttpFoundationAdapter.php +++ b/src/Adapters/HttpFoundationAdapter.php @@ -4,6 +4,7 @@ namespace Osteel\OpenApi\Testing\Adapters; +use InvalidArgumentException; use Nyholm\Psr7\Factory\Psr17Factory; use Psr\Http\Message\MessageInterface; use Psr\Http\Message\ResponseInterface; @@ -17,9 +18,9 @@ final class HttpFoundationAdapter implements AdapterInterface /** * @inheritDoc * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to convert + * @param object $message the HTTP message to convert */ - public function convert(Request|Response|ResponseInterface|ServerRequestInterface $message): MessageInterface + public function convert(object $message): MessageInterface { if ($message instanceof ResponseInterface || $message instanceof ServerRequestInterface) { return $message; @@ -32,6 +33,10 @@ public function convert(Request|Response|ResponseInterface|ServerRequestInterfac return $psrHttpFactory->createResponse($message); } - return $psrHttpFactory->createRequest($message); + if ($message instanceof Request) { + return $psrHttpFactory->createRequest($message); + } + + throw new InvalidArgumentException(sprintf('Unsupported %s object received', $message::class)); } } diff --git a/src/Validator.php b/src/Validator.php index b6e83fc..cfeecd7 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -11,9 +11,6 @@ use Osteel\OpenApi\Testing\Adapters\AdapterInterface; use Osteel\OpenApi\Testing\Exceptions\ValidationException; use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; final class Validator implements ValidatorInterface { @@ -27,17 +24,14 @@ public function __construct( /** * @inheritDoc * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path - * @param string $method the HTTP method + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path + * @param string $method the HTTP method * * @throws ValidationException */ - public function validate( - Request|Response|ResponseInterface|ServerRequestInterface $message, - string $path, - string $method, - ): bool { + public function validate(object $message, string $path, string $method): bool + { $message = $this->adapter->convert($message); $operation = $this->getOperationAddress($path, $method); $validator = $message instanceof ResponseInterface ? $this->responseValidator : $this->requestValidator; @@ -68,12 +62,12 @@ private function getOperationAddress(string $path, string $method): OperationAdd /** * @inheritDoc * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function delete(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool + public function delete(object $message, string $path): bool { return $this->validate($message, $path, 'delete'); } @@ -81,12 +75,12 @@ public function delete(Request|Response|ResponseInterface|ServerRequestInterface /** * @inheritDoc * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function get(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool + public function get(object $message, string $path): bool { return $this->validate($message, $path, 'get'); } @@ -94,12 +88,12 @@ public function get(Request|Response|ResponseInterface|ServerRequestInterface $m /** * @inheritDoc * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function head(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool + public function head(object $message, string $path): bool { return $this->validate($message, $path, 'head'); } @@ -107,12 +101,12 @@ public function head(Request|Response|ResponseInterface|ServerRequestInterface $ /** * @inheritDoc * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function options(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool + public function options(object $message, string $path): bool { return $this->validate($message, $path, 'options'); } @@ -120,12 +114,12 @@ public function options(Request|Response|ResponseInterface|ServerRequestInterfac /** * @inheritDoc * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function patch(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool + public function patch(object $message, string $path): bool { return $this->validate($message, $path, 'patch'); } @@ -133,12 +127,12 @@ public function patch(Request|Response|ResponseInterface|ServerRequestInterface /** * @inheritDoc * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function post(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool + public function post(object $message, string $path): bool { return $this->validate($message, $path, 'post'); } @@ -146,12 +140,12 @@ public function post(Request|Response|ResponseInterface|ServerRequestInterface $ /** * @inheritDoc * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function put(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool + public function put(object $message, string $path): bool { return $this->validate($message, $path, 'put'); } @@ -159,12 +153,12 @@ public function put(Request|Response|ResponseInterface|ServerRequestInterface $m /** * @inheritDoc * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function trace(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool + public function trace(object $message, string $path): bool { return $this->validate($message, $path, 'trace'); } diff --git a/src/ValidatorInterface.php b/src/ValidatorInterface.php index 98b7fda..179e16f 100644 --- a/src/ValidatorInterface.php +++ b/src/ValidatorInterface.php @@ -6,101 +6,97 @@ use League\OpenAPIValidation\PSR7\Exception\ValidationFailed; use Osteel\OpenApi\Testing\Exceptions\ValidationException; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; interface ValidatorInterface { /** * Validate a HTTP message against the specified OpenAPI definition. * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path - * @param string $method the HTTP method + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path + * @param string $method the HTTP method * * @throws ValidationException */ - public function validate(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path, string $method): bool; + public function validate(object $message, string $path, string $method): bool; /** * Validate a HTTP message for a DELETE operation on the provided OpenAPI definition path. * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function delete(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool; + public function delete(object $message, string $path): bool; /** * Validate a HTTP message for a GET operation on the provided OpenAPI definition path. * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function get(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool; + public function get(object $message, string $path): bool; /** * Validate a HTTP message for a HEAD operation on the provided OpenAPI definition path. * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function head(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool; + public function head(object $message, string $path): bool; /** * Validate a HTTP message for a OPTIONS operation on the provided OpenAPI definition path. * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function options(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool; + public function options(object $message, string $path): bool; /** * Validate a HTTP message for a PATCH operation on the provided OpenAPI definition path. * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function patch(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool; + public function patch(object $message, string $path): bool; /** * Validate a HTTP message for a POST operation on the provided OpenAPI definition path. * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function post(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool; + public function post(object $message, string $path): bool; /** * Validate a HTTP message for a PUT operation on the provided OpenAPI definition path. * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function put(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool; + public function put(object $message, string $path): bool; /** * Validate a HTTP message for a TRACE operation on the provided OpenAPI definition path. * - * @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate - * @param string $path the OpenAPI path + * @param object $message the HTTP message to validate + * @param string $path the OpenAPI path * * @throws ValidationFailed */ - public function trace(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool; + public function trace(object $message, string $path): bool; } diff --git a/tests/Adapters/HttpFoundationAdapterTest.php b/tests/Adapters/HttpFoundationAdapterTest.php index 00e8c26..41c0f48 100644 --- a/tests/Adapters/HttpFoundationAdapterTest.php +++ b/tests/Adapters/HttpFoundationAdapterTest.php @@ -2,6 +2,7 @@ namespace Osteel\OpenApi\Testing\Tests\Adapters; +use InvalidArgumentException; use Osteel\OpenApi\Testing\Adapters\HttpFoundationAdapter; use Osteel\OpenApi\Testing\Tests\TestCase; use Psr\Http\Message\ResponseInterface; @@ -20,6 +21,14 @@ protected function setUp(): void $this->sut = new HttpFoundationAdapter(); } + public function test_it_does_not_convert_the_message_because_the_type_is_not_supported() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Unsupported InvalidArgumentException object received'); + + $this->sut->convert(new InvalidArgumentException()); + } + public function test_it_converts_the_http_foundation_request() { $result = $this->sut->convert(Request::create('/foo'));