2525use Ibexa \Core \Base \Exceptions \NotFoundException ;
2626use Ibexa \Core \Base \Exceptions \UnauthorizedException ;
2727use PHPUnit \Framework \TestCase ;
28+ use RuntimeException ;
2829use Symfony \Component \HttpFoundation \Request ;
2930use Symfony \Component \HttpFoundation \Response ;
3031use Symfony \Component \HttpKernel \Event \ExceptionEvent ;
32+ use Symfony \Component \HttpKernel \Exception \AccessDeniedHttpException ;
3133use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
3234use Symfony \Component \HttpKernel \Exception \HttpException ;
3335use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
3840
3941class ExceptionListenerTest extends TestCase
4042{
41- /** @var \PHPUnit\Framework\MockObject\MockObject| \Symfony\Contracts\Translation\TranslatorInterface */
42- private $ translator ;
43+ /** @var \PHPUnit\Framework\MockObject\MockObject& \Symfony\Contracts\Translation\TranslatorInterface */
44+ private TranslatorInterface $ translator ;
4345
44- /** @var \Ibexa\Bundle\Core\EventListener\ExceptionListener */
45- private $ listener ;
46+ private ExceptionListener $ listener ;
4647
4748 protected function setUp (): void
4849 {
@@ -51,7 +52,7 @@ protected function setUp(): void
5152 $ this ->listener = new ExceptionListener ($ this ->translator );
5253 }
5354
54- public function testGetSubscribedEvents ()
55+ public function testGetSubscribedEvents (): void
5556 {
5657 self ::assertSame (
5758 [KernelEvents::EXCEPTION => ['onKernelException ' , 10 ]],
@@ -60,11 +61,9 @@ public function testGetSubscribedEvents()
6061 }
6162
6263 /**
63- * @param \Exception $exception
64- *
6564 * @return \Symfony\Component\HttpKernel\Event\ExceptionEvent
6665 */
67- private function generateExceptionEvent (Exception $ exception )
66+ private function generateExceptionEvent (Exception $ exception ): ExceptionEvent
6867 {
6968 return new ExceptionEvent (
7069 $ this ->createMock (HttpKernelInterface::class),
@@ -74,7 +73,7 @@ private function generateExceptionEvent(Exception $exception)
7473 );
7574 }
7675
77- public function testNotFoundException ()
76+ public function testNotFoundException (): void
7877 {
7978 $ messageTemplate = 'some message template ' ;
8079 $ translationParams = ['some ' => 'thing ' ];
@@ -84,20 +83,17 @@ public function testNotFoundException()
8483 $ event = $ this ->generateExceptionEvent ($ exception );
8584
8685 $ translatedMessage = 'translated message ' ;
87- $ this ->translator
88- ->expects (self ::once ())
89- ->method ('trans ' )
90- ->with ($ messageTemplate , $ translationParams )
91- ->willReturn ($ translatedMessage );
86+ $ this ->mockTranslatorTrans ($ messageTemplate , $ translationParams , $ translatedMessage );
9287
93- $ this ->listener ->onKernelException ($ event );
94- $ convertedException = $ event ->getThrowable ();
95- self ::assertInstanceOf (NotFoundHttpException::class, $ convertedException );
96- self ::assertSame ($ exception , $ convertedException ->getPrevious ());
97- self ::assertSame ($ translatedMessage , $ convertedException ->getMessage ());
88+ $ this ->assertSameException (
89+ NotFoundHttpException::class,
90+ $ event ,
91+ $ exception ,
92+ $ translatedMessage
93+ );
9894 }
9995
100- public function testUnauthorizedException ()
96+ public function testUnauthorizedException (): void
10197 {
10298 $ messageTemplate = 'some message template ' ;
10399 $ translationParams = ['some ' => 'thing ' ];
@@ -107,25 +103,22 @@ public function testUnauthorizedException()
107103 $ event = $ this ->generateExceptionEvent ($ exception );
108104
109105 $ translatedMessage = 'translated message ' ;
110- $ this ->translator
111- ->expects (self ::once ())
112- ->method ('trans ' )
113- ->with ($ messageTemplate , $ translationParams )
114- ->willReturn ($ translatedMessage );
106+ $ this ->mockTranslatorTrans ($ messageTemplate , $ translationParams , $ translatedMessage );
115107
116- $ this ->listener ->onKernelException ($ event );
117- $ convertedException = $ event ->getThrowable ();
118- self ::assertInstanceOf (AccessDeniedException::class, $ convertedException );
119- self ::assertSame ($ exception , $ convertedException ->getPrevious ());
120- self ::assertSame ($ translatedMessage , $ convertedException ->getMessage ());
108+ $ this ->assertSameException (
109+ AccessDeniedException::class,
110+ $ event ,
111+ $ exception ,
112+ $ translatedMessage
113+ );
121114 }
122115
123116 /**
124117 * @dataProvider badRequestExceptionProvider
125118 *
126- * @param \Exception| \Ibexa\Core\Base\Translatable $exception
119+ * @param \Exception& \Ibexa\Core\Base\Translatable $exception
127120 */
128- public function testBadRequestException (Exception $ exception )
121+ public function testBadRequestException (Exception $ exception ): void
129122 {
130123 $ messageTemplate = 'some message template ' ;
131124 $ translationParams = ['some ' => 'thing ' ];
@@ -134,35 +127,58 @@ public function testBadRequestException(Exception $exception)
134127 $ event = $ this ->generateExceptionEvent ($ exception );
135128
136129 $ translatedMessage = 'translated message ' ;
137- $ this ->translator
138- ->expects (self ::once ())
139- ->method ('trans ' )
140- ->with ($ messageTemplate , $ translationParams )
141- ->willReturn ($ translatedMessage );
130+ $ this ->mockTranslatorTrans ($ messageTemplate , $ translationParams , $ translatedMessage );
142131
143- $ this ->listener ->onKernelException ($ event );
144- $ convertedException = $ event ->getThrowable ();
145- self ::assertInstanceOf (BadRequestHttpException::class, $ convertedException );
146- self ::assertSame ($ exception , $ convertedException ->getPrevious ());
147- self ::assertSame ($ translatedMessage , $ convertedException ->getMessage ());
132+ $ this ->assertSameException (
133+ BadRequestHttpException::class,
134+ $ event ,
135+ $ exception ,
136+ $ translatedMessage
137+ );
148138 }
149139
150- public function badRequestExceptionProvider ()
140+ /**
141+ * @dataProvider provideDataForTestForbiddenException
142+ *
143+ * @param \Exception&\Ibexa\Core\Base\Translatable $exception
144+ */
145+ public function testForbiddenException (Exception $ exception ): void
151146 {
152- return [
153- [new BadStateException ('foo ' , 'bar ' )],
154- [new InvalidArgumentException ('foo ' , 'bar ' )],
155- [new InvalidArgumentType ('foo ' , 'bar ' )],
156- [new InvalidArgumentValue ('foo ' , 'bar ' )],
157- ];
147+ $ messageTemplate = 'some message template ' ;
148+ $ translationParams = ['some ' => 'thing ' ];
149+ $ exception ->setMessageTemplate ($ messageTemplate );
150+ $ exception ->setParameters ($ translationParams );
151+ $ event = $ this ->generateExceptionEvent ($ exception );
152+
153+ $ translatedMessage = 'translated message ' ;
154+ $ this ->mockTranslatorTrans ($ messageTemplate , $ translationParams , $ translatedMessage );
155+
156+ $ this ->assertSameException (
157+ AccessDeniedHttpException::class,
158+ $ event ,
159+ $ exception ,
160+ $ translatedMessage
161+ );
162+ }
163+
164+ public function testUntouchedException (): void
165+ {
166+ $ exception = new RuntimeException ('foo ' );
167+ $ event = $ this ->generateExceptionEvent ($ exception );
168+ $ this ->translator
169+ ->expects (self ::never ())
170+ ->method ('trans ' );
171+
172+ $ this ->listener ->onKernelException ($ event );
173+ self ::assertSame ($ exception , $ event ->getThrowable ());
158174 }
159175
160176 /**
161177 * @dataProvider otherExceptionProvider
162178 *
163- * @param \Exception| \Ibexa\Core\Base\Translatable $exception
179+ * @param \Exception& \Ibexa\Core\Base\Translatable $exception
164180 */
165- public function testOtherRepositoryException (Exception $ exception )
181+ public function testOtherRepositoryException (Exception $ exception ): void
166182 {
167183 $ messageTemplate = 'some message template ' ;
168184 $ translationParams = ['some ' => 'thing ' ];
@@ -171,11 +187,7 @@ public function testOtherRepositoryException(Exception $exception)
171187 $ event = $ this ->generateExceptionEvent ($ exception );
172188
173189 $ translatedMessage = 'translated message ' ;
174- $ this ->translator
175- ->expects (self ::once ())
176- ->method ('trans ' )
177- ->with ($ messageTemplate , $ translationParams )
178- ->willReturn ($ translatedMessage );
190+ $ this ->mockTranslatorTrans ($ messageTemplate , $ translationParams , $ translatedMessage );
179191
180192 $ this ->listener ->onKernelException ($ event );
181193 $ convertedException = $ event ->getThrowable ();
@@ -185,30 +197,76 @@ public function testOtherRepositoryException(Exception $exception)
185197 self ::assertSame (Response::HTTP_INTERNAL_SERVER_ERROR , $ convertedException ->getStatusCode ());
186198 }
187199
188- public function otherExceptionProvider ()
200+ /**
201+ * @return iterable<array{\Exception&\Ibexa\Core\Base\Translatable}>
202+ */
203+ public function badRequestExceptionProvider (): iterable
204+ {
205+ return [
206+ [new BadStateException ('foo ' , 'bar ' )],
207+ [new InvalidArgumentException ('foo ' , 'bar ' )],
208+ [new InvalidArgumentType ('foo ' , 'bar ' )],
209+ [new InvalidArgumentValue ('foo ' , 'bar ' )],
210+ ];
211+ }
212+
213+ /**
214+ * @return iterable<array{\Exception&\Ibexa\Core\Base\Translatable}>
215+ */
216+ public function provideDataForTestForbiddenException (): iterable
189217 {
190218 return [
191- [new ForbiddenException ('foo ' )],
219+ [new ForbiddenException ('foo "%param%" ' , [ ' %param% ' => ' bar ' ] )],
192220 [new LimitationValidationException ([])],
193- [new MissingClass ('foo ' )],
194221 [new ContentValidationException ('foo ' )],
195222 [new ContentTypeValidationException ('foo ' )],
196223 [new ContentFieldValidationException ([])],
197224 [new ContentTypeFieldDefinitionValidationException ([])],
225+ ];
226+ }
227+
228+ /**
229+ * @return iterable<array{\Exception&\Ibexa\Core\Base\Translatable}>
230+ */
231+ public function otherExceptionProvider (): iterable
232+ {
233+ return [
234+ [new MissingClass ('foo ' )],
198235 [new FieldTypeNotFoundException ('foo ' )],
199236 [new LimitationNotFoundException ('foo ' )],
200237 ];
201238 }
202239
203- public function testUntouchedException ()
204- {
205- $ exception = new \RuntimeException ('foo ' );
206- $ event = $ this ->generateExceptionEvent ($ exception );
240+ /**
241+ * @param array<string, mixed> $translationParams
242+ */
243+ private function mockTranslatorTrans (
244+ string $ messageTemplate ,
245+ array $ translationParams ,
246+ string $ translatedMessage
247+ ): void {
207248 $ this ->translator
208- ->expects (self ::never ())
209- ->method ('trans ' );
249+ ->expects (self ::once ())
250+ ->method ('trans ' )
251+ ->with ($ messageTemplate , $ translationParams )
252+ ->willReturn ($ translatedMessage );
253+ }
210254
255+ /**
256+ * @param class-string $expectedException
257+ * @param \Exception&\Ibexa\Core\Base\Translatable $exception
258+ */
259+ private function assertSameException (
260+ string $ expectedException ,
261+ ExceptionEvent $ event ,
262+ Exception $ exception ,
263+ string $ translatedMessage
264+ ): void {
211265 $ this ->listener ->onKernelException ($ event );
212- self ::assertSame ($ exception , $ event ->getThrowable ());
266+ $ convertedException = $ event ->getThrowable ();
267+
268+ self ::assertInstanceOf ($ expectedException , $ convertedException );
269+ self ::assertSame ($ exception , $ convertedException ->getPrevious ());
270+ self ::assertSame ($ translatedMessage , $ convertedException ->getMessage ());
213271 }
214272}
0 commit comments