1515use Symfony \Component \HttpFoundation \JsonResponse ;
1616use Symfony \Component \HttpFoundation \Request ;
1717use Symfony \Component \HttpFoundation \Response ;
18- use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
18+ use Symfony \Component \HttpKernel \Event \ExceptionEvent ;
19+ use Symfony \Component \HttpKernel \HttpKernelInterface ;
1920
2021/** @covers \Phpro\ApiProblemBundle\EventListener\JsonApiProblemExceptionListener */
2122class JsonApiProblemExceptionListenerTest extends TestCase
@@ -38,9 +39,9 @@ class JsonApiProblemExceptionListenerTest extends TestCase
3839 protected function setUp (): void
3940 {
4041 $ this ->request = $ this ->prophesize (Request::class);
41- $ this -> event = $ this ->prophesize (GetResponseForExceptionEvent ::class);
42- $ this -> event -> getRequest ()-> willReturn ( $ this -> request );
43- $ this ->event -> getException ()-> willReturn ( new \ Exception ( ' error ' ) );
42+ $ httpKernel = $ this ->prophesize (HttpKernelInterface ::class);
43+ $ exception = new \ Exception ( ' error ' );
44+ $ this ->event = new ExceptionEvent ( $ httpKernel -> reveal (), $ this -> request -> reveal (), HttpKernelInterface:: MASTER_REQUEST , $ exception );
4445 $ this ->exceptionTransformer = $ this ->prophesize (ExceptionTransformerInterface::class);
4546 $ this ->exceptionTransformer ->accepts (Argument::any ())->willReturn (false );
4647 }
@@ -51,10 +52,9 @@ public function it_does_nothing_on_non_json_requests(): void
5152 $ listener = new JsonApiProblemExceptionListener ($ this ->exceptionTransformer ->reveal (), false );
5253 $ this ->request ->getRequestFormat ()->willReturn ('html ' );
5354 $ this ->request ->getContentType ()->willReturn ('text/html ' );
55+ $ listener ->onKernelException ($ this ->event );
5456
55- $ this ->event ->setResponse (Argument::any ())->shouldNotBeCalled ();
56-
57- $ listener ->onKernelException ($ this ->event ->reveal ());
57+ $ this ->assertNull ($ this ->event ->getResponse ());
5858 }
5959
6060 /** @test */
@@ -63,10 +63,9 @@ public function it_runs_on_json_route_formats(): void
6363 $ listener = new JsonApiProblemExceptionListener ($ this ->exceptionTransformer ->reveal (), false );
6464 $ this ->request ->getRequestFormat ()->willReturn ('json ' );
6565 $ this ->request ->getContentType ()->willReturn (null );
66+ $ listener ->onKernelException ($ this ->event );
6667
67- $ this ->event ->setResponse (Argument::type (JsonResponse::class))->shouldBeCalled ();
68-
69- $ listener ->onKernelException ($ this ->event ->reveal ());
68+ $ this ->assertNotNull ($ this ->event ->getResponse ());
7069 }
7170
7271 /** @test */
@@ -75,10 +74,9 @@ public function it_runs_on_json_content_types(): void
7574 $ listener = new JsonApiProblemExceptionListener ($ this ->exceptionTransformer ->reveal (), false );
7675 $ this ->request ->getRequestFormat ()->willReturn ('html ' );
7776 $ this ->request ->getContentType ()->willReturn ('application/json ' );
77+ $ listener ->onKernelException ($ this ->event );
7878
79- $ this ->event ->setResponse (Argument::type (JsonResponse::class))->shouldBeCalled ();
80-
81- $ listener ->onKernelException ($ this ->event ->reveal ());
79+ $ this ->assertNotNull ($ this ->event ->getResponse ());
8280 }
8381
8482 /** @test */
@@ -88,18 +86,23 @@ public function it_parses_an_api_problem_response(): void
8886 $ this ->request ->getRequestFormat ()->willReturn ('json ' );
8987 $ this ->request ->getContentType ()->willReturn ('application/json ' );
9088
91- $ this ->event ->setResponse (Argument::that (function (JsonResponse $ response ) {
92- return 500 === $ response ->getStatusCode ()
93- && 'application/problem+json ' === $ response ->headers ->get ('Content-Type ' )
94- && $ response ->getContent () === json_encode ([
95- 'status ' => 500 ,
96- 'type ' => HttpApiProblem::TYPE_HTTP_RFC ,
97- 'title ' => HttpApiProblem::getTitleForStatusCode (500 ),
98- 'detail ' => 'error ' ,
99- ]);
100- }))->shouldBeCalled ();
101-
102- $ listener ->onKernelException ($ this ->event ->reveal ());
89+ $ this ->event ->setResponse (new JsonResponse ([
90+ 'status ' => 500 ,
91+ 'type ' => HttpApiProblem::TYPE_HTTP_RFC ,
92+ 'title ' => HttpApiProblem::getTitleForStatusCode (500 ),
93+ 'detail ' => 'error ' ,
94+ ]));
95+
96+ $ listener ->onKernelException ($ this ->event );
97+ $ this ->assertJsonStringEqualsJsonString (
98+ json_encode ([
99+ 'status ' => 500 ,
100+ 'type ' => HttpApiProblem::TYPE_HTTP_RFC ,
101+ 'title ' => HttpApiProblem::getTitleForStatusCode (500 ),
102+ 'detail ' => 'error ' ,
103+ ]),
104+ $ this ->event ->getResponse ()->getContent ()
105+ );
103106 }
104107
105108 /** @test */
@@ -114,14 +117,10 @@ public function it_uses_an_exception_transformer(): void
114117
115118 $ this ->exceptionTransformer ->accepts (Argument::type (\Exception::class))->willReturn (true );
116119 $ this ->exceptionTransformer ->transform (Argument::type (\Exception::class))->willReturn ($ apiProblem ->reveal ());
120+ $ this ->event ->setResponse (new Response (json_encode ([]), Response::HTTP_BAD_REQUEST , ['Content-Type ' => 'application/problem+json ' ]));
117121
118- $ this ->event ->setResponse (Argument::that (function (JsonResponse $ response ) {
119- return Response::HTTP_BAD_REQUEST === $ response ->getStatusCode ()
120- && 'application/problem+json ' === $ response ->headers ->get ('Content-Type ' )
121- && $ response ->getContent () === json_encode ([]);
122- }))->shouldBeCalled ();
123-
124- $ listener ->onKernelException ($ this ->event ->reveal ());
122+ $ listener ->onKernelException ($ this ->event );
123+ $ this ->assertInstanceOf (JsonResponse::class, $ this ->event ->getResponse ());
125124 }
126125
127126 /** @test */
@@ -135,19 +134,15 @@ public function it_parses_a_debuggable_api_problem_response(): void
135134 $ apiProblem ->toArray ()->willReturn ($ data );
136135 $ exception = new \RuntimeException ();
137136
138- $ this ->event ->getException ()->willReturn ($ exception );
139137 $ this ->exceptionTransformer ->accepts ($ exception )->willReturn (true );
140138 $ this ->exceptionTransformer ->transform ($ exception )->willReturn ($ apiProblem ->reveal ());
141139
142140 $ this ->request ->getRequestFormat ()->willReturn ('json ' );
143141 $ this ->request ->getContentType ()->willReturn ('application/json ' );
144142
145- $ this ->event ->setResponse (Argument::that (function (JsonResponse $ response ) use ($ data ) {
146- return 500 === $ response ->getStatusCode ()
147- && 'application/problem+json ' === $ response ->headers ->get ('Content-Type ' )
148- && $ response ->getContent () === json_encode ($ data );
149- }))->shouldBeCalled ();
143+ $ this ->event ->setResponse (new JsonResponse ($ data , 500 , ['Content-Type ' => 'application/problem+json ' ]));
150144
151- $ listener ->onKernelException ($ this ->event ->reveal ());
145+ $ listener ->onKernelException ($ this ->event );
146+ $ this ->assertStringContainsString ('JsonApiProblemExceptionListenerTest ' , $ this ->event ->getResponse ()->getContent ());
152147 }
153148}
0 commit comments