|
19 | 19 | use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
|
20 | 20 | use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component3;
|
21 | 21 | use Symfony\UX\LiveComponent\Tests\Fixture\Entity\Entity1;
|
| 22 | +use Symfony\UX\TwigComponent\ComponentAttributes; |
22 | 23 | use Symfony\UX\TwigComponent\ComponentFactory;
|
| 24 | +use Symfony\UX\TwigComponent\HasAttributesTrait; |
23 | 25 | use function Zenstruck\Foundry\create;
|
24 | 26 | use Zenstruck\Foundry\Test\Factories;
|
25 | 27 | use Zenstruck\Foundry\Test\ResetDatabase;
|
@@ -265,4 +267,32 @@ public function testCanDehydrateAndHydrateArrays(): void
|
265 | 267 |
|
266 | 268 | $this->assertSame($instance->prop, $component->prop);
|
267 | 269 | }
|
| 270 | + |
| 271 | + public function testCanDehydrateAndHydrateComponentsWithAttributes(): void |
| 272 | + { |
| 273 | + if (!class_exists(ComponentAttributes::class)) { |
| 274 | + $this->markTestSkipped('Attributes trait not available.'); |
| 275 | + } |
| 276 | + |
| 277 | + /** @var LiveComponentHydrator $hydrator */ |
| 278 | + $hydrator = self::getContainer()->get('ux.live_component.component_hydrator'); |
| 279 | + |
| 280 | + $component = new class() { |
| 281 | + use HasAttributesTrait; |
| 282 | + }; |
| 283 | + $instance = clone $component; |
| 284 | + $instance->attributes = new ComponentAttributes($attributes = ['class' => 'foo']); |
| 285 | + |
| 286 | + $this->assertSame($attributes, $instance->attributes->all()); |
| 287 | + |
| 288 | + $dehydrated = $hydrator->dehydrate($instance); |
| 289 | + |
| 290 | + $this->assertArrayHasKey('attributes', $dehydrated); |
| 291 | + $this->assertSame($attributes, $dehydrated['attributes']); |
| 292 | + $this->assertFalse(isset($component->prop)); |
| 293 | + |
| 294 | + $hydrator->hydrate($component, $dehydrated); |
| 295 | + |
| 296 | + $this->assertSame($attributes, $component->attributes->all()); |
| 297 | + } |
268 | 298 | }
|
0 commit comments