|
18 | 18 | use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
|
19 | 19 | use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component3;
|
20 | 20 | use Symfony\UX\LiveComponent\Tests\Fixture\Entity\Entity1;
|
| 21 | +use Symfony\UX\TwigComponent\ComponentAttributes; |
21 | 22 | use Symfony\UX\TwigComponent\ComponentFactory;
|
| 23 | +use Symfony\UX\TwigComponent\HasAttributesTrait; |
22 | 24 | use function Zenstruck\Foundry\create;
|
23 | 25 | use Zenstruck\Foundry\Test\Factories;
|
24 | 26 | use Zenstruck\Foundry\Test\ResetDatabase;
|
@@ -239,4 +241,32 @@ public function testCorrectlyUsesCustomFrontendNameInDehydrateAndHydrate(): void
|
239 | 241 | $this->assertSame('value1', $component->prop1);
|
240 | 242 | $this->assertSame('value2', $component->prop2);
|
241 | 243 | }
|
| 244 | + |
| 245 | + public function testCanDehydrateAndHydrateComponentsWithAttributes(): void |
| 246 | + { |
| 247 | + if (!class_exists(ComponentAttributes::class)) { |
| 248 | + $this->markTestSkipped('Attributes trait not available.'); |
| 249 | + } |
| 250 | + |
| 251 | + /** @var LiveComponentHydrator $hydrator */ |
| 252 | + $hydrator = self::getContainer()->get('ux.live_component.component_hydrator'); |
| 253 | + |
| 254 | + $component = new class() { |
| 255 | + use HasAttributesTrait; |
| 256 | + }; |
| 257 | + $instance = clone $component; |
| 258 | + $instance->attributes = new ComponentAttributes($attributes = ['class' => 'foo']); |
| 259 | + |
| 260 | + $this->assertSame($attributes, $instance->attributes->all()); |
| 261 | + |
| 262 | + $dehydrated = $hydrator->dehydrate($instance); |
| 263 | + |
| 264 | + $this->assertArrayHasKey('attributes', $dehydrated); |
| 265 | + $this->assertSame($attributes, $dehydrated['attributes']); |
| 266 | + $this->assertFalse(isset($component->prop)); |
| 267 | + |
| 268 | + $hydrator->hydrate($component, $dehydrated); |
| 269 | + |
| 270 | + $this->assertSame($attributes, $component->attributes->all()); |
| 271 | + } |
242 | 272 | }
|
0 commit comments