66
77use Jenky \ApiError \Formatter \GenericErrorFormatter ;
88use Jenky \ApiError \Formatter \Rfc7807ErrorFormatter ;
9+ use Jenky \ApiError \GenericProblem ;
10+ use Jenky \ApiError \Problem ;
11+ use Jenky \ApiError \Rfc7807Problem ;
12+ use Jenky \ApiError \Transformer \ExceptionTransformer ;
913use PHPUnit \Framework \Attributes \DataProvider ;
1014use PHPUnit \Framework \TestCase ;
1115
@@ -37,14 +41,15 @@ public function test_generic_error_formatter(\Throwable $exception, bool $debug)
3741 #[DataProvider('provideExceptions ' )]
3842 public function test_rfc7808_error_formatter (\Throwable $ exception , bool $ debug ): void
3943 {
40- $ formatter = new Rfc7807ErrorFormatter ($ debug );
44+ $ formatter = new Rfc7807ErrorFormatter ($ debug, new Rfc7807Transformer () );
4145
4246 $ data = $ formatter ->format ($ exception )->data ;
4347
4448 $ this ->assertIsArray ($ data );
4549 $ this ->assertArrayHasKey ('type ' , $ data );
4650 $ this ->assertArrayHasKey ('title ' , $ data );
4751 $ this ->assertArrayHasKey ('status ' , $ data );
52+ $ this ->assertArrayHasKey ('invalid-params ' , $ data );
4853
4954 if ($ debug ) {
5055 $ this ->assertArrayHasKey ('debug ' , $ data );
@@ -57,11 +62,33 @@ public function test_rfc7808_error_formatter(\Throwable $exception, bool $debug)
5762 $ this ->assertSame ($ data ['detail ' ], $ exception ->getMessage ());
5863 }
5964
60- $ this ->assertSame ($ data ['type ' ], 'about:blank ' );
65+ $ this ->assertSame ($ data ['type ' ], 'http://localhost ' );
6166 $ this ->assertSame ($ data ['title ' ], 'Internal Server Error ' );
6267 $ this ->assertSame ($ data ['status ' ], 500 );
6368 }
6469
70+ #[DataProvider('provideExceptions ' )]
71+ public function test_custom_formatter (\Throwable $ exception , bool $ debug ): void
72+ {
73+ $ formatter = new GenericErrorFormatter (transformer: new ExceptionClassTransformer ());
74+ $ formatter ->setFormat ([
75+ 'message ' => '{title} ' ,
76+ 'status ' => '{status_code} ' ,
77+ 'class ' => '{class} ' ,
78+ ]);
79+
80+ $ data = $ formatter ->format ($ exception )->data ;
81+
82+ $ this ->assertIsArray ($ data );
83+ $ this ->assertArrayHasKey ('message ' , $ data );
84+ $ this ->assertArrayHasKey ('status ' , $ data );
85+ $ this ->assertArrayHasKey ('class ' , $ data );
86+
87+ $ this ->assertSame ($ data ['message ' ], $ exception ->getMessage () ?: 'Internal Server Error ' );
88+ $ this ->assertSame ($ data ['status ' ], 500 );
89+ $ this ->assertSame ($ data ['class ' ], \get_class ($ exception ));
90+ }
91+
6592 public static function provideExceptions (): iterable
6693 {
6794 yield [new \RuntimeException (), false ];
@@ -74,3 +101,29 @@ public static function provideExceptions(): iterable
74101 yield [new \InvalidArgumentException ('' , 100 ), true ];
75102 }
76103}
104+
105+ final class ExceptionClassTransformer implements ExceptionTransformer
106+ {
107+ public function transform (\Throwable $ exception ): Problem
108+ {
109+ $ problem = GenericProblem::createFromThrowable ($ exception );
110+
111+ $ problem ->set ('class ' , $ problem ->getClass ());
112+
113+ return $ problem ;
114+ }
115+ }
116+
117+ final class Rfc7807Transformer implements ExceptionTransformer
118+ {
119+ public function transform (\Throwable $ exception ): Problem
120+ {
121+ $ problem = Rfc7807Problem::createFromThrowable ($ exception );
122+
123+ $ problem ->setType ('http://localhost ' )
124+ ->addInvalidParam ('foo ' , 'bar ' )
125+ ->addInvalidParam ('quiz ' , 'qux ' );
126+
127+ return $ problem ;
128+ }
129+ }
0 commit comments