From a91ca47c1ffa237d8b2dc804ffb7ca3c4794c737 Mon Sep 17 00:00:00 2001 From: Uplink03 <1977375+Uplink03@users.noreply.github.com> Date: Mon, 2 Oct 2023 22:36:18 +0100 Subject: [PATCH 1/3] Fix ChoiceField when entity is enumType and no choices have been given explicitly --- src/Field/Configurator/ChoiceConfigurator.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Field/Configurator/ChoiceConfigurator.php b/src/Field/Configurator/ChoiceConfigurator.php index 8db5e14430..cc105abc01 100644 --- a/src/Field/Configurator/ChoiceConfigurator.php +++ b/src/Field/Configurator/ChoiceConfigurator.php @@ -52,7 +52,10 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c $enumTypeClass = $field->getDoctrineMetadata()->get('enumType'); if (0 === \count($choices) && null !== $enumTypeClass && enum_exists($enumTypeClass)) { $choices = $enumTypeClass::cases(); - } elseif ($allChoicesAreEnums) { + $allChoicesAreEnums = true; + } + + if ($allChoicesAreEnums) { $processedEnumChoices = []; foreach ($choices as $choice) { if ($choice instanceof \BackedEnum) { From 8774da3634bda9427ec3d0150118f3ae30ef8c1c Mon Sep 17 00:00:00 2001 From: Uplink03 <1977375+Uplink03@users.noreply.github.com> Date: Mon, 2 Oct 2023 23:08:07 +0100 Subject: [PATCH 2/3] Fix ChoiceConfigurator with enums Apply the solution provided by the reporter of https://github.com/EasyCorp/EasyAdminBundle/issues/5641 --- src/Field/Configurator/ChoiceConfigurator.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Field/Configurator/ChoiceConfigurator.php b/src/Field/Configurator/ChoiceConfigurator.php index cc105abc01..03a6aad806 100644 --- a/src/Field/Configurator/ChoiceConfigurator.php +++ b/src/Field/Configurator/ChoiceConfigurator.php @@ -58,11 +58,7 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c if ($allChoicesAreEnums) { $processedEnumChoices = []; foreach ($choices as $choice) { - if ($choice instanceof \BackedEnum) { - $processedEnumChoices[$choice->name] = $choice->value; - } else { - $processedEnumChoices[$choice->name] = $choice->name; - } + $processedEnumChoices[$choice->name] = $choice; } $choices = $processedEnumChoices; From 81175ac05e3e3087738f65114e16a65a7152b49e Mon Sep 17 00:00:00 2001 From: Radu Cristescu Date: Mon, 2 Oct 2023 23:37:14 +0100 Subject: [PATCH 3/3] Updated ChoiceConfiguratorTest to work with the ChoiceConfigurator fixes --- .idea/.gitignore | 8 ++ .idea/EasyAdminBundle-fork.iml | 93 ++++++++++++++ .idea/encodings.xml | 6 + .idea/inspectionProfiles/Project_Default.xml | 6 + .idea/modules.xml | 8 ++ .idea/php.xml | 113 ++++++++++++++++++ .idea/symfony2.xml | 6 + .idea/vcs.xml | 6 + .../Configurator/ChoiceConfiguratorTest.php | 18 ++- 9 files changed, 260 insertions(+), 4 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/EasyAdminBundle-fork.iml create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/php.xml create mode 100644 .idea/symfony2.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..13566b81b0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/EasyAdminBundle-fork.iml b/.idea/EasyAdminBundle-fork.iml new file mode 100644 index 0000000000..486d44ac80 --- /dev/null +++ b/.idea/EasyAdminBundle-fork.iml @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000000..97626ba454 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000000..576d84cdb9 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..5f3066799e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000000..21e1b12aad --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/symfony2.xml b/.idea/symfony2.xml new file mode 100644 index 0000000000..bd98e4094e --- /dev/null +++ b/.idea/symfony2.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..35eb1ddfbb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/tests/Field/Configurator/ChoiceConfiguratorTest.php b/tests/Field/Configurator/ChoiceConfiguratorTest.php index 57cb832ea0..889413a0f7 100644 --- a/tests/Field/Configurator/ChoiceConfiguratorTest.php +++ b/tests/Field/Configurator/ChoiceConfiguratorTest.php @@ -51,7 +51,12 @@ public function testBackedEnumTypeChoices(): void $field = ChoiceField::new(self::PROPERTY_NAME); $field->getAsDto()->setDoctrineMetadata(['enumType' => StatusBackedEnum::class]); - $this->assertSame($this->configure($field)->getFormTypeOption('choices'), StatusBackedEnum::cases()); + $formChoices = array_combine( + array_column(StatusBackedEnum::cases(), 'name'), + StatusBackedEnum::cases(), + ); + + $this->assertSame($this->configure($field)->getFormTypeOption('choices'), $formChoices); } public function testBackedEnumChoices(): void @@ -63,7 +68,7 @@ public function testBackedEnumChoices(): void $expected = []; foreach (StatusBackedEnum::cases() as $case) { - $expected[$case->name] = $case->value; + $expected[$case->name] = $case; } $this->assertSame($this->configure($field)->getFormTypeOption('choices'), $expected); @@ -76,7 +81,12 @@ public function testUnitEnumTypeChoices(): void $field = ChoiceField::new(self::PROPERTY_NAME); $field->getAsDto()->setDoctrineMetadata(['enumType' => PriorityUnitEnum::class]); - $this->assertSame($this->configure($field)->getFormTypeOption('choices'), PriorityUnitEnum::cases()); + $formChoices = array_combine( + array_column(PriorityUnitEnum::cases(), 'name'), + PriorityUnitEnum::cases(), + ); + + $this->assertSame($this->configure($field)->getFormTypeOption('choices'), $formChoices); } public function testUnitEnumChoices(): void @@ -88,7 +98,7 @@ public function testUnitEnumChoices(): void $expected = []; foreach (PriorityUnitEnum::cases() as $case) { - $expected[$case->name] = $case->name; + $expected[$case->name] = $case; } $this->assertSame($this->configure($field)->getFormTypeOption('choices'), $expected);