1 | <?php |
||
32 | class HandlerTest extends UnitTestCase |
||
33 | { |
||
34 | //<editor-fold desc="Public Methods"> |
||
35 | /** |
||
36 | * @covers \Tfboe\FmLib\Exceptions\Handler::render |
||
37 | * @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionHTTPStatusCode |
||
38 | * @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionName |
||
39 | * @covers \Tfboe\FmLib\Exceptions\Handler::getJsonMessage |
||
40 | * @uses \Tfboe\FmLib\Exceptions\AuthenticationException |
||
41 | * @uses \Tfboe\FmLib\Exceptions\DuplicateException |
||
42 | * @uses \Tfboe\FmLib\Exceptions\PlayerAlreadyExists |
||
43 | * @uses \Tfboe\FmLib\Exceptions\ReferenceException |
||
44 | * @uses \Tfboe\FmLib\Exceptions\UnorderedPhaseNumberException |
||
45 | */ |
||
46 | public function testRender() |
||
47 | { |
||
48 | $handler = $this->handler(); |
||
49 | $exception = new \Exception("Exception message"); |
||
50 | $res = $handler->render($this->request(), $exception); |
||
51 | self::assertInstanceOf(JsonResponse::class, $res); |
||
52 | /** @var JsonResponse $res */ |
||
53 | self::assertEquals(['status' => 500, 'message' => 'Exception message', 'name' => 'InternalException'], |
||
54 | $res->getData(true)); |
||
55 | |||
56 | $exception = new \Exception("Exception message", 402); |
||
57 | $res = $handler->render($this->request(), $exception); |
||
58 | self::assertInstanceOf(JsonResponse::class, $res); |
||
59 | /** @var JsonResponse $res */ |
||
60 | self::assertEquals(['status' => 402, 'message' => 'Exception message', 'name' => 'InternalException'], |
||
61 | $res->getData(true)); |
||
62 | |||
63 | $exception = new AuthenticationException("Authentication Exception message"); |
||
64 | $res = $handler->render($this->request(), $exception); |
||
65 | self::assertInstanceOf(JsonResponse::class, $res); |
||
66 | /** @var JsonResponse $res */ |
||
67 | self::assertEquals(['status' => 401, 'message' => 'Authentication Exception message', |
||
68 | 'name' => 'AuthenticationException'], $res->getData(true)); |
||
69 | |||
70 | $exception = new DuplicateException('value', 'name', 'array'); |
||
71 | $res = $handler->render($this->request(), $exception); |
||
72 | self::assertInstanceOf(JsonResponse::class, $res); |
||
73 | /** @var JsonResponse $res */ |
||
74 | self::assertEquals(['status' => 409, 'message' => 'Duplicate Exception', |
||
75 | 'name' => 'DuplicateException', 'duplicateValue' => 'value', 'arrayName' => 'array'], $res->getData(true)); |
||
76 | |||
77 | $exception = new ReferenceException('value', 'name'); |
||
78 | $res = $handler->render($this->request(), $exception); |
||
79 | self::assertInstanceOf(JsonResponse::class, $res); |
||
80 | /** @var JsonResponse $res */ |
||
81 | self::assertEquals(['status' => 409, 'message' => 'Reference Exception', |
||
82 | 'name' => 'ReferenceException', 'referenceValue' => 'value', 'referenceName' => 'name'], $res->getData(true)); |
||
83 | |||
84 | $exception = new UnorderedPhaseNumberException(2, 1); |
||
85 | $res = $handler->render($this->request(), $exception); |
||
86 | self::assertInstanceOf(JsonResponse::class, $res); |
||
87 | /** @var JsonResponse $res */ |
||
88 | self::assertEquals(['status' => 409, 'message' => 'Unordered Phase Number Exception', |
||
89 | 'name' => 'UnorderedPhaseNumberException', 'previousPhaseNumber' => 2, 'nextPhaseNumber' => 1], |
||
90 | $res->getData(true)); |
||
91 | |||
92 | $exception = new PlayerAlreadyExists([]); |
||
93 | $res = $handler->render($this->request(), $exception); |
||
94 | self::assertInstanceOf(JsonResponse::class, $res); |
||
95 | /** @var JsonResponse $res */ |
||
96 | self::assertEquals(['status' => 409, 'message' => 'Some players do already exist', |
||
97 | 'name' => 'PlayerAlreadyExistsException', 'players' => []], $res->getData(true)); |
||
98 | |||
99 | // test trace in debug mode |
||
100 | |||
101 | $exception = new PlayerAlreadyExists([]); |
||
102 | $res = $handler->render($this->request(), $exception, true); |
||
103 | self::assertInstanceOf(JsonResponse::class, $res); |
||
104 | /** @var JsonResponse $res */ |
||
105 | $data = $res->getData(true); |
||
106 | self::assertArraySubset(['status' => 409, 'message' => 'Some players do already exist', |
||
|
|||
107 | 'name' => 'PlayerAlreadyExistsException', 'players' => []], $data); |
||
108 | self::assertArrayHasKey('trace', $data); |
||
109 | self::assertNotEmpty($data['trace']); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @covers \Tfboe\FmLib\Exceptions\Handler::render |
||
114 | * @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionHTTPStatusCode |
||
115 | * @covers \Tfboe\FmLib\Exceptions\Handler::getExceptionName |
||
116 | * @covers \Tfboe\FmLib\Exceptions\Handler::getJsonMessage |
||
117 | */ |
||
118 | public function testRenderValidationErrors() |
||
135 | //</editor-fold desc="Public Methods"> |
||
136 | |||
137 | //<editor-fold desc="Private Methods"> |
||
138 | /** |
||
139 | * @return Handler a new handler |
||
140 | */ |
||
141 | private function handler() |
||
145 | |||
146 | /** |
||
147 | * @return Request a new request |
||
148 | */ |
||
149 | private function request() |
||
153 | //</editor-fold desc="Private Methods"> |
||
154 | } |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.