Skip to content

Commit

Permalink
Merge pull request #47 from swagindustries/feature/add-json-api-header
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- authored Nov 26, 2021
2 parents 22ad2fd + 0afac11 commit 771334f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Add json-api header in responses

### Changed
- Removed hard dependency to the form component (it is still required for many features)

Expand Down
18 changes: 11 additions & 7 deletions src/Response/Listener/SerializeOnKernelView.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class SerializeOnKernelView implements EventSubscriberInterface
{
public const CONTENT_TYPE = 'application/vnd.api+json';

/**
* @var SerializerInterface
*/
Expand Down Expand Up @@ -49,13 +51,15 @@ public function onKernelView(ViewEvent $event)

$context = $this->contextBuilderChain->buildContext([], $response);

$event->setResponse(
new JsonResponse(
$this->serializer->serialize($response, 'json', $context),
$response->httpStatus(),
$response->headers(),
true
)
$jsonResponse = new JsonResponse(
$this->serializer->serialize($response, 'json', $context),
$response->httpStatus(),
$response->headers(),
true
);

$jsonResponse->headers->set('Content-Type', self::CONTENT_TYPE);

$event->setResponse($jsonResponse);
}
}
57 changes: 45 additions & 12 deletions tests/Melodiia/Response/Listener/SerializeOnKernelViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
Expand All @@ -29,19 +30,34 @@ class SerializeOnKernelViewTest extends TestCase
/** @var SerializeOnKernelView */
private $listener;

/** @var ApiResponse */
private $dummyResponse;

public function setUp(): void
{
$this->serializer = $this->prophesize(SerializerInterface::class);
$this->contextChain = $this->prophesize(ContextBuilderChainInterface::class);
$this->contextChain->buildContext(Argument::cetera())->willReturn([]);
$this->listener = new SerializeOnKernelView($this->serializer->reveal(), $this->contextChain->reveal());
$this->dummyResponse = new class() implements ApiResponse {
public function httpStatus(): int
{
return 200;
}

public function headers(): array
{
return [];
}
};
}

public function tearDown(): void
{
$this->serializer = null;
$this->contextChain = null;
$this->listener = null;
$this->dummyResponse = null;
}

public function testItSubscribeOnKernelView()
Expand All @@ -52,27 +68,44 @@ public function testItSubscribeOnKernelView()

public function testItTransformApiResponse()
{
$response = new class() implements ApiResponse {
public function httpStatus(): int
{
return 200;
}
$this->serializer->serialize($this->dummyResponse, Argument::cetera())->shouldBeCalled()->willReturn('"hello"');
$event = new ViewEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
$this->prophesize(Request::class)->reveal(),
HttpKernelInterface::MASTER_REQUEST,
$this->dummyResponse
);

public function headers(): array
{
return [];
}
};
$this->serializer->serialize($response, Argument::cetera())->shouldBeCalled()->willReturn('"hello"');
$this->listener->onKernelView($event);

$this->assertInstanceOf(JsonResponse::class, $event->getResponse());
}

public function testItDoesNotTransformSymfonyResponse()
{
$response = new Response('<h1>Containing HTML?</h1>');
$event = new ViewEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
$this->prophesize(Request::class)->reveal(),
HttpKernelInterface::MASTER_REQUEST,
$response
);
$this->listener->onKernelView($event);
$this->assertSame(null, $event->getResponse());
}

public function testItSendsTheRightContentType()
{
$this->serializer->serialize($this->dummyResponse, Argument::cetera())->shouldBeCalled()->willReturn('"hello"');
$event = new ViewEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
$this->prophesize(Request::class)->reveal(),
HttpKernelInterface::MASTER_REQUEST,
$this->dummyResponse
);

$this->listener->onKernelView($event);

$this->assertInstanceOf(JsonResponse::class, $event->getResponse());
$this->assertEquals($event->getResponse()->headers->get('content-type'), SerializeOnKernelView::CONTENT_TYPE);
}
}

0 comments on commit 771334f

Please sign in to comment.