[Serializer] Nicer ExtraAttributesException message for single attribute

This commit is contained in:
Matěj Humpál 2021-03-25 18:21:49 +01:00
parent 2dcf313a87
commit 1ba6a2cf50
No known key found for this signature in database
GPG Key ID: 7BEF289A1ADEDC4D
2 changed files with 19 additions and 1 deletions

View File

@ -22,7 +22,11 @@ class ExtraAttributesException extends RuntimeException
public function __construct(array $extraAttributes, \Throwable $previous = null)
{
$msg = sprintf('Extra attributes are not allowed ("%s" are unknown).', implode('", "', $extraAttributes));
$msg = sprintf(
'Extra attributes are not allowed ("%s" %s unknown).',
implode('", "', $extraAttributes),
\count($extraAttributes) > 1 ? 'are' : 'is'
);
$this->extraAttributes = $extraAttributes;

View File

@ -61,6 +61,20 @@ class AbstractObjectNormalizerTest extends TestCase
$this->assertInstanceOf(Dummy::class, $normalizer->instantiateObject($data, $class, $context, new \ReflectionClass($class), []));
}
public function testDenormalizeWithExtraAttribute()
{
$this->expectException(ExtraAttributesException::class);
$this->expectExceptionMessage('Extra attributes are not allowed ("fooFoo" is unknown).');
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new AbstractObjectNormalizerDummy($factory);
$normalizer->denormalize(
['fooFoo' => 'foo'],
Dummy::class,
'any',
['allow_extra_attributes' => false]
);
}
public function testDenormalizeWithExtraAttributes()
{
$this->expectException(ExtraAttributesException::class);