|
12 | 12 | namespace Symfony\Component\Serializer\Tests\Deserialize\Metadata;
|
13 | 13 |
|
14 | 14 | 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; |
15 | 22 |
|
16 | 23 | class TypePropertyMetadataLoaderTest extends TestCase
|
17 | 24 | {
|
| 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 | + } |
18 | 88 | }
|
0 commit comments