Skip to content
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
13 changes: 10 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'@PSR12' => true,
'blank_line_after_opening_tag' => false,
'@DoctrineAnnotation' => true,
'array_syntax' => ['syntax' => 'short'],
'no_unused_imports' => true,
'ordered_imports' => true,
'blank_line_before_statement' => ['statements' => ['return']],
'visibility_required' => true,
'modifier_keywords' => true,
'cast_spaces' => ['space' => 'single'],
'concat_space' => ['spacing' => 'one'],
'type_declaration_spaces' => true,
Expand All @@ -40,10 +42,11 @@
'trim_array_spaces' => true,
'single_space_around_construct' => true,
'single_line_comment_spacing' => true,
'fully_qualified_strict_types' => true,
'fully_qualified_strict_types' => ['import_symbols' => true, 'leading_backslash_in_global_namespace' => true],
'global_namespace_import' => ['import_classes' => false, 'import_constants' => null, 'import_functions' => null],

'nullable_type_declaration_for_default_null_value' => true,
'attribute_empty_parentheses' => ['use_parentheses' => false],

'no_empty_phpdoc' => true,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
Expand All @@ -64,6 +67,10 @@
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_no_alias_tag' => true,
'phpdoc_param_order' => true,
'multiline_comment_opening_closing' => true,

'php_unit_attributes' => true,
])
->setFinder($finder)
;
14 changes: 9 additions & 5 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
use Rector\CodeQuality\Rector\Attribute\SortAttributeNamedArgsRector;
use Rector\CodeQuality\Rector\FuncCall\SortCallLikeNamedArgsRector;
use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\ValueObject\PhpVersion;

return RectorConfig::configure()
->withRules([
TypedPropertyFromStrictConstructorRector::class
])
->withPreparedSets(true, true)
->withSkipPath(__DIR__ . '/tests/Fixtures')
->withSkip([
// reorders the named arguments of swagger-php attributes, which decides the order
// of the generated specifications
SortAttributeNamedArgsRector::class,
SortCallLikeNamedArgsRector::class,
])
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
typeDeclarations: true,
phpunitCodeQuality: true,
)
->withAttributesSets(phpunit: true)
->withPhpVersion(PhpVersion::PHP_82);
5 changes: 3 additions & 2 deletions src/Adapters/AbstractOpenApiResponseVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Radebatz\OpenApi\Verifier\Adapters;

use Composer\Autoload\ClassLoader;
use OpenApi\Generator;

trait AbstractOpenApiResponseVerifier
Expand Down Expand Up @@ -41,9 +42,9 @@ protected function prepareOpenApiSpecificationLoader(string $srcDir, ?string $sp
*/
protected function getAppRoot(): string
{
$classLoader = (new \ReflectionClass(\Composer\Autoload\ClassLoader::class))->getFileName();
$classLoader = (new \ReflectionClass(ClassLoader::class))->getFileName();
if (false === $classLoader) {
throw new \RuntimeException('Cannot locate the project root: Composer\'s ClassLoader has no file');
throw new \RuntimeException("Cannot locate the project root: Composer's ClassLoader has no file");
}

return dirname($classLoader, 3);
Expand Down
3 changes: 2 additions & 1 deletion src/Adapters/Laravel/OpenApiResponseVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
*/
trait OpenApiResponseVerifier
{
use AbstractOpenApiResponseVerifier, VerifiesOpenApi;
use AbstractOpenApiResponseVerifier;
use VerifiesOpenApi;

public function registerOpenApiVerifier(?Application $app = null, ?string $specification = null): void
{
Expand Down
6 changes: 3 additions & 3 deletions src/Adapters/Laravel/OpenApiVerifierMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public function terminate(Request $request, Response $response): void
// matched none, a 404 for example, has none
$route = ($request->getRouteResolver())();
$this->verifier->verify($psrRequest, $psrResponse, $route instanceof Route ? $route->uri() : null);
} catch (OpenApiSchemaMismatchException $oasme) {
} catch (OpenApiSchemaMismatchException $openApiSchemaMismatchException) {
if ($this->onMismatch instanceof \Closure) {
($this->onMismatch)($oasme, $psrResponse);
($this->onMismatch)($openApiSchemaMismatchException, $psrResponse);
}

throw $oasme;
throw $openApiSchemaMismatchException;
}
}
}
6 changes: 3 additions & 3 deletions src/Adapters/Psr15/OpenApiVerifierMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

try {
$this->verifier->verify($request, $response);
} catch (OpenApiSchemaMismatchException $oasme) {
} catch (OpenApiSchemaMismatchException $openApiSchemaMismatchException) {
if ($this->onMismatch instanceof \Closure) {
($this->onMismatch)($oasme, $response);
($this->onMismatch)($openApiSchemaMismatchException, $response);
}

throw $oasme;
throw $openApiSchemaMismatchException;
}

return $response;
Expand Down
3 changes: 2 additions & 1 deletion src/Adapters/Slim/OpenApiResponseVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

trait OpenApiResponseVerifier
{
use AbstractOpenApiResponseVerifier, VerifiesOpenApi;
use AbstractOpenApiResponseVerifier;
use VerifiesOpenApi;

/**
* @template TContainer of \Psr\Container\ContainerInterface|null
Expand Down
2 changes: 1 addition & 1 deletion src/VerifiesOpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Radebatz\OpenApi\Verifier;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

trait VerifiesOpenApi
{
Expand Down
5 changes: 3 additions & 2 deletions tests/Adapters/AbstractOpenApiResponseVerifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Radebatz\OpenApi\Verifier\Adapters\AbstractOpenApiResponseVerifier;
use Radebatz\OpenApi\Verifier\OpenApiSpecificationLoader;
use Radebatz\OpenApi\Verifier\VerifiesOpenApi;

class AbstractOpenApiResponseVerifierTest extends TestCase
final class AbstractOpenApiResponseVerifierTest extends TestCase
{
use AbstractOpenApiResponseVerifier;
use VerifiesOpenApi;
Expand All @@ -32,7 +33,7 @@ protected function getAppRoot(): string

protected function assertVerifiesUsers(): void
{
$this->assertNotNull($this->getOpenApiSpecificationLoader());
$this->assertInstanceOf(OpenApiSpecificationLoader::class, $this->getOpenApiSpecificationLoader());
$this->assertTrue($this->verifyOpenApiResponseBody(
'get',
'/users',
Expand Down
14 changes: 8 additions & 6 deletions tests/Adapters/ClientAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Radebatz\OpenApi\Verifier\OpenApiSchemaMismatchException;
use Radebatz\OpenApi\Verifier\OpenApiVerifier;

class ClientAdapterTest extends TestCase
final class ClientAdapterTest extends TestCase
{
protected function verifier(): OpenApiVerifier
{
Expand Down Expand Up @@ -45,10 +45,10 @@ protected function send(callable $send, string $body, ?string $error): void
try {
/** @var ResponseInterface $response */
$response = $send();
} catch (OpenApiSchemaMismatchException $oasme) {
$this->assertStringContainsString($error, (string) $oasme->getErrorSummary());
} catch (OpenApiSchemaMismatchException $openApiSchemaMismatchException) {
$this->assertStringContainsString($error, (string) $openApiSchemaMismatchException->getErrorSummary());

throw $oasme;
throw $openApiSchemaMismatchException;
}

// verifying must leave the body for the caller to read
Expand All @@ -61,9 +61,10 @@ public function guzzleMiddleware(string $path, string $body, ?string $error): vo
{
$stack = HandlerStack::create(new MockHandler([new Response(200, ['Content-Type' => 'application/json'], $body)]));
$stack->push(new OpenApiVerifierMiddleware($this->verifier()), 'openapi-verifier');

$client = new Client(['handler' => $stack, 'base_uri' => 'http://localhost']);

$this->send(fn () => $client->get($path), $body, $error);
$this->send(fn (): ResponseInterface => $client->get($path), $body, $error);
}

#[Test]
Expand All @@ -72,6 +73,7 @@ public function guzzleMiddlewareAsync(string $path, string $body, ?string $error
{
$stack = HandlerStack::create(new MockHandler([new Response(200, ['Content-Type' => 'application/json'], $body)]));
$stack->push(new OpenApiVerifierMiddleware($this->verifier()), 'openapi-verifier');

$client = new Client(['handler' => $stack, 'base_uri' => 'http://localhost']);

$this->send(fn () => $client->getAsync($path)->wait(), $body, $error);
Expand All @@ -84,6 +86,6 @@ public function psr18Client(string $path, string $body, ?string $error): void
$inner = new Client(['handler' => HandlerStack::create(new MockHandler([new Response(200, ['Content-Type' => 'application/json'], $body)]))]);
$client = new OpenApiVerifyingClient($inner, $this->verifier());

$this->send(fn () => $client->sendRequest(new Request('GET', 'http://localhost' . $path)), $body, $error);
$this->send(fn (): ResponseInterface => $client->sendRequest(new Request('GET', 'http://localhost' . $path)), $body, $error);
}
}
10 changes: 5 additions & 5 deletions tests/Adapters/LaravelAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPUnit\Framework\Attributes\Test;
use Radebatz\OpenApi\Verifier\Adapters\Laravel\OpenApiResponseVerifier;

class LaravelAdapterTest extends TestCase
final class LaravelAdapterTest extends TestCase
{
use OpenApiResponseVerifier;

Expand All @@ -25,7 +25,7 @@ protected function setUp(): void
public function passVerificationUsers(): void
{
$this->app
->get('router')->get('/users', function () {
->get('router')->get('/users', function (): string {
return '{"data":[{"id":1,"name":"joe","email":"joe@cool.com"}]}';
});

Expand All @@ -38,7 +38,7 @@ public function passVerificationUsers(): void
public function failVerificationUsers(): void
{
$this->app
->get('router')->get('/users', function () {
->get('router')->get('/users', function (): string {
return '{"data":[{"id":1,"email":"joe@cool.com"}]}';
});

Expand All @@ -51,7 +51,7 @@ public function failVerificationUsers(): void
public function passVerificationUser(): void
{
$this->app
->get('router')->get('/user/{id}', function () {
->get('router')->get('/user/{id}', function (): string {
return '{"data":{"id":1,"name":"joe","email":"joe@cool.com"}}';
});

Expand All @@ -64,7 +64,7 @@ public function passVerificationUser(): void
public function failVerificationUser(): void
{
$this->app
->get('router')->get('/user/{id}', function () {
->get('router')->get('/user/{id}', function (): string {
return '{"data":{"id":1,"name":"joe"}}';
});

Expand Down
4 changes: 2 additions & 2 deletions tests/Adapters/Psr15AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Radebatz\OpenApi\Verifier\OpenApiSchemaMismatchException;
use Radebatz\OpenApi\Verifier\OpenApiVerifier;

class Psr15AdapterTest extends TestCase
final class Psr15AdapterTest extends TestCase
{
protected function handle(string $body, ?\Closure $onMismatch = null): ResponseInterface
{
Expand All @@ -22,7 +22,7 @@ protected function handle(string $body, ?\Closure $onMismatch = null): ResponseI
$onMismatch
);

$handler = new class($body) implements RequestHandlerInterface {
$handler = new class ($body) implements RequestHandlerInterface {
public function __construct(private readonly string $body)
{
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Adapters/SlimAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Radebatz\OpenApi\Verifier\Adapters\Slim\OpenApiResponseVerifier;
use Slim\Factory\AppFactory;

class SlimAdapterTest extends TestCase
final class SlimAdapterTest extends TestCase
{
use OpenApiResponseVerifier;

Expand All @@ -21,7 +21,7 @@ public function passVerificationUsers(): void
{
$response = $this->runApp('GET', '/users', valid: true);

$this->assertEquals(200, $response->getStatusCode());
$this->assertSame(200, $response->getStatusCode());
}

#[Test]
Expand All @@ -38,7 +38,7 @@ public function passVerificationUser(): void
{
$response = $this->runApp('GET', '/user/1', valid: true);

$this->assertEquals(200, $response->getStatusCode());
$this->assertSame(200, $response->getStatusCode());
}

#[Test]
Expand Down Expand Up @@ -68,23 +68,23 @@ protected function runApp(string $requestMethod, string $requestUri, bool $valid

// register test route as we do not have an actual app...
if ($valid) {
$app->get('/users', function (RequestInterface $request, ResponseInterface $response) {
$app->get('/users', function (RequestInterface $request, ResponseInterface $response): ResponseInterface {
$response->getBody()->write('{"data":[{"id":1,"name":"joe","email":"joe@cool.com"}]}');

return $response;
});
$app->get('/user/{id}', function (RequestInterface $request, ResponseInterface $response) {
$app->get('/user/{id}', function (RequestInterface $request, ResponseInterface $response): ResponseInterface {
$response->getBody()->write('{"data":{"id":1,"name":"joe","email":"joe@cool.com"}}');

return $response;
});
} else {
$app->get('/users', function (RequestInterface $request, ResponseInterface $response) {
$app->get('/users', function (RequestInterface $request, ResponseInterface $response): ResponseInterface {
$response->getBody()->write('{"data":[{"id":1,"email":"joe@cool.com"}]}');

return $response;
});
$app->get('/user/{id}', function (RequestInterface $request, ResponseInterface $response) {
$app->get('/user/{id}', function (RequestInterface $request, ResponseInterface $response): ResponseInterface {
$response->getBody()->write('{"data":{"id":1,"name":"joe"}}');

return $response;
Expand Down
2 changes: 1 addition & 1 deletion tests/OpenApiSchemaMismatchExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Radebatz\OpenApi\Verifier\OpenApiSchemaMismatchException;
use Radebatz\OpenApi\Verifier\Schema\SchemaError;

class OpenApiSchemaMismatchExceptionTest extends TestCase
final class OpenApiSchemaMismatchExceptionTest extends TestCase
{
#[Test]
public function getErrorsReturnsSetData(): void
Expand Down
Loading
Loading