Skip to content

Commit 6b7e0db

Browse files
Merge pull request #1 from Phauthentic/symfony-trait
Add a Symfny WebTestCase specific trait
2 parents b0f9724 + afb21ff commit 6b7e0db

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

src/OpenAPISymfonySchemaValidator.php renamed to src/Symfony/OpenAPISymfonySchemaValidator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
declare(strict_types=1);
44

5-
namespace Phauthentic\PHPUnit\OpenAPIValidator;
5+
namespace Phauthentic\PHPUnit\OpenAPIValidator\Symfony;
66

77
use Nyholm\Psr7\Factory\Psr17Factory;
8+
use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISchemaValidator;
89
use Psr\Http\Message\RequestInterface as PsrRequestInterface;
910
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
1011
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

tests/OpenAPISymfonySchemaValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Phauthentic\PHPUnit\OpenAPIValidator\Tests;
66

7+
use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISchemaValidationFailedException;
8+
use Phauthentic\PHPUnit\OpenAPIValidator\Symfony\OpenAPISymfonySchemaValidator;
79
use PHPUnit\Framework\Attributes\Test;
810
use PHPUnit\Framework\TestCase;
9-
use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISchemaValidationFailedException;
10-
use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPISymfonySchemaValidator;
1111
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
1212
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
1313

0 commit comments

Comments
 (0)