66
77use Chubbyphp \ApiHttp \Manager \ResponseManagerInterface ;
88use Chubbyphp \ApiHttp \Middleware \ApiExceptionMiddleware ;
9+ use Chubbyphp \HttpException \HttpException ;
910use Chubbyphp \HttpException \HttpExceptionInterface ;
1011use Chubbyphp \Mock \Argument \ArgumentCallback ;
1112use Chubbyphp \Mock \Call ;
@@ -84,7 +85,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
8485 $ logger = $ this ->getMockByCalls (LoggerInterface::class, [
8586 Call::create ('error ' )
8687 ->with (
87- 'Exception ' ,
88+ 'Http Exception ' ,
8889 new ArgumentCallback (static function (array $ context ): void {
8990 $ backtrace = $ context ['backtrace ' ];
9091
@@ -116,6 +117,68 @@ public function handle(ServerRequestInterface $request): ResponseInterface
116117 self ::assertSame ($ response , $ middleware ->process ($ request , $ requestHandler ));
117118 }
118119
120+ public function testWithHttpExceptionWithDebugWithLogger (): void
121+ {
122+ $ httpException = HttpException::createNotFound ();
123+
124+ /** @var MockObject|ServerRequestInterface $request */
125+ $ request = $ this ->getMockByCalls (ServerRequestInterface::class, [
126+ Call::create ('getAttribute ' )->with ('accept ' , null )->willReturn ('application/xml ' ),
127+ ]);
128+
129+ /** @var MockObject|ResponseInterface $response */
130+ $ response = $ this ->getMockByCalls (ResponseInterface::class);
131+
132+ $ requestHandler = new class ($ httpException ) implements RequestHandlerInterface {
133+ public function __construct (private HttpExceptionInterface $ httpException )
134+ {
135+ }
136+
137+ public function handle (ServerRequestInterface $ request ): ResponseInterface
138+ {
139+ throw $ this ->httpException ;
140+ }
141+ };
142+
143+ /** @var MockObject|ResponseManagerInterface $responseManager */
144+ $ responseManager = $ this ->getMockByCalls (ResponseManagerInterface::class, [
145+ Call::create ('createFromHttpException ' )
146+ ->with (
147+ new ArgumentCallback (static function (HttpExceptionInterface $ givenhttpException ) use ($ httpException ): void {
148+ self ::assertSame ($ httpException , $ givenhttpException );
149+ }),
150+ 'application/xml ' ,
151+ )
152+ ->willReturn ($ response ),
153+ ]);
154+
155+ /** @var LoggerInterface|MockObject $logger */
156+ $ logger = $ this ->getMockByCalls (LoggerInterface::class, [
157+ Call::create ('info ' )
158+ ->with (
159+ 'Http Exception ' ,
160+ new ArgumentCallback (static function (array $ context ): void {
161+ $ backtrace = $ context ['backtrace ' ];
162+
163+ self ::assertCount (1 , $ backtrace );
164+
165+ $ exception = array_shift ($ backtrace );
166+
167+ self ::assertSame (HttpException::class, $ exception ['class ' ]);
168+ self ::assertSame ('Not Found ' , $ exception ['message ' ]);
169+ self ::assertSame (404 , $ exception ['code ' ]);
170+ self ::assertArrayHasKey ('file ' , $ exception );
171+ self ::assertArrayHasKey ('line ' , $ exception );
172+ self ::assertArrayHasKey ('trace ' , $ exception );
173+ })
174+ ),
175+ ]);
176+
177+ $ middleware = new ApiExceptionMiddleware ($ responseManager , true , $ logger );
178+
179+ self ::assertSame ($ response , $ middleware ->process ($ request , $ requestHandler ));
180+ }
181+
119182 public function testWithExceptionWithDebugWithLogger (): void
120183 {
121184 /** @var MockObject|ServerRequestInterface $request */
@@ -142,6 +205,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
142205
143206 $ data = $ httpException ->jsonSerialize ();
144207
208+ self ::assertArrayHasKey ('detail ' , $ data );
209+
210+ self ::assertSame ('runtime exception ' , $ data ['detail ' ]);
211+
145212 self ::assertArrayHasKey ('backtrace ' , $ data );
146213
147214 $ backtrace = $ data ['backtrace ' ];
@@ -175,7 +242,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
175242 $ logger = $ this ->getMockByCalls (LoggerInterface::class, [
176243 Call::create ('error ' )
177244 ->with (
178- 'Exception ' ,
245+ 'Http Exception ' ,
179246 new ArgumentCallback (static function (array $ context ): void {
180247 $ backtrace = $ context ['backtrace ' ];
181248
@@ -246,7 +313,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
246313 $ logger = $ this ->getMockByCalls (LoggerInterface::class, [
247314 Call::create ('error ' )
248315 ->with (
249- 'Exception ' ,
316+ 'Http Exception ' ,
250317 new ArgumentCallback (static function (array $ context ): void {
251318 $ backtrace = $ context ['backtrace ' ];
252319
0 commit comments