16
16
use ApiPlatform \Core \Bridge \Symfony \Validator \EventListener \ValidationExceptionListener ;
17
17
use ApiPlatform \Core \Bridge \Symfony \Validator \Exception \ValidationException ;
18
18
use PHPUnit \Framework \TestCase ;
19
+ use Prophecy \Argument ;
19
20
use Symfony \Component \HttpFoundation \Request ;
20
21
use Symfony \Component \HttpFoundation \Response ;
21
22
use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
@@ -41,19 +42,27 @@ public function testNotValidationException()
41
42
42
43
public function testValidationException ()
43
44
{
45
+ $ exceptionJson = '{"foo": "bar"} ' ;
44
46
$ list = new ConstraintViolationList ([]);
45
47
46
48
$ eventProphecy = $ this ->prophesize (GetResponseForExceptionEvent::class);
47
49
$ eventProphecy ->getException ()->willReturn (new ValidationException ($ list ))->shouldBeCalled ();
48
50
$ 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 ();
54
63
55
64
$ serializerProphecy = $ this ->prophesize (SerializerInterface::class);
56
- $ serializerProphecy ->serialize ($ list , 'hydra ' )->willReturn (' {"foo": "bar"} ' )->shouldBeCalled ();
65
+ $ serializerProphecy ->serialize ($ list , 'hydra ' )->willReturn ($ exceptionJson )->shouldBeCalled ();
57
66
58
67
$ listener = new ValidationExceptionListener ($ serializerProphecy ->reveal (), ['hydra ' => ['application/ld+json ' ]]);
59
68
$ listener ->onKernelException ($ eventProphecy ->reveal ());
0 commit comments