|
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\Priority; |
| 21 | +use Symfony\UX\LiveComponent\Tests\Fixtures\Enum\Status; |
20 | 22 | use Symfony\UX\LiveComponent\Tests\LiveComponentTestHelper;
|
21 | 23 | use Zenstruck\Foundry\Test\Factories;
|
22 | 24 | use Zenstruck\Foundry\Test\ResetDatabase;
|
@@ -279,4 +281,64 @@ public function testCanDehydrateAndHydrateComponentsWithEmptyAttributes(): void
|
279 | 281 |
|
280 | 282 | $this->assertSame([], $mounted->getAttributes()->all());
|
281 | 283 | }
|
| 284 | + |
| 285 | + /** |
| 286 | + * @requires PHP >= 8.1 |
| 287 | + */ |
| 288 | + public function testCanHydrateEnums(): void |
| 289 | + { |
| 290 | + $mounted = $this->mountComponent('with_enum', ['priority' => Priority::HIGH, 'status' => Status::ACTIVE]); |
| 291 | + |
| 292 | + $dehydrated = $this->dehydrateComponent($mounted); |
| 293 | + |
| 294 | + $this->assertSame(10, $dehydrated['priority']); |
| 295 | + $this->assertSame('active', $dehydrated['status']); |
| 296 | + |
| 297 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 298 | + |
| 299 | + $this->assertSame(Priority::HIGH, $mounted->getComponent()->priority); |
| 300 | + $this->assertSame(Status::ACTIVE, $mounted->getComponent()->status); |
| 301 | + |
| 302 | + $dehydrated['priority'] = Priority::LOW->value; |
| 303 | + $dehydrated['status'] = Status::PENDING->value; |
| 304 | + |
| 305 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 306 | + |
| 307 | + $this->assertSame(Priority::LOW, $mounted->getComponent()->priority); |
| 308 | + $this->assertSame(Status::PENDING, $mounted->getComponent()->status); |
| 309 | + } |
| 310 | + |
| 311 | + /** |
| 312 | + * @requires PHP >= 8.1 |
| 313 | + */ |
| 314 | + public function testCanHydrateEnumsThatAreNull(): void |
| 315 | + { |
| 316 | + $mounted = $this->mountComponent('with_enum'); |
| 317 | + |
| 318 | + $dehydrated = $this->dehydrateComponent($mounted); |
| 319 | + |
| 320 | + $this->assertNull($dehydrated['priority']); |
| 321 | + $this->assertNull($dehydrated['status']); |
| 322 | + |
| 323 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 324 | + |
| 325 | + $this->assertNull($mounted->getComponent()->priority); |
| 326 | + $this->assertNull($mounted->getComponent()->status); |
| 327 | + |
| 328 | + $dehydrated['priority'] = Priority::LOW->value; |
| 329 | + $dehydrated['status'] = Status::PENDING->value; |
| 330 | + |
| 331 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 332 | + |
| 333 | + $this->assertSame(Priority::LOW, $mounted->getComponent()->priority); |
| 334 | + $this->assertSame(Status::PENDING, $mounted->getComponent()->status); |
| 335 | + |
| 336 | + $dehydrated['priority'] = null; |
| 337 | + $dehydrated['status'] = null; |
| 338 | + |
| 339 | + $mounted = $this->hydrateComponent($this->getComponent('with_enum'), $dehydrated, $mounted->getName()); |
| 340 | + |
| 341 | + $this->assertNull($mounted->getComponent()->priority); |
| 342 | + $this->assertNull($mounted->getComponent()->status); |
| 343 | + } |
282 | 344 | }
|
0 commit comments