Skip to content

Commit 800529e

Browse files
Mokhtar Tlilisfmok
authored andcommitted
Fix exceptions issues
1 parent 311881f commit 800529e

File tree

8 files changed

+33
-8
lines changed

8 files changed

+33
-8
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function getConfigTreeBuilder(): TreeBuilder
4040
}
4141
return false;
4242
})
43-
->thenInvalid(sprintf('Only the formats %s are supported. Got %s.', implode(', ', Input::INPUT_SUPPORTED_FORMATS), '%s'))
43+
->thenInvalid(sprintf('Only the formats [%s] are supported. Got %s.', implode(', ', Input::INPUT_SUPPORTED_FORMATS), '%s'))
4444
->end()
4545
->end()
4646
->booleanNode('skip_validation')

src/EventListener/ExceptionListener.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Symfony\Component\HttpFoundation\Response;
1010
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1111
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
12-
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
1312
use Symfony\Component\Serializer\SerializerInterface;
1413

1514
class ExceptionListener

src/EventListener/ReadInputListener.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Sfmok\RequestInput\EventListener;
66

77
use Sfmok\RequestInput\Attribute\Input;
8+
use Sfmok\RequestInput\Exception\UnexpectedFormatException;
89
use Sfmok\RequestInput\Metadata\InputMetadataFactoryInterface;
910
use Symfony\Component\HttpKernel\Event\ControllerEvent;
1011

@@ -22,6 +23,14 @@ public function onKernelController(ControllerEvent $event): void
2223
return;
2324
}
2425

26+
if (!\in_array($inputMetadata->getFormat(), Input::INPUT_SUPPORTED_FORMATS)) {
27+
throw new UnexpectedFormatException(sprintf(
28+
'Only the formats [%s] are supported. Got %s.',
29+
implode(', ', Input::INPUT_SUPPORTED_FORMATS),
30+
$inputMetadata->getFormat()
31+
));
32+
}
33+
2534
$event->getRequest()->attributes->set('_input', $inputMetadata);
2635
}
2736
}

src/Exception/UnexpectedFormatException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
88

9-
class UnexpectedFormatException extends BadRequestHttpException implements ExceptionInterface
9+
class UnexpectedFormatException extends BadRequestHttpException
1010
{
1111
}

src/Factory/InputFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function createFromRequest(Request $request, string $type, string $format
3232
{
3333
if (!\in_array($format, Input::INPUT_SUPPORTED_FORMATS)) {
3434
throw new UnexpectedFormatException(sprintf(
35-
'Only the formats %s are supported. Got %s.',
36-
$format,
37-
implode(', ', Input::INPUT_SUPPORTED_FORMATS)
35+
'Only the formats [%s] are supported. Got %s.',
36+
implode(', ', Input::INPUT_SUPPORTED_FORMATS),
37+
$format
3838
));
3939
}
4040

tests/DependencyInjection/ConfigurationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public function invalidFormatsProvider(): iterable
5050
*/
5151
public function testInvalidFormatsConfig(array $formats): void
5252
{
53-
$this->expectException(InvalidConfigurationException::class);
54-
$this->expectExceptionMessageMatches('/Only the formats .+ are supported. Got .+./');
53+
self::expectException(InvalidConfigurationException::class);
54+
self::expectExceptionMessageMatches('/Only the formats .+ are supported. Got .+./');
5555

5656
$this->processor->processConfiguration($this->configuration, [
5757
'request_input' => [

tests/EventListener/ReadInputListenerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Prophecy\Prophecy\ObjectProphecy;
1010
use Sfmok\RequestInput\Attribute\Input;
1111
use Sfmok\RequestInput\EventListener\ReadInputListener;
12+
use Sfmok\RequestInput\Exception\UnexpectedFormatException;
1213
use Sfmok\RequestInput\Metadata\InputMetadataFactory;
1314
use Sfmok\RequestInput\Tests\Fixtures\Controller\TestController;
1415
use Symfony\Component\HttpFoundation\Request;
@@ -47,6 +48,17 @@ public function testOnKernelControllerWithoutInput(): void
4748
self::assertFalse($request->attributes->has('_input'));
4849
}
4950

51+
public function testOnKernelControllerWithUnsupportedFormat(): void
52+
{
53+
self::expectException(UnexpectedFormatException::class);
54+
self::expectExceptionMessageMatches('/Only the formats .+ are supported. Got .+./');
55+
56+
$request = new Request();
57+
$event = $this->getControllerEvent($request, 'testWithInputUnsupportedFormat');
58+
$listener = new ReadInputListener(new InputMetadataFactory());
59+
$listener->onKernelController($event);
60+
}
61+
5062
private function getControllerEvent(Request $request, string $method): ControllerEvent
5163
{
5264
return new ControllerEvent(

tests/Fixtures/Controller/TestController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public function testWithInput(): void
2121
public function testWithoutInput(): void
2222
{
2323
}
24+
25+
#[Input(format: 'unsupported')]
26+
public function testWithInputUnsupportedFormat(): void
27+
{
28+
}
2429
}

0 commit comments

Comments
 (0)