Skip to content

Commit afc84f3

Browse files
committed
[Serializer] Includes a test case to cover this scenario
1 parent c3c8ecc commit afc84f3

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,54 @@ public function testCollectDenormalizationErrorsDefaultContext()
16771677

16781678
$serializer->denormalize($data, DummyWithVariadicParameter::class);
16791679
}
1680+
1681+
public function testDenormalizationFailsWithMultipleErrorsInDefaultContext()
1682+
{
1683+
$serializer = new Serializer(
1684+
[new DateTimeNormalizer(), new ObjectNormalizer()],
1685+
[],
1686+
[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true]
1687+
);
1688+
1689+
$data = ['date' => '', 'unknown' => null];
1690+
1691+
try {
1692+
$serializer->denormalize($data, DummyEntityWithStringAndDateTime::class);
1693+
$this->fail('Expected PartialDenormalizationException was not thrown');
1694+
} catch (PartialDenormalizationException $e) {
1695+
$this->assertIsArray($e->getErrors());
1696+
$this->assertCount(2, $e->getErrors(), 'Expected two denormalization errors');
1697+
1698+
$exceptionsAsArray = array_map(function (NotNormalizableValueException $ex): array {
1699+
return [
1700+
'currentType' => $ex->getCurrentType(),
1701+
'expectedTypes' => $ex->getExpectedTypes(),
1702+
'path' => $ex->getPath(),
1703+
'useMessageForUser' => $ex->canUseMessageForUser(),
1704+
'message' => $ex->getMessage(),
1705+
];
1706+
}, $e->getErrors());
1707+
1708+
$expected = [
1709+
[
1710+
'currentType' => 'null',
1711+
'expectedTypes' => ['string'],
1712+
'path' => 'bar',
1713+
'useMessageForUser' => true,
1714+
'message' => 'Failed to create object because the class misses the "bar" property.',
1715+
],
1716+
[
1717+
'currentType' => 'string',
1718+
'expectedTypes' => ['string'],
1719+
'path' => 'date',
1720+
'useMessageForUser' => true,
1721+
'message' => 'The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.',
1722+
],
1723+
];
1724+
1725+
$this->assertSame($expected, $exceptionsAsArray);
1726+
}
1727+
}
16801728
}
16811729

16821730
class Model
@@ -1743,6 +1791,15 @@ public function __construct($value)
17431791
}
17441792
}
17451793

1794+
class DummyEntityWithStringAndDateTime
1795+
{
1796+
public function __construct(
1797+
public string $bar,
1798+
public \DateTimeInterface $date,
1799+
) {
1800+
}
1801+
}
1802+
17461803
class DummyUnionType
17471804
{
17481805
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy