minor #40585 [Serializer] Nicer ExtraAttributesException message for single attribute (finwe)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[Serializer] Nicer ExtraAttributesException message for single attribute

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | none
| License       | MIT
| Doc PR        | n/a

This PR changes wording of ExtraAttributesException message in case there is only one extra attribute:

Before
----
    Extra attributes are not allowed ("foo", "bar" are unknown).
    Extra attributes are not allowed ("foo" are unknown).

After:
----

    Extra attributes are not allowed ("foo", "bar" are unknown).
    Extra attributes are not allowed ("foo" is unknown).

Commits
-------

1ba6a2cf50 [Serializer] Nicer ExtraAttributesException message for single attribute
This commit is contained in:
Fabien Potencier 2021-03-28 08:38:24 +02:00
commit 8081d97994
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);