Skip to content

Commit

Permalink
Tests: updated to reflect changes in middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
charjr committed Dec 15, 2022
1 parent 4efe6ee commit 46027df
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
22 changes: 13 additions & 9 deletions tests/ApiProblemBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,37 @@
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
{

/** @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));
}

}
27 changes: 17 additions & 10 deletions tests/Middleware/ResponseJsonFlatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'),
],
];
}
Expand All @@ -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);
}
Expand Down
27 changes: 17 additions & 10 deletions tests/Middleware/ResponseJsonNestedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'),
],
];
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 46027df

Please sign in to comment.