|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Phauthentic\PHPUnit\OpenAPIValidator\Symfony; |
| 6 | + |
| 7 | +use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISchemaValidationFailedException; |
| 8 | +use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISchemaValidatorInterface; |
| 9 | +use PHPUnit\Framework\Exception; |
| 10 | +use Psr\Http\Message\RequestInterface; |
| 11 | +use Psr\Http\Message\ResponseInterface; |
| 12 | +use RuntimeException; |
| 13 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * @phpstan-ignore-next-line |
| 17 | + */ |
| 18 | +trait OpenAPIValidatorSymfonyTrait |
| 19 | +{ |
| 20 | + protected static ?OpenAPISymfonySchemaValidator $openAPISchemaValidator; |
| 21 | + |
| 22 | + protected static function getOpenAPISchemaValidator(): OpenAPISchemaValidatorInterface |
| 23 | + { |
| 24 | + if (null === self::$openAPISchemaValidator) { |
| 25 | + throw new Exception( |
| 26 | + 'OpenAPISchemaValidator is not set. ' |
| 27 | + . 'Call self::setOpenAPISchemaValidator() before using this method.' |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + return self::$openAPISchemaValidator; |
| 32 | + } |
| 33 | + |
| 34 | + protected static function setOpenAPISchemaValidator(OpenAPISymfonySchemaValidator $validator): void |
| 35 | + { |
| 36 | + self::$openAPISchemaValidator = $validator; |
| 37 | + } |
| 38 | + |
| 39 | + protected static function assertRequestMatchesOpenAPISchema(?RequestInterface $request = null): void |
| 40 | + { |
| 41 | + if (null === $request) { |
| 42 | + self::assertWebTestCase(); |
| 43 | + $request = self::getClient()->getRequest(); |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + self::getOpenAPISchemaValidator()->validateRequest($request); |
| 48 | + } catch (OpenAPISchemaValidationFailedException $exception) { |
| 49 | + self::fail('OpenAPI request validation failed: ' . $exception->getMessage()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + protected static function assertResponseMatchesOpenAPISchema( |
| 54 | + string $path, |
| 55 | + string $method, |
| 56 | + ?ResponseInterface $response = null, |
| 57 | + ): void { |
| 58 | + if (null === $response) { |
| 59 | + self::assertWebTestCase(); |
| 60 | + $response = self::getClient()->getResponse(); |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + self::getOpenAPISchemaValidator()->validateResponse( |
| 65 | + response: $response, |
| 66 | + path: $path, |
| 67 | + method: $method |
| 68 | + ); |
| 69 | + } catch (OpenAPISchemaValidationFailedException $exception) { |
| 70 | + self::fail('OpenAPI response validation failed: ' . $exception->getMessage()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static function assertWebTestCase(): void |
| 75 | + { |
| 76 | + if (!is_subclass_of(static::class, WebTestCase::class)) { |
| 77 | + throw new RuntimeException( |
| 78 | + 'OpenAPIValidatorSymfonyTrait can only be used in a class that extends ' |
| 79 | + . 'Symfony\Bundle\FrameworkBundle\Test\WebTestCase.' |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments