|
17 | 17 | use Symfony\UX\LiveComponent\Tests\Fixtures\Component\Component3; |
18 | 18 | use Symfony\UX\LiveComponent\Tests\Fixtures\Component\ComponentWithArrayProp; |
19 | 19 | use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\Entity1; |
| 20 | +use Symfony\UX\LiveComponent\Tests\Fixtures\Enum\EmptyStringEnum; |
| 21 | +use Symfony\UX\LiveComponent\Tests\Fixtures\Enum\IntEnum; |
| 22 | +use Symfony\UX\LiveComponent\Tests\Fixtures\Enum\StringEnum; |
| 23 | +use Symfony\UX\LiveComponent\Tests\Fixtures\Enum\ZeroIntEnum; |
20 | 24 | use Symfony\UX\LiveComponent\Tests\LiveComponentTestHelper; |
21 | 25 | use Zenstruck\Foundry\Test\Factories; |
22 | 26 | use Zenstruck\Foundry\Test\ResetDatabase; |
@@ -279,4 +283,60 @@ public function testCanDehydrateAndHydrateComponentsWithEmptyAttributes(): void |
279 | 283 |
|
280 | 284 | $this->assertSame([], $mounted->getAttributes()->all()); |
281 | 285 | } |
| 286 | + |
| 287 | + /** |
| 288 | + * @requires PHP >= 8.1 |
| 289 | + */ |
| 290 | + public function testCanHydrateEnums(): void |
| 291 | + { |
| 292 | + $mounted = $this->mountComponent('with_enum'); |
| 293 | + |
| 294 | + $dehydrated = $this->dehydrateComponent($mounted); |
| 295 | + |
| 296 | + $this->assertNull($dehydrated['int']); |
| 297 | + $this->assertNull($dehydrated['string']); |
| 298 | + |
| 299 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 300 | + |
| 301 | + $this->assertNull($mounted->getComponent()->int); |
| 302 | + $this->assertNull($mounted->getComponent()->string); |
| 303 | + |
| 304 | + $dehydrated['int'] = IntEnum::LOW->value; |
| 305 | + $dehydrated['string'] = StringEnum::PENDING->value; |
| 306 | + |
| 307 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 308 | + |
| 309 | + $this->assertSame(IntEnum::LOW, $mounted->getComponent()->int); |
| 310 | + $this->assertSame(StringEnum::PENDING, $mounted->getComponent()->string); |
| 311 | + |
| 312 | + $dehydrated['int'] = null; |
| 313 | + $dehydrated['string'] = null; |
| 314 | + |
| 315 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 316 | + |
| 317 | + $this->assertNull($mounted->getComponent()->int); |
| 318 | + $this->assertNull($mounted->getComponent()->string); |
| 319 | + |
| 320 | + $dehydrated['int'] = ''; |
| 321 | + $dehydrated['string'] = ''; |
| 322 | + |
| 323 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 324 | + |
| 325 | + $this->assertNull($mounted->getComponent()->int); |
| 326 | + $this->assertNull($mounted->getComponent()->string); |
| 327 | + |
| 328 | + $dehydrated['zeroInt'] = 0; |
| 329 | + $dehydrated['emptyString'] = ''; |
| 330 | + |
| 331 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 332 | + |
| 333 | + $this->assertSame(ZeroIntEnum::ZERO, $mounted->getComponent()->zeroInt); |
| 334 | + $this->assertSame(EmptyStringEnum::EMPTY, $mounted->getComponent()->emptyString); |
| 335 | + |
| 336 | + $dehydrated['zeroInt'] = '0'; |
| 337 | + |
| 338 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 339 | + |
| 340 | + $this->assertSame(ZeroIntEnum::ZERO, $mounted->getComponent()->zeroInt); |
| 341 | + } |
282 | 342 | } |
0 commit comments