Skip to content

Commit bb46a2e

Browse files
committed
deserializer metadata tests wip
1 parent d1f98de commit bb46a2e

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

src/Symfony/Component/Serializer/Deserialize/Mapping/TypePropertyMetadataLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function load(string $className, DeserializeConfig $config, array $contex
4444
$metadata = $metadata->withType($this->typeGenericsHelper->replaceGenericTypes($type, $genericTypes));
4545
}
4646

47-
if ($type->isObject() && $type->hasClass() && is_a(\DateTimeInterface::class, $type->className(), true)) {
47+
if ($type->isObject() && $type->hasClass() && is_a($type->className(), \DateTimeInterface::class, true)) {
4848
$metadata = $metadata
4949
->withType(Type::string())
5050
->withFormatter(self::castStringToDateTime(...));

src/Symfony/Component/Serializer/Serialize/Mapping/TypePropertyMetadataLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function load(string $className, SerializeConfig $config, array $context)
4545
$metadata = $metadata->withType($this->typeGenericsHelper->replaceGenericTypes($type, $genericTypes));
4646
}
4747

48-
if ($type->isObject() && $type->hasClass() && is_a(\DateTimeInterface::class, $type->className(), true)) {
48+
if ($type->isObject() && $type->hasClass() && is_a($type->className(), \DateTimeInterface::class, true)) {
4949
$metadata = $metadata
5050
->withType(Type::string())
5151
->withFormatter(self::castDateTimeToString(...));

src/Symfony/Component/Serializer/Tests/Deserialize/Metadata/PropertyMetadataTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@
1212
namespace Symfony\Component\Serializer\Tests\Deserialize\Metadata;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Deserialize\Mapping\PropertyMetadata;
16+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
17+
use Symfony\Component\Serializer\Tests\Fixtures\Dto\DummyWithMethods;
18+
use Symfony\Component\Serializer\Type\Type;
1519

1620
class PropertyMetadataTest extends TestCase
1721
{
22+
public function testThrowOnNonStaticFormatter()
23+
{
24+
$this->expectException(InvalidArgumentException::class);
25+
new PropertyMetadata('useless', Type::mixed(), [(new DummyWithMethods())->nonStatic(...)]);
26+
}
27+
28+
public function testThrowOnNonAnonymousFormatter()
29+
{
30+
$this->expectException(InvalidArgumentException::class);
31+
new PropertyMetadata('useless', Type::mixed(), [fn () => 'useless']);
32+
}
1833
}

src/Symfony/Component/Serializer/Tests/Deserialize/Metadata/TypePropertyMetadataLoaderTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,77 @@
1212
namespace Symfony\Component\Serializer\Tests\Deserialize\Metadata;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Deserialize\Config\DeserializeConfig;
16+
use Symfony\Component\Serializer\Deserialize\Mapping\PropertyMetadata;
17+
use Symfony\Component\Serializer\Deserialize\Mapping\PropertyMetadataLoaderInterface;
18+
use Symfony\Component\Serializer\Deserialize\Mapping\TypePropertyMetadataLoader;
19+
use Symfony\Component\Serializer\Tests\Fixtures\Dto\DummyWithGenerics;
20+
use Symfony\Component\Serializer\Type\Type;
21+
use Symfony\Component\Serializer\Type\TypeExtractorInterface;
1522

1623
class TypePropertyMetadataLoaderTest extends TestCase
1724
{
25+
public function testCastDateTimeToString()
26+
{
27+
$loader = new TypePropertyMetadataLoader(self::propertyMetadataLoader([
28+
'foo' => new PropertyMetadata('foo', Type::class(\DateTimeImmutable::class), []),
29+
]), $this->createStub(TypeExtractorInterface::class));
30+
31+
$metadata = $loader->load(self::class, new DeserializeConfig(), ['original_type' => Type::fromString('useless')]);
32+
33+
$this->assertEquals([
34+
'foo' => new PropertyMetadata('foo', Type::string(), [
35+
\Closure::fromCallable(TypePropertyMetadataLoader::castStringToDateTime(...)),
36+
]),
37+
], $metadata);
38+
39+
$formatter = $metadata['foo']->formatters()[0];
40+
41+
$this->assertEquals(
42+
new \DateTimeImmutable('2023-07-26'),
43+
$formatter('2023-07-26', new DeserializeConfig()),
44+
);
45+
46+
$this->assertEquals(
47+
(new \DateTimeImmutable('2023-07-26'))->setTime(0, 0),
48+
$formatter('26/07/2023 00:00:00', (new DeserializeConfig())->withDateTimeFormat('d/m/Y H:i:s')),
49+
);
50+
}
51+
52+
public function testReplaceGenerics()
53+
{
54+
$typeExtractor = $this->createStub(TypeExtractorInterface::class);
55+
$typeExtractor->method('extractTemplateFromClass')->willReturn(['T']);
56+
57+
$loader = new TypePropertyMetadataLoader(self::propertyMetadataLoader([
58+
'foo' => new PropertyMetadata('foo', Type::fromString('T'), []),
59+
]), $typeExtractor);
60+
61+
$metadata = $loader->load(
62+
DummyWithGenerics::class,
63+
new DeserializeConfig(),
64+
['original_type' => Type::class(DummyWithGenerics::class, genericParameterTypes: [Type::int()])],
65+
);
66+
67+
$this->assertEquals([
68+
'foo' => new PropertyMetadata('foo', Type::int(), []),
69+
], $metadata);
70+
}
71+
72+
/**
73+
* @param array<string, PropertyMetadata> $propertiesMetadata
74+
*/
75+
private static function propertyMetadataLoader(array $propertiesMetadata = []): PropertyMetadataLoaderInterface
76+
{
77+
return new class($propertiesMetadata) implements PropertyMetadataLoaderInterface {
78+
public function __construct(private readonly array $propertiesMetadata)
79+
{
80+
}
81+
82+
public function load(string $className, DeserializeConfig $config, array $context): array
83+
{
84+
return $this->propertiesMetadata;
85+
}
86+
};
87+
}
1888
}

0 commit comments

Comments
 (0)