diff --git a/composer.json b/composer.json index fd70bfb..03001c2 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,12 @@ "license": "MPL-2.0", "autoload": { "psr-4": { - "Membrane\\Laravel\\": "src/", + "Membrane\\Laravel\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Membrane\\Laravel\\Tests\\": "tests/", "Membrane\\Laravel\\Fixtures\\": "tests/fixtures/" } }, diff --git a/phpcs.xml b/phpcs.xml index 73969c8..b512276 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,11 +1,12 @@ - PHPCS configuration file. - src + PHPCS configuration file. + src + tests - */vendor/* + */vendor/* - - + + diff --git a/tests/ApiProblemBuilderTest.php b/tests/ApiProblemBuilderTest.php index fb58186..f90e87e 100644 --- a/tests/ApiProblemBuilderTest.php +++ b/tests/ApiProblemBuilderTest.php @@ -2,8 +2,10 @@ declare(strict_types=1); -namespace Membrane\Laravel; +namespace Membrane\Laravel\Tests; +use Membrane\Laravel\ApiProblemBuilder; +use Membrane\Laravel\ToSymfony; use Membrane\OpenAPI\Exception\CannotProcessSpecification; use Membrane\Renderer\Renderer; use PHPUnit\Framework\Attributes\CoversClass; @@ -17,7 +19,6 @@ #[UsesClass(ToSymfony::class)] class ApiProblemBuilderTest extends TestCase { - #[Test] public function buildFromRendererTest(): void { @@ -48,7 +49,12 @@ public static function dataSetsToBuildFromException(): array 'path not found, no apiResponseTypes' => [ CannotProcessSpecification::pathNotFound('api.json', '/pets'), new SymfonyResponse( - '{"title":"Not Found","type":"about:blank","status":404,"detail":"\/pets does not match any specified paths in api.json"}', + json_encode([ + 'title' => 'Not Found', + 'type' => 'about:blank', + 'status' => 404, + 'detail' => '/pets does not match any specified paths in api.json', + ]), 404, ['Content-Type' => 'application/problem+json'] ), @@ -57,7 +63,12 @@ public static function dataSetsToBuildFromException(): array 'path not found, no applicable apiResponseType' => [ CannotProcessSpecification::pathNotFound('api.json', '/pets'), new SymfonyResponse( - '{"title":"Not Found","type":"about:blank","status":404,"detail":"\/pets does not match any specified paths in api.json"}', + json_encode([ + 'title' => 'Not Found', + 'type' => 'about:blank', + 'status' => 404, + 'detail' => '/pets does not match any specified paths in api.json', + ]), 404, ['Content-Type' => 'application/problem+json'] ), @@ -66,7 +77,12 @@ public static function dataSetsToBuildFromException(): array 'path not found, applicable apiResponseType' => [ CannotProcessSpecification::pathNotFound('api.json', '/pets'), new SymfonyResponse( - '{"title":"Not Found","type":"Path Not Found","status":404,"detail":"\/pets does not match any specified paths in api.json"}', + json_encode([ + 'title' => 'Not Found', + 'type' => 'Path Not Found', + 'status' => 404, + 'detail' => '/pets does not match any specified paths in api.json', + ]), 404, ['Content-Type' => 'application/problem+json'] ), @@ -75,11 +91,20 @@ public static function dataSetsToBuildFromException(): array 'method not found, applicable apiResponseType' => [ CannotProcessSpecification::methodNotFound('get'), new SymfonyResponse( - '{"title":"Method Not Allowed","type":"Method Not Found","status":405,"detail":"get operation not specified on path"}', + json_encode([ + 'title' => 'Method Not Allowed', + 'type' => 'Method Not Found', + 'status' => 405, + 'detail' => 'get operation not specified on path', + ]), 405, ['Content-Type' => 'application/problem+json'] ), - [404 => 'Path Not Found', 405 => 'Method Not Found', 418 => 'I\'m a teapot'], + [ + 404 => 'Path Not Found', + 405 => 'Method Not Found', + 418 => 'I\'m a teapot', + ], ], ]; } @@ -98,7 +123,9 @@ public function buildFromExceptionTest( self::assertSame($expected->getContent(), $actual->getContent()); self::assertSame($expected->getStatusCode(), $actual->getStatusCode()); - self::assertSame($expected->headers->get('Content-Type'), $actual->headers->get('Content-Type')); + self::assertSame( + $expected->headers->get('Content-Type'), + $actual->headers->get('Content-Type') + ); } - } diff --git a/tests/Middleware/RequestValidationTest.php b/tests/Middleware/RequestValidationTest.php index b4b6026..4d5a2ba 100644 --- a/tests/Middleware/RequestValidationTest.php +++ b/tests/Middleware/RequestValidationTest.php @@ -2,12 +2,13 @@ declare(strict_types=1); -namespace Membrane\Laravel\Middleware; +namespace Membrane\Laravel\Tests\Middleware; use Illuminate\Contracts\Container\Container; use Illuminate\Http\Request; use Illuminate\Http\Response; use Membrane\Laravel\ApiProblemBuilder; +use Membrane\Laravel\Middleware\RequestValidation; use Membrane\Laravel\ToPsr7; use Membrane\Laravel\ToSymfony; use Membrane\OpenAPI\Exception\CannotProcessSpecification; @@ -19,7 +20,6 @@ use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; - #[CoversClass(RequestValidation::class)] #[UsesClass(ApiProblemBuilder::class)] #[UsesClass(ToPsr7::class)] @@ -31,18 +31,21 @@ public function registersResultInstanceInContainer(): void { $url = '/pets?limit=5&tags[]=cat&tags[]=tabby'; $expected = Result::valid([ - 'path' => [], - 'query' => ['limit' => 5, 'tags' => ['cat', 'tabby']], - 'header' => [], - 'cookie' => [], - 'body' => '', - 'request' => ['method' => 'get', 'operationId' => 'findPets'], - ] - ); + 'path' => [], + 'query' => ['limit' => 5, 'tags' => ['cat', 'tabby']], + 'header' => [], + 'cookie' => [], + 'body' => '', + 'request' => ['method' => 'get', 'operationId' => 'findPets'], + ]); $apiProblemBuilder = self::createStub(ApiProblemBuilder::class); $container = self::createMock(Container::class); - $sut = new RequestValidation(__DIR__ . '/../fixtures/petstore-expanded.json', $apiProblemBuilder, $container); + $sut = new RequestValidation( + __DIR__ . '/../fixtures/petstore-expanded.json', + $apiProblemBuilder, + $container + ); $container->expects(self::once()) ->method('instance') @@ -57,17 +60,21 @@ public static function dataSetsThatThrowCannotProcessSpecification(): array 'path not found' => [ '/hats', Method::GET, - CannotProcessSpecification::pathNotFound('petstore-expanded.json', '/hats'), + CannotProcessSpecification::pathNotFound( + 'petstore-expanded.json', + '/hats' + ), ], 'method not found' => [ '/pets', Method::DELETE, - CannotProcessSpecification::methodNotFound(Method::DELETE->value), + CannotProcessSpecification::methodNotFound( + Method::DELETE->value + ), ], ]; } - #[Test] #[DataProvider('dataSetsThatThrowCannotProcessSpecification')] public function catchesCannotProcessSpecification( @@ -87,7 +94,9 @@ public function catchesCannotProcessSpecification( ->method('buildFromException') ->with($expected); - $sut->handle(Request::create($path, $method->value), fn($p) => new Response()); + $sut->handle( + Request::create($path, $method->value), + fn($p) => new Response() + ); } - } diff --git a/tests/Middleware/ResponseJsonFlatTest.php b/tests/Middleware/ResponseJsonFlatTest.php index 8f61f6d..ddca147 100644 --- a/tests/Middleware/ResponseJsonFlatTest.php +++ b/tests/Middleware/ResponseJsonFlatTest.php @@ -2,11 +2,12 @@ declare(strict_types=1); -namespace Membrane\Laravel\Middleware; +namespace Membrane\Laravel\Tests\Middleware; use Illuminate\Contracts\Container\Container; use Illuminate\Http\Request; use Membrane\Laravel\ApiProblemBuilder; +use Membrane\Laravel\Middleware\ResponseJsonFlat; use Membrane\Laravel\ToSymfony; use Membrane\Result\FieldName; use Membrane\Result\Message; @@ -19,7 +20,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; - #[CoversClass(ResponseJsonFlat::class)] #[UsesClass(ApiProblemBuilder::class)] #[UsesClass(ToSymfony::class)] @@ -35,11 +35,25 @@ public static function dataSetsToHandle(): array 'invalid result returns response with ApiProblem' => [ Result::invalid( 1, - new MessageSet(new FieldName('id', 'pet'), new Message('must be an integer', [])), - new MessageSet(new FieldName('pet'), new Message('%s is a required field', ['name'])) + new MessageSet( + new FieldName('id', 'pet'), + new Message('must be an integer', []) + ), + new MessageSet( + new FieldName('pet'), + new Message('%s is a required field', ['name']) + ) ), (new SymfonyResponse( - content: '{"errors":{"pet->id":["must be an integer"],"pet":["name is a required field"]},"title":"Request payload failed validation","type":"about:blank","status":400}', + content: json_encode([ + 'errors' => [ + 'pet->id' => ['must be an integer'], + 'pet' => ['name is a required field'], + ], + 'title' => 'Request payload failed validation', + 'type' => 'about:blank', + 'status' => 400, + ]), status: 400, headers: ['Content-Type' => ['application/problem+json']], ))->setProtocolVersion('1.1'), diff --git a/tests/Middleware/ResponseJsonNestedTest.php b/tests/Middleware/ResponseJsonNestedTest.php index 613de06..a1fae4b 100644 --- a/tests/Middleware/ResponseJsonNestedTest.php +++ b/tests/Middleware/ResponseJsonNestedTest.php @@ -2,11 +2,12 @@ declare(strict_types=1); -namespace Membrane\Laravel\Middleware; +namespace Membrane\Laravel\Tests\Middleware; use Illuminate\Contracts\Container\Container; use Illuminate\Http\Request; use Membrane\Laravel\ApiProblemBuilder; +use Membrane\Laravel\Middleware\ResponseJsonNested; use Membrane\Laravel\ToSymfony; use Membrane\Result\FieldName; use Membrane\Result\Message; @@ -19,7 +20,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; - #[CoversClass(ResponseJsonNested::class)] #[UsesClass(ApiProblemBuilder::class)] #[UsesClass(ToSymfony::class)] @@ -35,11 +35,35 @@ public static function dataSetsToHandle(): array 'invalid result returns response with ApiProblem' => [ Result::invalid( 1, - new MessageSet(new FieldName('id', 'pet'), new Message('must be an integer', [])), - new MessageSet(new FieldName('pet'), new Message('%s is a required field', ['name'])) + new MessageSet( + new FieldName('id', 'pet'), + new Message('must be an integer', []) + ), + new MessageSet( + new FieldName('pet'), + new Message('%s is a required field', ['name']) + ) ), (new SymfonyResponse( - content: '{"errors":{"errors":[],"fields":{"pet":{"errors":["name is a required field"],"fields":{"id":{"errors":["must be an integer"],"fields":[]}}}}},"title":"Request payload failed validation","type":"about:blank","status":400}', + content: json_encode([ + 'errors' => [ + 'errors' => [], + 'fields' => [ + 'pet' => [ + 'errors' => ['name is a required field'], + 'fields' => [ + 'id' => [ + 'errors' => ['must be an integer'], + 'fields' => [], + ], + ], + ], + ], + ], + 'title' => 'Request payload failed validation', + 'type' => 'about:blank', + 'status' => 400, + ]), status: 400, headers: ['Content-Type' => ['application/problem+json']], ))->setProtocolVersion('1.1'), diff --git a/tests/ToPsr7Test.php b/tests/ToPsr7Test.php index f4e9238..4945e36 100644 --- a/tests/ToPsr7Test.php +++ b/tests/ToPsr7Test.php @@ -2,20 +2,20 @@ declare(strict_types=1); -namespace Membrane\Laravel; +namespace Membrane\Laravel\Tests; use Illuminate\Http\Request; +use Membrane\Laravel\ToPsr7; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; -/** - * @covers \Membrane\Laravel\ToPsr7 - */ +#[CoversClass(ToPsr7::class)] class ToPsr7Test extends TestCase { - - /** @test */ - public function invokeTurnsLaravelRequestIntoPsrRequest(): void + #[Test] + public function itConvertsLaravelRequestIntoPsrRequest(): void { $sut = new ToPsr7(); $request = Request::create('/pets/1'); @@ -26,5 +26,4 @@ public function invokeTurnsLaravelRequestIntoPsrRequest(): void self::assertSame('/pets/1', $actual->getUri()->getPath()); self::assertSame('GET', $actual->getMethod()); } - } diff --git a/tests/ToSymfonyTest.php b/tests/ToSymfonyTest.php index 70bc55f..0b8f530 100644 --- a/tests/ToSymfonyTest.php +++ b/tests/ToSymfonyTest.php @@ -2,18 +2,19 @@ declare(strict_types=1); +namespace Membrane\Laravel\Tests; use Membrane\Laravel\ToSymfony; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; -/** - * @covers \Membrane\Laravel\ToSymfony - */ +#[CoversClass(ToSymfony::class)] class ToSymfonyTest extends TestCase { - /** @test */ - public function invokeTest(): void + #[Test] + public function itConvertsPsrResponseToSymfonyResponse(): void { $sut = new ToSymfony(); $request = new \Nyholm\Psr7\Response();