|
12 | 12 | namespace Symfony\UX\LiveComponent\Tests\Unit; |
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\PropertyAccess\PropertyAccess; |
15 | 16 | use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
16 | 17 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
17 | 18 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
|
20 | 21 | use Symfony\UX\LiveComponent\Attribute\LiveProp; |
21 | 22 | use Symfony\UX\LiveComponent\LiveComponentHydrator; |
22 | 23 | use Symfony\UX\LiveComponent\Metadata\LegacyLivePropMetadata; |
| 24 | +use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadata; |
23 | 25 | use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory; |
24 | 26 | use Symfony\UX\LiveComponent\Metadata\LivePropMetadata; |
| 27 | +use Symfony\UX\LiveComponent\Tests\Fixtures\Component\ComponentWithHydrateWithProps; |
| 28 | +use Symfony\UX\TwigComponent\ComponentMetadata; |
25 | 29 | use Twig\Environment; |
| 30 | +use Twig\Runtime\EscaperRuntime; |
26 | 31 |
|
27 | 32 | final class LiveComponentHydratorTest extends TestCase |
28 | 33 | { |
@@ -80,6 +85,76 @@ public function testItCanHydrateWithNullValues() |
80 | 85 | self::assertNull($hydratedValue); |
81 | 86 | } |
82 | 87 | } |
| 88 | + |
| 89 | + public function testHydrateWithIsCalledWithNestedArrayAsProps() |
| 90 | + { |
| 91 | + $twigMock = $this->createMock(Environment::class); |
| 92 | + $twigMock->method('getRuntime')->willReturn(new EscaperRuntime()); |
| 93 | + $hydrator = new LiveComponentHydrator( |
| 94 | + [], |
| 95 | + PropertyAccess::createPropertyAccessor(), |
| 96 | + $this->createMock(LiveComponentMetadataFactory::class), |
| 97 | + new Serializer(normalizers: [new ObjectNormalizer()]), |
| 98 | + 'foo', |
| 99 | + $twigMock, |
| 100 | + ); |
| 101 | + $component = new ComponentWithHydrateWithProps(); |
| 102 | + |
| 103 | + // BC layer when "symfony/type-info" is not available |
| 104 | + if (!class_exists(Type::class)) { |
| 105 | + $hydrator->hydrate( |
| 106 | + $component, |
| 107 | + [ |
| 108 | + '@checksum' => 'SNKH1tHUgwgWT8V+Z/A7z126JQ2dWGiG6xVmjx7FbeA=', |
| 109 | + 'integers' => [ |
| 110 | + 'one' => 1, |
| 111 | + 'two' => 2, |
| 112 | + 'three' => 3, |
| 113 | + ], |
| 114 | + ], |
| 115 | + [ |
| 116 | + 'integers.one' => '4', |
| 117 | + 'integers.two' => '5', |
| 118 | + 'integers.three' => '6', |
| 119 | + ], |
| 120 | + new LiveComponentMetadata( |
| 121 | + new ComponentMetadata([]), |
| 122 | + [ |
| 123 | + new LegacyLivePropMetadata('integers', new LiveProp(writable: true, hydrateWith: 'hydrateIntegers'), typeName: 'array', isBuiltIn: true, allowsNull: false, collectionValueType: new \Symfony\Component\PropertyInfo\Type('int')), |
| 124 | + ], |
| 125 | + ), |
| 126 | + ); |
| 127 | + } else { |
| 128 | + $hydrator->hydrate( |
| 129 | + $component, |
| 130 | + [ |
| 131 | + '@checksum' => 'SNKH1tHUgwgWT8V+Z/A7z126JQ2dWGiG6xVmjx7FbeA=', |
| 132 | + 'integers' => [ |
| 133 | + 'one' => 1, |
| 134 | + 'two' => 2, |
| 135 | + 'three' => 3, |
| 136 | + ], |
| 137 | + ], |
| 138 | + [ |
| 139 | + 'integers.one' => '4', |
| 140 | + 'integers.two' => '5', |
| 141 | + 'integers.three' => '6', |
| 142 | + ], |
| 143 | + new LiveComponentMetadata( |
| 144 | + new ComponentMetadata([]), |
| 145 | + [ |
| 146 | + new LivePropMetadata('integers', new LiveProp(writable: true, hydrateWith: 'hydrateIntegers'), Type::array(Type::int(), Type::string())), |
| 147 | + ], |
| 148 | + ), |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + self::assertSame([ |
| 153 | + 'one' => 4, |
| 154 | + 'two' => 5, |
| 155 | + 'three' => 6, |
| 156 | + ], $component->integers); |
| 157 | + } |
83 | 158 | } |
84 | 159 |
|
85 | 160 | class Foo |
|
0 commit comments