|
16 | 16 | use Symfony\Component\Debug\Exception\FlattenException;
|
17 | 17 | use Symfony\Component\HttpFoundation\Request;
|
18 | 18 | use Symfony\Component\HttpFoundation\Response;
|
| 19 | +use Symfony\Component\Serializer\Exception\ExceptionInterface; |
19 | 20 | use Symfony\Component\Serializer\SerializerInterface;
|
20 | 21 |
|
21 | 22 | /**
|
22 | 23 | * @author Amrouche Hamza <hamza.simperfit@gmail.com>
|
| 24 | + * @author Baptiste Meyer <baptiste.meyer@gmail.com> |
23 | 25 | */
|
24 | 26 | class ExceptionActionTest extends \PHPUnit_Framework_TestCase
|
25 | 27 | {
|
26 |
| - public function testGetException() |
| 28 | + public function testActionWithCatchableException() |
27 | 29 | {
|
28 |
| - $flattenException = $this->prophesize(FlattenException::class); |
29 |
| - $flattenException->getClass()->willReturn(InvalidArgumentException::class); |
30 |
| - $flattenException->setStatusCode(Response::HTTP_BAD_REQUEST)->willReturn(); |
31 |
| - $flattenException->getHeaders()->willReturn(['Content-Type' => 'application/problem+json']); |
| 30 | + $serializerException = $this->prophesize(ExceptionInterface::class); |
| 31 | + $serializerException->willExtend(\Exception::class); |
| 32 | + |
| 33 | + $flattenException = FlattenException::create($serializerException->reveal()); |
32 | 34 |
|
33 |
| - $flattenException->getStatusCode()->willReturn(Response::HTTP_BAD_REQUEST); |
34 | 35 | $serializer = $this->prophesize(SerializerInterface::class);
|
35 |
| - $exceptionAction = new ExceptionAction($serializer->reveal(), ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']]); |
| 36 | + $serializer->serialize($flattenException, 'jsonproblem')->willReturn(); |
| 37 | + |
| 38 | + $exceptionAction = new ExceptionAction($serializer->reveal(), ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']], [ExceptionInterface::class => Response::HTTP_BAD_REQUEST, InvalidArgumentException::class => Response::HTTP_BAD_REQUEST]); |
| 39 | + |
36 | 40 | $request = new Request();
|
37 | 41 | $request->setFormat('jsonproblem', 'application/problem+json');
|
38 |
| - $serializer->serialize($flattenException, 'jsonproblem')->willReturn(); |
| 42 | + |
39 | 43 | $expected = new Response('', Response::HTTP_BAD_REQUEST, [
|
40 | 44 | 'Content-Type' => 'application/problem+json; charset=utf-8',
|
41 | 45 | 'X-Content-Type-Options' => 'nosniff',
|
42 | 46 | 'X-Frame-Options' => 'deny',
|
43 | 47 | ]);
|
44 | 48 |
|
45 |
| - $this->assertEquals($expected, $exceptionAction($flattenException->reveal(), $request)); |
| 49 | + $this->assertEquals($expected, $exceptionAction($flattenException, $request)); |
| 50 | + } |
| 51 | + |
| 52 | + public function testActionWithUncatchableException() |
| 53 | + { |
| 54 | + $serializerException = $this->prophesize(ExceptionInterface::class); |
| 55 | + $serializerException->willExtend(\Exception::class); |
| 56 | + |
| 57 | + $flattenException = FlattenException::create($serializerException->reveal()); |
| 58 | + |
| 59 | + $serializer = $this->prophesize(SerializerInterface::class); |
| 60 | + $serializer->serialize($flattenException, 'jsonproblem')->willReturn(); |
| 61 | + |
| 62 | + $exceptionAction = new ExceptionAction($serializer->reveal(), ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']]); |
| 63 | + |
| 64 | + $request = new Request(); |
| 65 | + $request->setFormat('jsonproblem', 'application/problem+json'); |
| 66 | + |
| 67 | + $expected = new Response('', Response::HTTP_INTERNAL_SERVER_ERROR, [ |
| 68 | + 'Content-Type' => 'application/problem+json; charset=utf-8', |
| 69 | + 'X-Content-Type-Options' => 'nosniff', |
| 70 | + 'X-Frame-Options' => 'deny', |
| 71 | + ]); |
| 72 | + |
| 73 | + $this->assertEquals($expected, $exceptionAction($flattenException, $request)); |
46 | 74 | }
|
47 | 75 | }
|
0 commit comments