Skip to content

Commit b0a559f

Browse files
committed
Add and fix tests
1 parent c1f4810 commit b0a559f

File tree

7 files changed

+250
-37
lines changed

7 files changed

+250
-37
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
428428
} else {
429429
$message = sprintf('The type of the "%s" attribute for class "%s" must be bool ("%s" given).', $attribute, $currentClass, $data);
430430

431-
return $this->denormalizationFailure($data, $attribute, $message, $context);
431+
return $this->denormalizationFailure($data, $message, $context);
432432
}
433433
break;
434434
case Type::BUILTIN_TYPE_INT:
@@ -437,7 +437,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
437437
} else {
438438
$message = sprintf('The type of the "%s" attribute for class "%s" must be int ("%s" given).', $attribute, $currentClass, $data);
439439

440-
return $this->denormalizationFailure($data, $attribute, $message, $context);
440+
return $this->denormalizationFailure($data, $message, $context);
441441
}
442442
break;
443443
case Type::BUILTIN_TYPE_FLOAT:
@@ -455,7 +455,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
455455
default:
456456
$message = sprintf('The type of the "%s" attribute for class "%s" must be float ("%s" given).', $attribute, $currentClass, $data);
457457

458-
return $this->denormalizationFailure($data, $attribute, $message, $context);
458+
return $this->denormalizationFailure($data, $message, $context);
459459
}
460460

461461
break;
@@ -528,7 +528,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
528528

529529
$message = sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), get_debug_type($data));
530530

531-
return $this->denormalizationFailure($data, $attribute, $message, $context);
531+
return $this->denormalizationFailure($data, $message, $context);
532532
}
533533

534534
private function denormalizationSuccess($denormalizedValue, array $context)
@@ -540,12 +540,12 @@ private function denormalizationSuccess($denormalizedValue, array $context)
540540
return $denormalizedValue;
541541
}
542542

543-
private function denormalizationFailure($normalizedValue, string $attribute, string $message, array $context)
543+
private function denormalizationFailure($normalizedValue, string $message, array $context)
544544
{
545545
if ($context[self::COLLECT_INVARIANT_VIOLATIONS] ?? false) {
546546
$violation = new InvariantViolation($normalizedValue, $message);
547547

548-
return DenormalizationResult::failure([$attribute => [$violation]]);
548+
return DenormalizationResult::failure(['' => [$violation]]);
549549
}
550550

551551
throw new NotNormalizableValueException($message);

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ public function denormalize($data, string $type, string $format = null, array $c
202202
throw new NotNormalizableValueException($message);
203203
}
204204

205+
if ($context[self::COLLECT_INVARIANT_VIOLATIONS] ?? false) {
206+
return DenormalizationResult::success($data);
207+
}
208+
205209
return $data;
206210
}
207211

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Serializer\Tests;
6+
7+
use Exception;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\Serializer\InvariantViolation;
10+
11+
class InvariantViolationTest extends TestCase
12+
{
13+
public function testItRepresentsAnInvariantViolation(): void
14+
{
15+
$exception = new Exception();
16+
17+
$violation = new InvariantViolation('foo', '"foo" is not an integer.', $exception);
18+
19+
self::assertSame('foo', $violation->getNormalizedValue());
20+
self::assertSame('"foo" is not an integer.', $violation->getMessage());
21+
self::assertSame($exception, $violation->getException());
22+
}
23+
}

src/Symfony/Component/Serializer/Tests/Normalizer/DataUriNormalizerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpFoundation\File\File;
16+
use Symfony\Component\Serializer\DenormalizationResult;
1617
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
18+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1719

1820
/**
1921
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -169,6 +171,34 @@ public function validUriProvider()
169171
];
170172
}
171173

174+
/**
175+
* @dataProvider validUriProvider
176+
*/
177+
public function testItDenormalizesAndReturnsSuccessResult($uri): void
178+
{
179+
$result = $this->normalizer->denormalize($uri, \SplFileObject::class, null, [
180+
DenormalizerInterface::COLLECT_INVARIANT_VIOLATIONS => true,
181+
]);
182+
183+
self::assertInstanceOf(DenormalizationResult::class, $result);
184+
self::assertTrue($result->isSucessful());
185+
self::assertInstanceOf(\SplFileObject::class, $result->getDenormalizedValue());
186+
}
187+
188+
public function testItDenormalizesAndReturnsFailureResult(): void
189+
{
190+
$result = $this->normalizer->denormalize('not-a-uri', \SplFileObject::class, null, [
191+
DenormalizerInterface::COLLECT_INVARIANT_VIOLATIONS => true,
192+
]);
193+
194+
self::assertInstanceOf(DenormalizationResult::class, $result);
195+
self::assertFalse($result->isSucessful());
196+
self::assertSame(
197+
['' => ['The provided "data:" URI is not valid.']],
198+
$result->getInvariantViolationMessages()
199+
);
200+
}
201+
172202
private function getContent(\SplFileObject $file)
173203
{
174204
$buffer = '';

src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\DenormalizationResult;
1516
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
17+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1618

1719
/**
1820
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -259,4 +261,29 @@ public function testDenormalizeFormatMismatchThrowsException()
259261
$this->expectException('Symfony\Component\Serializer\Exception\UnexpectedValueException');
260262
$this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d|']);
261263
}
264+
265+
public function testItDenormalizesAndReturnsSuccessResult(): void
266+
{
267+
$result = $this->normalizer->denormalize('2020-01-01', \DateTimeInterface::class, null, [
268+
DenormalizerInterface::COLLECT_INVARIANT_VIOLATIONS => true,
269+
]);
270+
271+
self::assertInstanceOf(DenormalizationResult::class, $result);
272+
self::assertTrue($result->isSucessful());
273+
self::assertEquals(new \DateTime('2020-01-01'), $result->getDenormalizedValue());
274+
}
275+
276+
public function testItDenormalizesAndReturnsFailureResult(): void
277+
{
278+
$result = $this->normalizer->denormalize('not-a-date', \DateTimeInterface::class, null, [
279+
DenormalizerInterface::COLLECT_INVARIANT_VIOLATIONS => true,
280+
]);
281+
282+
self::assertInstanceOf(DenormalizationResult::class, $result);
283+
self::assertFalse($result->isSucessful());
284+
self::assertSame(
285+
['' => ['DateTimeImmutable::__construct(): Failed to parse time string (not-a-date) at position 0 (n): The timezone could not be found in the database']],
286+
$result->getInvariantViolationMessages()
287+
);
288+
}
262289
}

src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace Symfony\Component\Serializer\Tests\Normalizer;
44

55
use PHPUnit\Framework\TestCase;
6-
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
6+
use Symfony\Component\Serializer\DenormalizationResult;
7+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
78
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
89
use Symfony\Component\Uid\AbstractUid;
910
use Symfony\Component\Uid\Ulid;
@@ -86,4 +87,51 @@ public function testDenormalize($uuidString, $class)
8687
$this->assertEquals(Uuid::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class));
8788
}
8889
}
90+
91+
/**
92+
* @dataProvider dataProvider
93+
*/
94+
public function testItDenormalizesAndReturnsSuccessResult(string $uuidString, string $class): void
95+
{
96+
$result = $this->normalizer->denormalize($uuidString, $class, null, [
97+
DenormalizerInterface::COLLECT_INVARIANT_VIOLATIONS => true,
98+
]);
99+
100+
self::assertInstanceOf(DenormalizationResult::class, $result);
101+
self::assertTrue($result->isSucessful());
102+
self::assertEquals(
103+
Ulid::class === $class ? new Ulid($uuidString) : Uuid::fromString($uuidString),
104+
$result->getDenormalizedValue()
105+
);
106+
}
107+
108+
/**
109+
* @dataProvider failureDataProvider
110+
*/
111+
public function testItDenormalizesAndReturnsFailureResult(string $class): void
112+
{
113+
$result = $this->normalizer->denormalize('not-an-uuid', $class, null, [
114+
DenormalizerInterface::COLLECT_INVARIANT_VIOLATIONS => true,
115+
]);
116+
117+
self::assertInstanceOf(DenormalizationResult::class, $result);
118+
self::assertFalse($result->isSucessful());
119+
self::assertSame(
120+
['' => [sprintf('The data is not a valid "%s" string representation.', $class)]],
121+
$result->getInvariantViolationMessages()
122+
);
123+
}
124+
125+
public function failureDataProvider(): iterable
126+
{
127+
return [
128+
[UuidV1::class],
129+
[UuidV3::class],
130+
[UuidV4::class],
131+
[UuidV5::class],
132+
[UuidV6::class],
133+
[AbstractUid::class],
134+
[Ulid::class],
135+
];
136+
}
89137
}

0 commit comments

Comments
 (0)