Skip to content

Commit b85acb7

Browse files
committed
Fix ValidationExceptionListenerTest
It was failing randomly because of the time-sensitive Date response header
1 parent e75a93d commit b85acb7

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

tests/Bridge/Symfony/Validator/EventListener/ValidationExceptionListenerTest.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Core\Bridge\Symfony\Validator\EventListener\ValidationExceptionListener;
1717
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
1818
use PHPUnit\Framework\TestCase;
19+
use Prophecy\Argument;
1920
use Symfony\Component\HttpFoundation\Request;
2021
use Symfony\Component\HttpFoundation\Response;
2122
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
@@ -41,19 +42,27 @@ public function testNotValidationException()
4142

4243
public function testValidationException()
4344
{
45+
$exceptionJson = '{"foo": "bar"}';
4446
$list = new ConstraintViolationList([]);
4547

4648
$eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class);
4749
$eventProphecy->getException()->willReturn(new ValidationException($list))->shouldBeCalled();
4850
$eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled();
49-
$eventProphecy->setResponse(new Response('{"foo": "bar"}', Response::HTTP_BAD_REQUEST, [
50-
'Content-Type' => 'application/ld+json; charset=utf-8',
51-
'X-Content-Type-Options' => 'nosniff',
52-
'X-Frame-Options' => 'deny',
53-
]))->shouldBeCalled();
51+
$eventProphecy->setResponse(Argument::allOf(
52+
Argument::type(Response::class),
53+
Argument::which('getContent', $exceptionJson),
54+
Argument::which('getStatusCode', Response::HTTP_BAD_REQUEST),
55+
Argument::that(function (Response $response): bool {
56+
return
57+
'application/ld+json; charset=utf-8' === $response->headers->get('Content-Type')
58+
&& 'nosniff' === $response->headers->get('X-Content-Type-Options')
59+
&& 'deny' === $response->headers->get('X-Frame-Options')
60+
;
61+
})
62+
))->shouldBeCalled();
5463

5564
$serializerProphecy = $this->prophesize(SerializerInterface::class);
56-
$serializerProphecy->serialize($list, 'hydra')->willReturn('{"foo": "bar"}')->shouldBeCalled();
65+
$serializerProphecy->serialize($list, 'hydra')->willReturn($exceptionJson)->shouldBeCalled();
5766

5867
$listener = new ValidationExceptionListener($serializerProphecy->reveal(), ['hydra' => ['application/ld+json']]);
5968
$listener->onKernelException($eventProphecy->reveal());

0 commit comments

Comments
 (0)