Skip to content

Commit 6d945e2

Browse files
committed
Merge remote-tracking branch 'origin/4.6'
2 parents 4b60242 + 9de4817 commit 6d945e2

File tree

3 files changed

+128
-132
lines changed

3 files changed

+128
-132
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -22032,72 +22032,6 @@ parameters:
2203222032
count: 1
2203322033
path: tests/bundle/Core/EventListener/ContentDownloadRouteReferenceListenerTest.php
2203422034

22035-
-
22036-
message: '#^Call to an undefined method Exception\:\:setMessageTemplate\(\)\.$#'
22037-
identifier: method.notFound
22038-
count: 2
22039-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22040-
22041-
-
22042-
message: '#^Call to an undefined method Exception\:\:setParameters\(\)\.$#'
22043-
identifier: method.notFound
22044-
count: 2
22045-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22046-
22047-
-
22048-
message: '#^Method Ibexa\\Tests\\Bundle\\Core\\EventListener\\ExceptionListenerTest\:\:badRequestExceptionProvider\(\) has no return type specified\.$#'
22049-
identifier: missingType.return
22050-
count: 1
22051-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22052-
22053-
-
22054-
message: '#^Method Ibexa\\Tests\\Bundle\\Core\\EventListener\\ExceptionListenerTest\:\:otherExceptionProvider\(\) has no return type specified\.$#'
22055-
identifier: missingType.return
22056-
count: 1
22057-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22058-
22059-
-
22060-
message: '#^Method Ibexa\\Tests\\Bundle\\Core\\EventListener\\ExceptionListenerTest\:\:testBadRequestException\(\) has no return type specified\.$#'
22061-
identifier: missingType.return
22062-
count: 1
22063-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22064-
22065-
-
22066-
message: '#^Method Ibexa\\Tests\\Bundle\\Core\\EventListener\\ExceptionListenerTest\:\:testGetSubscribedEvents\(\) has no return type specified\.$#'
22067-
identifier: missingType.return
22068-
count: 1
22069-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22070-
22071-
-
22072-
message: '#^Method Ibexa\\Tests\\Bundle\\Core\\EventListener\\ExceptionListenerTest\:\:testNotFoundException\(\) has no return type specified\.$#'
22073-
identifier: missingType.return
22074-
count: 1
22075-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22076-
22077-
-
22078-
message: '#^Method Ibexa\\Tests\\Bundle\\Core\\EventListener\\ExceptionListenerTest\:\:testOtherRepositoryException\(\) has no return type specified\.$#'
22079-
identifier: missingType.return
22080-
count: 1
22081-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22082-
22083-
-
22084-
message: '#^Method Ibexa\\Tests\\Bundle\\Core\\EventListener\\ExceptionListenerTest\:\:testUnauthorizedException\(\) has no return type specified\.$#'
22085-
identifier: missingType.return
22086-
count: 1
22087-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22088-
22089-
-
22090-
message: '#^Method Ibexa\\Tests\\Bundle\\Core\\EventListener\\ExceptionListenerTest\:\:testUntouchedException\(\) has no return type specified\.$#'
22091-
identifier: missingType.return
22092-
count: 1
22093-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22094-
22095-
-
22096-
message: '#^PHPDoc tag @param for parameter \$exception with type Exception\|Ibexa\\Core\\Base\\Translatable is not subtype of native type Exception\.$#'
22097-
identifier: parameter.phpDocType
22098-
count: 2
22099-
path: tests/bundle/Core/EventListener/ExceptionListenerTest.php
22100-
2210122035
-
2210222036
message: '#^Method Ibexa\\Tests\\Bundle\\Core\\EventListener\\IndexRequestListenerTest\:\:indexPageProvider\(\) has no return type specified\.$#'
2210322037
identifier: missingType.return

src/bundle/Core/EventListener/ExceptionListener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Exception;
1111
use Ibexa\Contracts\Core\Repository\Exceptions\BadStateException;
12+
use Ibexa\Contracts\Core\Repository\Exceptions\ForbiddenException;
1213
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
1314
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
1415
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
@@ -17,6 +18,7 @@
1718
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1819
use Symfony\Component\HttpFoundation\Response;
1920
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
21+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
2022
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2123
use Symfony\Component\HttpKernel\Exception\HttpException;
2224
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -51,6 +53,8 @@ public function onKernelException(ExceptionEvent $event)
5153
$event->setThrowable(new AccessDeniedException($this->getTranslatedMessage($exception), $exception));
5254
} elseif ($exception instanceof BadStateException || $exception instanceof InvalidArgumentException) {
5355
$event->setThrowable(new BadRequestHttpException($this->getTranslatedMessage($exception), $exception));
56+
} elseif ($exception instanceof ForbiddenException) {
57+
$event->setThrowable(new AccessDeniedHttpException($this->getTranslatedMessage($exception), $exception));
5458
} elseif ($exception instanceof Translatable) {
5559
$event->setThrowable(
5660
new HttpException(

tests/bundle/Core/EventListener/ExceptionListenerTest.php

Lines changed: 124 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
use Ibexa\Core\Base\Exceptions\NotFoundException;
2626
use Ibexa\Core\Base\Exceptions\UnauthorizedException;
2727
use PHPUnit\Framework\TestCase;
28+
use RuntimeException;
2829
use Symfony\Component\HttpFoundation\Request;
2930
use Symfony\Component\HttpFoundation\Response;
3031
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
32+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
3133
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
3234
use Symfony\Component\HttpKernel\Exception\HttpException;
3335
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -38,11 +40,10 @@
3840

3941
class 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

Comments
 (0)