From 1032f9925fc140d44003e0dc0291935b028e7046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Wed, 4 Jun 2025 16:39:02 +0200 Subject: [PATCH 1/2] Adding a trait for Symfony WebTestCase --- .../OpenAPISymfonySchemaValidator.php | 3 +- src/Symfony/OpenAPIValidatorSymfonyTrait.php | 83 +++++++++++++++++++ tests/OpenAPISymfonySchemaValidatorTest.php | 4 +- 3 files changed, 87 insertions(+), 3 deletions(-) rename src/{ => Symfony}/OpenAPISymfonySchemaValidator.php (93%) create mode 100644 src/Symfony/OpenAPIValidatorSymfonyTrait.php diff --git a/src/OpenAPISymfonySchemaValidator.php b/src/Symfony/OpenAPISymfonySchemaValidator.php similarity index 93% rename from src/OpenAPISymfonySchemaValidator.php rename to src/Symfony/OpenAPISymfonySchemaValidator.php index aa0626f..be5df85 100644 --- a/src/OpenAPISymfonySchemaValidator.php +++ b/src/Symfony/OpenAPISymfonySchemaValidator.php @@ -2,9 +2,10 @@ declare(strict_types=1); -namespace Phauthentic\PHPUnit\OpenAPIValidator; +namespace Phauthentic\PHPUnit\OpenAPIValidator\Symfony; use Nyholm\Psr7\Factory\Psr17Factory; +use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISchemaValidator; use Psr\Http\Message\RequestInterface as PsrRequestInterface; use Psr\Http\Message\ResponseInterface as PsrResponseInterface; use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; diff --git a/src/Symfony/OpenAPIValidatorSymfonyTrait.php b/src/Symfony/OpenAPIValidatorSymfonyTrait.php new file mode 100644 index 0000000..2108ad8 --- /dev/null +++ b/src/Symfony/OpenAPIValidatorSymfonyTrait.php @@ -0,0 +1,83 @@ +getRequest(); + } + + try { + self::getOpenAPISchemaValidator()->validateRequest($request); + } catch (OpenAPISchemaValidationFailedException $exception) { + self::fail('OpenAPI request validation failed: ' . $exception->getMessage()); + } + } + + protected static function assertResponseMatchesOpenAPISchema( + ?ResponseInterface $response, + string $path, + string $method, + ): void { + if (null === $response) { + self::assertWebTestCase(); + $response = self::getClient()->getResponse(); + } + + try { + self::getOpenAPISchemaValidator()->validateResponse( + response: $response, + path: $path, + method: $method + ); + } catch (OpenAPISchemaValidationFailedException $exception) { + self::fail('OpenAPI response validation failed: ' . $exception->getMessage()); + } + } + + private static function assertWebTestCase(): void + { + if (!is_subclass_of(static::class, WebTestCase::class)) { + throw new RuntimeException( + 'OpenAPIValidatorSymfonyTrait can only be used in a class that extends ' + . 'Symfony\Bundle\FrameworkBundle\Test\WebTestCase.' + ); + } + } +} diff --git a/tests/OpenAPISymfonySchemaValidatorTest.php b/tests/OpenAPISymfonySchemaValidatorTest.php index 40cc4a9..0250fb8 100644 --- a/tests/OpenAPISymfonySchemaValidatorTest.php +++ b/tests/OpenAPISymfonySchemaValidatorTest.php @@ -4,10 +4,10 @@ namespace Phauthentic\PHPUnit\OpenAPIValidator\Tests; +use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISchemaValidationFailedException; +use Phauthentic\PHPUnit\OpenAPIValidator\Symfony\OpenAPISymfonySchemaValidator; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; -use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISchemaValidationFailedException; -use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISymfonySchemaValidator; use Symfony\Component\HttpFoundation\Request as SymfonyRequest; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; From afb21ff0432b8153fe5b63f1d31a36c47c49f82a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Wed, 4 Jun 2025 16:42:57 +0200 Subject: [PATCH 2/2] Fixing optional response arg --- src/Symfony/OpenAPIValidatorSymfonyTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/OpenAPIValidatorSymfonyTrait.php b/src/Symfony/OpenAPIValidatorSymfonyTrait.php index 2108ad8..e8eb808 100644 --- a/src/Symfony/OpenAPIValidatorSymfonyTrait.php +++ b/src/Symfony/OpenAPIValidatorSymfonyTrait.php @@ -51,9 +51,9 @@ protected static function assertRequestMatchesOpenAPISchema(?RequestInterface $r } protected static function assertResponseMatchesOpenAPISchema( - ?ResponseInterface $response, string $path, string $method, + ?ResponseInterface $response = null, ): void { if (null === $response) { self::assertWebTestCase();