|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Unit\Filter; |
| 4 | + |
| 5 | +use EasyCorp\Bundle\EasyAdminBundle\Filter\ChoiceFilter; |
| 6 | +use EasyCorp\Bundle\EasyAdminBundle\Tests\Unit\Filter\Fixtures\BackedEnumChoice; |
| 7 | +use EasyCorp\Bundle\EasyAdminBundle\Tests\Unit\Filter\Fixtures\TranslatableBackedEnumChoice; |
| 8 | +use EasyCorp\Bundle\EasyAdminBundle\Tests\Unit\Filter\Fixtures\TranslatableUnitEnumChoice; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Symfony\Component\Translation\TranslatableMessage; |
| 11 | + |
| 12 | +class ChoiceFilterTest extends TestCase |
| 13 | +{ |
| 14 | + public function testSetTranslatableChoicesWithTranslatableMessages(): void |
| 15 | + { |
| 16 | + $filter = ChoiceFilter::new('status')->setTranslatableChoices([ |
| 17 | + 'open' => new TranslatableMessage('Open'), |
| 18 | + 'closed' => new TranslatableMessage('Closed'), |
| 19 | + ]); |
| 20 | + $dto = $filter->getAsDto(); |
| 21 | + |
| 22 | + $this->assertSame(['open', 'closed'], $dto->getFormTypeOption('value_type_options.choices')); |
| 23 | + |
| 24 | + $choiceLabel = $dto->getFormTypeOption('value_type_options.choice_label'); |
| 25 | + $this->assertIsCallable($choiceLabel); |
| 26 | + $this->assertInstanceOf(TranslatableMessage::class, $choiceLabel('open')); |
| 27 | + $this->assertInstanceOf(TranslatableMessage::class, $choiceLabel('closed')); |
| 28 | + } |
| 29 | + |
| 30 | + public function testSetTranslatableChoicesWithTranslatableBackedEnumCases(): void |
| 31 | + { |
| 32 | + $filter = ChoiceFilter::new('status')->setTranslatableChoices(TranslatableBackedEnumChoice::cases()); |
| 33 | + $dto = $filter->getAsDto(); |
| 34 | + |
| 35 | + // the submitted values must be the enum backing values, not the list indices (0, 1, ...) |
| 36 | + $this->assertSame(['draft', 'published'], $dto->getFormTypeOption('value_type_options.choices')); |
| 37 | + |
| 38 | + $choiceLabel = $dto->getFormTypeOption('value_type_options.choice_label'); |
| 39 | + $this->assertIsCallable($choiceLabel); |
| 40 | + $this->assertSame(TranslatableBackedEnumChoice::Draft, $choiceLabel('draft')); |
| 41 | + $this->assertSame(TranslatableBackedEnumChoice::Published, $choiceLabel('published')); |
| 42 | + } |
| 43 | + |
| 44 | + public function testSetTranslatableChoicesWithBackedEnumCases(): void |
| 45 | + { |
| 46 | + $filter = ChoiceFilter::new('status')->setTranslatableChoices(BackedEnumChoice::cases()); |
| 47 | + $dto = $filter->getAsDto(); |
| 48 | + |
| 49 | + $this->assertSame(['draft', 'published'], $dto->getFormTypeOption('value_type_options.choices')); |
| 50 | + |
| 51 | + $choiceLabel = $dto->getFormTypeOption('value_type_options.choice_label'); |
| 52 | + $this->assertSame(BackedEnumChoice::Draft, $choiceLabel('draft')); |
| 53 | + } |
| 54 | + |
| 55 | + public function testSetTranslatableChoicesWithTranslatableUnitEnumCases(): void |
| 56 | + { |
| 57 | + $filter = ChoiceFilter::new('status')->setTranslatableChoices(TranslatableUnitEnumChoice::cases()); |
| 58 | + $dto = $filter->getAsDto(); |
| 59 | + |
| 60 | + $this->assertSame(['Draft', 'Published'], $dto->getFormTypeOption('value_type_options.choices')); |
| 61 | + |
| 62 | + $choiceLabel = $dto->getFormTypeOption('value_type_options.choice_label'); |
| 63 | + $this->assertSame(TranslatableUnitEnumChoice::Draft, $choiceLabel('Draft')); |
| 64 | + } |
| 65 | +} |
0 commit comments