From 46027dfad5d7e3721bbb38d7c7e755cf46770036 Mon Sep 17 00:00:00 2001 From: John Charman Date: Thu, 15 Dec 2022 11:33:38 +0000 Subject: [PATCH] Tests: updated to reflect changes in middleware --- tests/ApiProblemBuilderTest.php | 22 ++++++++++------- tests/Middleware/ResponseJsonFlatTest.php | 27 +++++++++++++-------- tests/Middleware/ResponseJsonNestedTest.php | 27 +++++++++++++-------- 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/tests/ApiProblemBuilderTest.php b/tests/ApiProblemBuilderTest.php index 9818d00..324999b 100644 --- a/tests/ApiProblemBuilderTest.php +++ b/tests/ApiProblemBuilderTest.php @@ -5,11 +5,11 @@ namespace Membrane\Laravel; use Membrane\Renderer\Renderer; -use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; /** * @covers \Membrane\Laravel\ApiProblemBuilder + * @uses \Membrane\Laravel\ToSymfony */ class ApiProblemBuilderTest extends TestCase { @@ -17,21 +17,25 @@ class ApiProblemBuilderTest extends TestCase /** @test */ public function buildsApiProblemImplementingServerResponseInterface(): void { - $expected = new Response( - status: 400, - headers: ['Content-Type' => ['application/problem+json']], - body: '{"errors":{"id":["error message"]}', - ); + $expected = [ + 'errors' => [ + 'id' => ['must be an integer'], + ], + 'title' => 'Request payload failed validation', + 'status' => 400, + 'type' => 'about:blank', - $sut = new ApiProblemBuilder(); + ]; + + $sut = new ApiProblemBuilder(400, 'about:blank'); $renderer = self::createMock(Renderer::class); $renderer->expects(self::once()) ->method('jsonSerialize') - ->willReturn('{"errors":{"id":["error message"]},"title":"Request payload fail'); + ->willReturn(['id' => ['must be an integer']]); $actual = $sut->build($renderer); - self::assertEquals($expected, $actual); + self::assertEquals($expected, json_decode($actual->getContent(), true)); } } diff --git a/tests/Middleware/ResponseJsonFlatTest.php b/tests/Middleware/ResponseJsonFlatTest.php index 030cbb4..816687c 100644 --- a/tests/Middleware/ResponseJsonFlatTest.php +++ b/tests/Middleware/ResponseJsonFlatTest.php @@ -6,16 +6,18 @@ use Illuminate\Contracts\Container\Container; use Illuminate\Http\Request; -use Illuminate\Http\Response; +use Membrane\Laravel\ApiProblemBuilder; use Membrane\Result\FieldName; use Membrane\Result\Message; use Membrane\Result\MessageSet; use Membrane\Result\Result; use PHPUnit\Framework\TestCase; -use Psr\Http\Message\ResponseInterface; +use Symfony\Component\HttpFoundation\Response as SymfonyResponse; /** * @covers \Membrane\Laravel\Middleware\ResponseJsonFlat + * @uses \Membrane\Laravel\ApiProblemBuilder + * @uses \Membrane\Laravel\ToSymfony */ class ResponseJsonFlatTest extends TestCase { @@ -24,15 +26,19 @@ public function dataSetsToHandle(): array return [ 'valid results return valid responses' => [ Result::valid(1), - new Response(), + new SymfonyResponse(), ], 'invalid result returns response with ApiProblem' => [ - Result::invalid(1, new MessageSet(new FieldName('id'), new Message('error message', []))), - new \Nyholm\Psr7\Response( + 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 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}', status: 400, headers: ['Content-Type' => ['application/problem+json']], - body: '{"errors":{"id":["error message"]}', - ), + ))->setProtocolVersion('1.1'), ], ]; } @@ -41,18 +47,19 @@ public function dataSetsToHandle(): array * @test * @dataProvider dataSetsToHandle */ - public function handleTest(Result $result, Response|ResponseInterface $expected): void + public function handleTest(Result $result, SymfonyResponse $expected): void { $request = self::createStub(Request::class); $container = self::createMock(Container::class); - $sut = new ResponseJsonFlat($container); + $apiProblemBuilder = new ApiProblemBuilder(400, 'about:blank'); + $sut = new ResponseJsonFlat($container, $apiProblemBuilder); $container->expects(self::once()) ->method('get') ->with(Result::class) ->willReturn($result); - $actual = $sut->handle($request, fn($var) => new Response()); + $actual = $sut->handle($request, fn($var) => new SymfonyResponse()); self::assertEquals($expected, $actual); } diff --git a/tests/Middleware/ResponseJsonNestedTest.php b/tests/Middleware/ResponseJsonNestedTest.php index 640d64e..f22b9f1 100644 --- a/tests/Middleware/ResponseJsonNestedTest.php +++ b/tests/Middleware/ResponseJsonNestedTest.php @@ -6,16 +6,18 @@ use Illuminate\Contracts\Container\Container; use Illuminate\Http\Request; -use Illuminate\Http\Response; +use Membrane\Laravel\ApiProblemBuilder; use Membrane\Result\FieldName; use Membrane\Result\Message; use Membrane\Result\MessageSet; use Membrane\Result\Result; use PHPUnit\Framework\TestCase; -use Psr\Http\Message\ResponseInterface; +use Symfony\Component\HttpFoundation\Response as SymfonyResponse; /** * @covers \Membrane\Laravel\Middleware\ResponseJsonNested + * @uses \Membrane\Laravel\ApiProblemBuilder + * @uses \Membrane\Laravel\ToSymfony */ class ResponseJsonNestedTest extends TestCase { @@ -24,15 +26,19 @@ public function dataSetsToHandle(): array return [ 'valid results return valid responses' => [ Result::valid(1), - new Response(), + new SymfonyResponse(), ], 'invalid result returns response with ApiProblem' => [ - Result::invalid(1, new MessageSet(new FieldName('id'), new Message('error message', []))), - new \Nyholm\Psr7\Response( + 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 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}', status: 400, headers: ['Content-Type' => ['application/problem+json']], - body: '{"errors":{"id":["error message"]}', - ), + ))->setProtocolVersion('1.1'), ], ]; } @@ -41,18 +47,19 @@ public function dataSetsToHandle(): array * @test * @dataProvider dataSetsToHandle */ - public function handleTest(Result $result, Response|ResponseInterface $expected): void + public function handleTest(Result $result, SymfonyResponse $expected): void { $request = self::createStub(Request::class); $container = self::createMock(Container::class); - $sut = new ResponseJsonNested($container); + $apiProblemBuilder = new ApiProblemBuilder(400, 'about:blank'); + $sut = new ResponseJsonNested($container, $apiProblemBuilder); $container->expects(self::once()) ->method('get') ->with(Result::class) ->willReturn($result); - $actual = $sut->handle($request, fn($var) => new Response()); + $actual = $sut->handle($request, fn($var) => new SymfonyResponse()); self::assertEquals($expected, $actual); }