Skip to content

Commit a112a45

Browse files
author
Mokhtar Tlili
committed
enhance phpunit tests
1 parent e4bdbf9 commit a112a45

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

tests/EventListener/ExceptionListenerTest.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Prophecy\Prophecy\ObjectProphecy;
910
use Sfmok\RequestInput\EventListener\ExceptionListener;
1011
use Sfmok\RequestInput\Exception\ValidationException;
1112
use Symfony\Component\HttpFoundation\Request;
@@ -19,17 +20,25 @@ class ExceptionListenerTest extends TestCase
1920
{
2021
use ProphecyTrait;
2122

22-
public function testOnKernelException(): void
23+
private ObjectProphecy $serializer;
24+
private ObjectProphecy $httpKernel;
25+
26+
protected function setUp(): void
27+
{
28+
$this->serializer = $this->prophesize(SerializerInterface::class);
29+
$this->httpKernel = $this->prophesize(HttpKernelInterface::class);
30+
}
31+
32+
public function testOnKernelExceptionWithValidationException(): void
2333
{
2434
$serializedConstraintViolationList = '{"foo": "bar"}';
2535
$list = new ConstraintViolationList([]);
2636

27-
$serializerProphecy = $this->prophesize(SerializerInterface::class);
28-
$serializerProphecy->serialize($list, 'json')->willReturn($serializedConstraintViolationList)->shouldBeCalled();
37+
$this->serializer->serialize($list, 'json')->willReturn($serializedConstraintViolationList)->shouldBeCalled();
2938

30-
$listener = new ExceptionListener($serializerProphecy->reveal());
39+
$listener = new ExceptionListener($this->serializer->reveal());
3140
$event = new ExceptionEvent(
32-
$this->prophesize(HttpKernelInterface::class)->reveal(),
41+
$this->httpKernel->reveal(),
3342
new Request(),
3443
HttpKernelInterface::MAIN_REQUEST,
3544
new ValidationException($list)
@@ -43,4 +52,19 @@ public function testOnKernelException(): void
4352
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
4453
$this->assertSame('application/problem+json; charset=utf-8', $response->headers->get('Content-Type'));
4554
}
55+
56+
public function testOnKernelExceptionWithoutValidationException(): void
57+
{
58+
$this->serializer->serialize()->shouldNotBeCalled();
59+
60+
$listener = new ExceptionListener($this->serializer->reveal());
61+
$event = new ExceptionEvent(
62+
$this->httpKernel->reveal(),
63+
new Request(),
64+
HttpKernelInterface::MAIN_REQUEST,
65+
new \Exception()
66+
);
67+
68+
$listener->onKernelException($event);
69+
}
4670
}

0 commit comments

Comments
 (0)