Skip to content

Add a Symfny WebTestCase specific trait #1

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 2 commits into from
Jun 4, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
83 changes: 83 additions & 0 deletions src/Symfony/OpenAPIValidatorSymfonyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Phauthentic\PHPUnit\OpenAPIValidator\Symfony;

use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISchemaValidationFailedException;
use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISchemaValidatorInterface;
use PHPUnit\Framework\Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
* @phpstan-ignore-next-line
*/
trait OpenAPIValidatorSymfonyTrait
{
protected static ?OpenAPISymfonySchemaValidator $openAPISchemaValidator;

protected static function getOpenAPISchemaValidator(): OpenAPISchemaValidatorInterface
{
if (null === self::$openAPISchemaValidator) {
throw new Exception(
'OpenAPISchemaValidator is not set. '
. 'Call self::setOpenAPISchemaValidator() before using this method.'
);
}

return self::$openAPISchemaValidator;
}

protected static function setOpenAPISchemaValidator(OpenAPISymfonySchemaValidator $validator): void
{
self::$openAPISchemaValidator = $validator;
}

protected static function assertRequestMatchesOpenAPISchema(?RequestInterface $request = null): void
{
if (null === $request) {
self::assertWebTestCase();
$request = self::getClient()->getRequest();
}

try {
self::getOpenAPISchemaValidator()->validateRequest($request);
} catch (OpenAPISchemaValidationFailedException $exception) {
self::fail('OpenAPI request validation failed: ' . $exception->getMessage());
}
}

protected static function assertResponseMatchesOpenAPISchema(
string $path,
string $method,
?ResponseInterface $response = null,
): 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.'
);
}
}
}
4 changes: 2 additions & 2 deletions tests/OpenAPISymfonySchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down