6
6
7
7
use PHPUnit \Framework \TestCase ;
8
8
use Prophecy \PhpUnit \ProphecyTrait ;
9
+ use Prophecy \Prophecy \ObjectProphecy ;
9
10
use Sfmok \RequestInput \EventListener \ExceptionListener ;
10
11
use Sfmok \RequestInput \Exception \ValidationException ;
11
12
use Symfony \Component \HttpFoundation \Request ;
@@ -19,17 +20,25 @@ class ExceptionListenerTest extends TestCase
19
20
{
20
21
use ProphecyTrait;
21
22
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
23
33
{
24
34
$ serializedConstraintViolationList = '{"foo": "bar"} ' ;
25
35
$ list = new ConstraintViolationList ([]);
26
36
27
- $ serializerProphecy = $ this ->prophesize (SerializerInterface::class);
28
- $ serializerProphecy ->serialize ($ list , 'json ' )->willReturn ($ serializedConstraintViolationList )->shouldBeCalled ();
37
+ $ this ->serializer ->serialize ($ list , 'json ' )->willReturn ($ serializedConstraintViolationList )->shouldBeCalled ();
29
38
30
- $ listener = new ExceptionListener ($ serializerProphecy ->reveal ());
39
+ $ listener = new ExceptionListener ($ this -> serializer ->reveal ());
31
40
$ event = new ExceptionEvent (
32
- $ this ->prophesize (HttpKernelInterface::class) ->reveal (),
41
+ $ this ->httpKernel ->reveal (),
33
42
new Request (),
34
43
HttpKernelInterface::MAIN_REQUEST ,
35
44
new ValidationException ($ list )
@@ -43,4 +52,19 @@ public function testOnKernelException(): void
43
52
$ this ->assertSame (Response::HTTP_BAD_REQUEST , $ response ->getStatusCode ());
44
53
$ this ->assertSame ('application/problem+json; charset=utf-8 ' , $ response ->headers ->get ('Content-Type ' ));
45
54
}
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
+ }
46
70
}
0 commit comments