From b0c394425444275cc2f4566ec7cf2b2ead8bd032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Mon, 24 May 2021 14:26:07 +0200 Subject: [PATCH] Introduce EnumInterface::getLabel (#39) * Introduce EnumInterface::getLabel to not rely on getChoices from the outside * Store translated enum choices in a private property to avoid calling translation more than once per enum * Normalize assert method usage to static calls * Fixed doc use statements * #37 $choices assignment refacto + comment getLabel in EnumInterface for next major (#40) Co-authored-by: Mathieu Ducrot --- doc/declaring-enum.md | 12 ++++++ src/AbstractTranslatedEnum.php | 37 ++++++++++++------- src/ConfigurableEnum.php | 2 + src/EnumInterface.php | 15 ++++++++ src/EnumLabelTrait.php | 21 +++++++++++ .../CannotExtractConstantsException.php | 2 +- src/Exception/DuplicatedEnumException.php | 2 +- src/Exception/ExceptionInterface.php | 9 +++++ src/Exception/InvalidEnumException.php | 2 +- src/Exception/InvalidEnumValueException.php | 16 ++++++++ .../InvalidTranslatePatternException.php | 2 +- src/Twig/Extension/EnumExtension.php | 12 +++++- tests/ConfigurableEnumTest.php | 14 ++++++- tests/ConfigurableTranslatedEnumTest.php | 28 +++++++++++--- tests/ConstantListEnumTest.php | 16 ++++++++ tests/ConstantListTranslatedEnumTest.php | 35 ++++++++++++++---- .../DependencyInjection/EnumExtensionTest.php | 6 +-- tests/EnumRegistryTest.php | 18 ++++----- tests/Fixtures/GenderEnum.php | 14 +++++++ tests/Form/Extension/EnumTypeGuesserTest.php | 12 +++--- tests/Form/Type/EnumTypeTest.php | 2 +- tests/Twig/Extension/EnumExtensionTest.php | 16 ++++---- 22 files changed, 234 insertions(+), 59 deletions(-) create mode 100644 src/EnumLabelTrait.php create mode 100644 src/Exception/ExceptionInterface.php create mode 100644 src/Exception/InvalidEnumValueException.php diff --git a/doc/declaring-enum.md b/doc/declaring-enum.md index 5a6a983..faea2e6 100644 --- a/doc/declaring-enum.md +++ b/doc/declaring-enum.md @@ -30,6 +30,7 @@ Create a new class, implement both `getName` & `getChoices` methods. namespace App\Enum; use Yokai\EnumBundle\EnumInterface; +use Yokai\EnumBundle\Exception\InvalidEnumValueException; class GenderEnum implements EnumInterface { @@ -42,6 +43,15 @@ class GenderEnum implements EnumInterface { return ['m' => 'Male', 'f' => 'Female']; } + + public function getLabel(string $value): string + { + if (!isset($this->getChoices()[$value])) { + throw InvalidEnumValueException::create($this, $value); + } + + return $this->getChoices()[$value]; + } } ``` @@ -64,11 +74,13 @@ Create a new class, use `EnumWithClassAsNameTrait` trait and implement `getChoic namespace App\Enum; use Yokai\EnumBundle\EnumInterface; +use Yokai\EnumBundle\EnumLabelTrait; use Yokai\EnumBundle\EnumWithClassAsNameTrait; class GenderEnum implements EnumInterface { use EnumWithClassAsNameTrait; + use EnumLabelTrait; public function getChoices(): array { diff --git a/src/AbstractTranslatedEnum.php b/src/AbstractTranslatedEnum.php index 7e9a0f3..e26e420 100644 --- a/src/AbstractTranslatedEnum.php +++ b/src/AbstractTranslatedEnum.php @@ -12,6 +12,8 @@ */ abstract class AbstractTranslatedEnum implements EnumInterface { + use EnumLabelTrait; + /** * @var TranslatorInterface */ @@ -27,6 +29,11 @@ abstract class AbstractTranslatedEnum implements EnumInterface */ private $transDomain = 'messages'; + /** + * @var array|null + */ + private $choices = null; + /** * @param TranslatorInterface $translator * @param string $transPattern @@ -46,19 +53,23 @@ public function __construct(TranslatorInterface $translator, string $transPatter */ public function getChoices(): array { - return array_combine( - $this->getValues(), - array_map( - function (string $value): string { - return $this->translator->trans( - sprintf($this->transPattern, $value), - [], - $this->transDomain - ); - }, - $this->getValues() - ) - ); + if ($this->choices === null) { + $this->choices = array_combine( + $this->getValues(), + array_map( + function (string $value): string { + return $this->translator->trans( + sprintf($this->transPattern, $value), + [], + $this->transDomain + ); + }, + $this->getValues() + ) + ); + } + + return $this->choices; } /** diff --git a/src/ConfigurableEnum.php b/src/ConfigurableEnum.php index 73aee05..1ab9fc1 100644 --- a/src/ConfigurableEnum.php +++ b/src/ConfigurableEnum.php @@ -9,6 +9,8 @@ */ class ConfigurableEnum implements EnumInterface { + use EnumLabelTrait; + /** * @var string */ diff --git a/src/EnumInterface.php b/src/EnumInterface.php index 3bb2b56..a2532dd 100644 --- a/src/EnumInterface.php +++ b/src/EnumInterface.php @@ -6,16 +6,31 @@ /** * @author Yann Eugoné + * + * NEXT_MAJOR: Add all these methods to the interface by uncommenting them. + * + * @method string getLabel(string $value) */ interface EnumInterface { /** + * Returns enum choices (value as keys, labels as values) + * * @return array */ public function getChoices(): array; /** + * Returns enum identifier (must be unique across your app). + * * @return string */ public function getName(): string; + + /** + * NEXT_MAJOR: uncomment this method + * + * @return string + */ +// public function getLabel(string $value): string; } diff --git a/src/EnumLabelTrait.php b/src/EnumLabelTrait.php new file mode 100644 index 0000000..eb1bddb --- /dev/null +++ b/src/EnumLabelTrait.php @@ -0,0 +1,21 @@ +getChoices(); + if (!isset($choices[$value])) { + throw InvalidEnumValueException::create($this, $value); + } + + return $choices[$value]; + } +} diff --git a/src/Exception/CannotExtractConstantsException.php b/src/Exception/CannotExtractConstantsException.php index 5b5bc72..a4ec514 100644 --- a/src/Exception/CannotExtractConstantsException.php +++ b/src/Exception/CannotExtractConstantsException.php @@ -9,7 +9,7 @@ /** * @author Yann Eugoné */ -class CannotExtractConstantsException extends InvalidArgumentException +class CannotExtractConstantsException extends InvalidArgumentException implements ExceptionInterface { public static function invalidPattern(string $pattern): self { diff --git a/src/Exception/DuplicatedEnumException.php b/src/Exception/DuplicatedEnumException.php index 39eff58..0929d01 100644 --- a/src/Exception/DuplicatedEnumException.php +++ b/src/Exception/DuplicatedEnumException.php @@ -9,7 +9,7 @@ /** * @author Yann Eugoné */ -class DuplicatedEnumException extends BadMethodCallException +class DuplicatedEnumException extends BadMethodCallException implements ExceptionInterface { /** * @param string $name diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php new file mode 100644 index 0000000..bf77829 --- /dev/null +++ b/src/Exception/ExceptionInterface.php @@ -0,0 +1,9 @@ + */ -class InvalidEnumException extends DomainException +class InvalidEnumException extends DomainException implements ExceptionInterface { /** * @param string $name diff --git a/src/Exception/InvalidEnumValueException.php b/src/Exception/InvalidEnumValueException.php new file mode 100644 index 0000000..a574f13 --- /dev/null +++ b/src/Exception/InvalidEnumValueException.php @@ -0,0 +1,16 @@ +getName(), $value) + ); + } +} diff --git a/src/Exception/InvalidTranslatePatternException.php b/src/Exception/InvalidTranslatePatternException.php index d89ae43..06734d6 100644 --- a/src/Exception/InvalidTranslatePatternException.php +++ b/src/Exception/InvalidTranslatePatternException.php @@ -9,7 +9,7 @@ /** * @author Yann Eugoné */ -class InvalidTranslatePatternException extends InvalidArgumentException +class InvalidTranslatePatternException extends InvalidArgumentException implements ExceptionInterface { /** * @param string $transPattern diff --git a/src/Twig/Extension/EnumExtension.php b/src/Twig/Extension/EnumExtension.php index ba7f157..a439f5a 100644 --- a/src/Twig/Extension/EnumExtension.php +++ b/src/Twig/Extension/EnumExtension.php @@ -8,6 +8,7 @@ use Twig\TwigFilter; use Twig\TwigFunction; use Yokai\EnumBundle\EnumRegistry; +use Yokai\EnumBundle\Exception\InvalidEnumValueException; /** * @author Yann Eugoné @@ -56,7 +57,16 @@ public function getFilters(): array */ public function getLabel(string $value, string $enum): string { - return $this->getChoices($enum)[$value] ?? $value; + $enum = $this->registry->get($enum); + if (\method_exists($enum, 'getLabel')) { + try { + return $enum->getLabel($value); + } catch (InvalidEnumValueException $exception) { + return $value; + } + } + + return $enum->getChoices()[$value] ?? $value; } /** diff --git a/tests/ConfigurableEnumTest.php b/tests/ConfigurableEnumTest.php index fcd6702..feba5c6 100644 --- a/tests/ConfigurableEnumTest.php +++ b/tests/ConfigurableEnumTest.php @@ -3,6 +3,7 @@ namespace Yokai\EnumBundle\Tests; use Yokai\EnumBundle\ConfigurableEnum; +use Yokai\EnumBundle\Exception\InvalidEnumValueException; /** * @author Yann Eugoné @@ -12,7 +13,16 @@ class ConfigurableEnumTest extends TestCase public function testConfigurability(): void { $fooEnum = new ConfigurableEnum('foo', ['foo' => 'FOO', 'bar' => 'BAR']); - $this->assertSame('foo', $fooEnum->getName()); - $this->assertSame(['foo' => 'FOO', 'bar' => 'BAR'], $fooEnum->getChoices()); + self::assertSame('foo', $fooEnum->getName()); + self::assertSame(['foo' => 'FOO', 'bar' => 'BAR'], $fooEnum->getChoices()); + self::assertSame('FOO', $fooEnum->getLabel('foo')); + self::assertSame('BAR', $fooEnum->getLabel('bar')); + } + + public function testLabelNotFound(): void + { + $this->expectException(InvalidEnumValueException::class); + $fooEnum = new ConfigurableEnum('foo', ['foo' => 'FOO', 'bar' => 'BAR']); + $fooEnum->getLabel('unknown enum value'); } } diff --git a/tests/ConfigurableTranslatedEnumTest.php b/tests/ConfigurableTranslatedEnumTest.php index 3c2abdd..7d9adf9 100644 --- a/tests/ConfigurableTranslatedEnumTest.php +++ b/tests/ConfigurableTranslatedEnumTest.php @@ -5,6 +5,7 @@ use Prophecy\Prophecy\ObjectProphecy; use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\EnumBundle\ConfigurableTranslatedEnum; +use Yokai\EnumBundle\Exception\InvalidEnumValueException; use Yokai\EnumBundle\Exception\InvalidTranslatePatternException; /** @@ -25,13 +26,15 @@ public function testTranslatedChoices(): void $translator = $this->prophesize(TranslatorInterface::class); $translator->trans('choice.something.foo', [], 'messages', null)->shouldBeCalled()->willReturn('FOO translated'); $translator->trans('choice.something.bar', [], 'messages', null)->shouldBeCalled()->willReturn('BAR translated'); - $type = new ConfigurableTranslatedEnum($translator->reveal(), 'choice.something.%s', 'something', ['foo', 'bar']); + $enum = new ConfigurableTranslatedEnum($translator->reveal(), 'choice.something.%s', 'something', ['foo', 'bar']); $expectedChoices = [ 'foo' => 'FOO translated', 'bar' => 'BAR translated', ]; - $this->assertEquals($expectedChoices, $type->getChoices()); + self::assertEquals($expectedChoices, $enum->getChoices()); + self::assertSame('FOO translated', $enum->getLabel('foo')); + self::assertSame('BAR translated', $enum->getLabel('bar')); } public function testTranslatedWithDomainChoices(): void @@ -42,13 +45,28 @@ public function testTranslatedWithDomainChoices(): void $translator->trans('choice.something.bar', [], 'messages', null)->shouldNotBeCalled(); $translator->trans('something.foo', [], 'choices', null)->shouldBeCalled()->willReturn('FOO translated'); $translator->trans('something.bar', [], 'choices', null)->shouldBeCalled()->willReturn('BAR translated'); - $type = new ConfigurableTranslatedEnum($translator->reveal(), 'something.%s', 'something', ['foo', 'bar']); - $type->setTransDomain('choices'); + $enum = new ConfigurableTranslatedEnum($translator->reveal(), 'something.%s', 'something', ['foo', 'bar']); + $enum->setTransDomain('choices'); $expectedChoices = [ 'foo' => 'FOO translated', 'bar' => 'BAR translated', ]; - $this->assertEquals($expectedChoices, $type->getChoices()); + self::assertEquals($expectedChoices, $enum->getChoices()); + self::assertSame('FOO translated', $enum->getLabel('foo')); + self::assertSame('BAR translated', $enum->getLabel('bar')); + } + + public function testLabelNotFound(): void + { + $this->expectException(InvalidEnumValueException::class); + + /** @var ObjectProphecy|TranslatorInterface $translator */ + $translator = $this->prophesize(TranslatorInterface::class); + $translator->trans('choice.something.foo', [], 'messages', null)->shouldBeCalled()->willReturn('FOO translated'); + $translator->trans('choice.something.bar', [], 'messages', null)->shouldBeCalled()->willReturn('BAR translated'); + $enum = new ConfigurableTranslatedEnum($translator->reveal(), 'choice.something.%s', 'something', ['foo', 'bar']); + + $enum->getLabel('unknown'); } } diff --git a/tests/ConstantListEnumTest.php b/tests/ConstantListEnumTest.php index b315bdf..7657c6d 100644 --- a/tests/ConstantListEnumTest.php +++ b/tests/ConstantListEnumTest.php @@ -3,6 +3,7 @@ namespace Yokai\EnumBundle\Tests; use Yokai\EnumBundle\ConstantListEnum; +use Yokai\EnumBundle\Exception\InvalidEnumValueException; use Yokai\EnumBundle\Tests\Fixtures\Vehicle; /** @@ -23,6 +24,8 @@ public function testVehicleEnums(): void ['bike' => 'bike', 'car' => 'car', 'bus' => 'bus'], $type->getChoices() ); + self::assertSame('bike', $type->getLabel('bike')); + self::assertSame('bus', $type->getLabel('bus')); $engine = $this->getEnum(Vehicle::class.'::ENGINE_*', 'vehicle.engine'); self::assertSame('vehicle.engine', $engine->getName()); @@ -30,6 +33,8 @@ public function testVehicleEnums(): void ['electic' => 'electic', 'combustion' => 'combustion'], $engine->getChoices() ); + self::assertSame('electic', $engine->getLabel('electic')); + self::assertSame('combustion', $engine->getLabel('combustion')); $brand = $this->getEnum(Vehicle::class.'::BRAND_*', 'vehicle.brand'); self::assertSame('vehicle.brand', $brand->getName()); @@ -37,5 +42,16 @@ public function testVehicleEnums(): void ['renault' => 'renault', 'volkswagen' => 'volkswagen', 'toyota' => 'toyota'], $brand->getChoices() ); + self::assertSame('renault', $brand->getLabel('renault')); + self::assertSame('toyota', $brand->getLabel('toyota')); + } + + public function testLabelNotFound(): void + { + $this->expectException(InvalidEnumValueException::class); + + $enum = $this->getEnum(Vehicle::class.'::TYPE_*', 'vehicle.type'); + + $enum->getLabel('unknown'); } } diff --git a/tests/ConstantListTranslatedEnumTest.php b/tests/ConstantListTranslatedEnumTest.php index 73cde96..2b87d37 100644 --- a/tests/ConstantListTranslatedEnumTest.php +++ b/tests/ConstantListTranslatedEnumTest.php @@ -5,6 +5,7 @@ use Prophecy\Prophecy\ObjectProphecy; use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\EnumBundle\ConstantListTranslatedEnum; +use Yokai\EnumBundle\Exception\InvalidEnumValueException; use Yokai\EnumBundle\Tests\Fixtures\Vehicle; /** @@ -35,32 +36,50 @@ public function getEnum(string $pattern, string $name): ConstantListTranslatedEn public function testVehicleEnums(): void { $type = $this->getEnum(Vehicle::class.'::TYPE_*', 'vehicle.type'); - $this->translator->trans('vehicle.type.bike', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Moto'); - $this->translator->trans('vehicle.type.car', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Voiture'); - $this->translator->trans('vehicle.type.bus', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Bus'); + $this->translator->trans('vehicle.type.bike', [], 'messages')->shouldBeCalled()->willReturn('Moto'); + $this->translator->trans('vehicle.type.car', [], 'messages')->shouldBeCalled()->willReturn('Voiture'); + $this->translator->trans('vehicle.type.bus', [], 'messages')->shouldBeCalled()->willReturn('Bus'); self::assertSame('vehicle.type', $type->getName()); self::assertSame( ['bike' => 'Moto', 'car' => 'Voiture', 'bus' => 'Bus'], $type->getChoices() ); + self::assertSame('Moto', $type->getLabel('bike')); + self::assertSame('Bus', $type->getLabel('bus')); $engine = $this->getEnum(Vehicle::class.'::ENGINE_*', 'vehicle.engine'); - $this->translator->trans('vehicle.engine.electic', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Electrique'); - $this->translator->trans('vehicle.engine.combustion', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Combustion'); + $this->translator->trans('vehicle.engine.electic', [], 'messages')->shouldBeCalled()->willReturn('Electrique'); + $this->translator->trans('vehicle.engine.combustion', [], 'messages')->shouldBeCalled()->willReturn('Combustion'); self::assertSame('vehicle.engine', $engine->getName()); self::assertSame( ['electic' => 'Electrique', 'combustion' => 'Combustion'], $engine->getChoices() ); + self::assertSame('Electrique', $engine->getLabel('electic')); + self::assertSame('Combustion', $engine->getLabel('combustion')); $brand = $this->getEnum(Vehicle::class.'::BRAND_*', 'vehicle.brand'); - $this->translator->trans('vehicle.brand.renault', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Renault'); - $this->translator->trans('vehicle.brand.volkswagen', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Volkswagen'); - $this->translator->trans('vehicle.brand.toyota', [], 'messages')->shouldBeCalledTimes(1)->willReturn('Toyota'); + $this->translator->trans('vehicle.brand.renault', [], 'messages')->shouldBeCalled()->willReturn('Renault'); + $this->translator->trans('vehicle.brand.volkswagen', [], 'messages')->shouldBeCalled()->willReturn('Volkswagen'); + $this->translator->trans('vehicle.brand.toyota', [], 'messages')->shouldBeCalled()->willReturn('Toyota'); self::assertSame('vehicle.brand', $brand->getName()); self::assertSame( ['renault' => 'Renault', 'volkswagen' => 'Volkswagen', 'toyota' => 'Toyota'], $brand->getChoices() ); + self::assertSame('Renault', $brand->getLabel('renault')); + self::assertSame('Toyota', $brand->getLabel('toyota')); + } + + public function testLabelNotFound(): void + { + $this->expectException(InvalidEnumValueException::class); + + $enum = $this->getEnum(Vehicle::class.'::TYPE_*', 'vehicle.type'); + $this->translator->trans('vehicle.type.bike', [], 'messages')->shouldBeCalled()->willReturn('Moto'); + $this->translator->trans('vehicle.type.car', [], 'messages')->shouldBeCalled()->willReturn('Voiture'); + $this->translator->trans('vehicle.type.bus', [], 'messages')->shouldBeCalled()->willReturn('Bus'); + + $enum->getLabel('unknown'); } } diff --git a/tests/DependencyInjection/EnumExtensionTest.php b/tests/DependencyInjection/EnumExtensionTest.php index ff00c92..fba70d9 100644 --- a/tests/DependencyInjection/EnumExtensionTest.php +++ b/tests/DependencyInjection/EnumExtensionTest.php @@ -37,9 +37,9 @@ public function it_register_services(): void } $autoconfigure = $container->getAutoconfiguredInstanceof(); - $this->assertArrayHasKey(EnumInterface::class, $autoconfigure); - $this->assertEquals(['yokai_enum.enum' => [[]]], $autoconfigure[EnumInterface::class]->getTags()); + self::assertArrayHasKey(EnumInterface::class, $autoconfigure); + self::assertEquals(['yokai_enum.enum' => [[]]], $autoconfigure[EnumInterface::class]->getTags()); - $this->assertTrue($container->hasAlias(EnumRegistry::class)); + self::assertTrue($container->hasAlias(EnumRegistry::class)); } } diff --git a/tests/EnumRegistryTest.php b/tests/EnumRegistryTest.php index 98b11f5..4d2b77d 100644 --- a/tests/EnumRegistryTest.php +++ b/tests/EnumRegistryTest.php @@ -55,16 +55,16 @@ public function testAddNominal(): void $this->registry->add($subscription); $this->registry->add($type); - $this->assertTrue($this->registry->has(GenderEnum::class)); - $this->assertTrue($this->registry->has('state')); - $this->assertTrue($this->registry->has('subscription')); - $this->assertTrue($this->registry->has('type')); + self::assertTrue($this->registry->has(GenderEnum::class)); + self::assertTrue($this->registry->has('state')); + self::assertTrue($this->registry->has('subscription')); + self::assertTrue($this->registry->has('type')); - $this->assertSame($gender, $this->registry->get(GenderEnum::class)); - $this->assertSame($state, $this->registry->get('state')); - $this->assertSame($subscription, $this->registry->get('subscription')); - $this->assertSame($type, $this->registry->get('type')); - $this->assertSame( + self::assertSame($gender, $this->registry->get(GenderEnum::class)); + self::assertSame($state, $this->registry->get('state')); + self::assertSame($subscription, $this->registry->get('subscription')); + self::assertSame($type, $this->registry->get('type')); + self::assertSame( [GenderEnum::class => $gender, 'state' => $state, 'subscription' => $subscription, 'type' => $type], $this->registry->all() ); diff --git a/tests/Fixtures/GenderEnum.php b/tests/Fixtures/GenderEnum.php index ab787f1..c19141c 100644 --- a/tests/Fixtures/GenderEnum.php +++ b/tests/Fixtures/GenderEnum.php @@ -4,6 +4,7 @@ use Yokai\EnumBundle\EnumInterface; use Yokai\EnumBundle\EnumWithClassAsNameTrait; +use Yokai\EnumBundle\Exception\InvalidEnumValueException; /** * @author Yann Eugoné @@ -16,4 +17,17 @@ public function getChoices(): array { return ['male' => 'Male', 'female' => 'Female']; } + + /** + * @inheritdoc + */ + public function getLabel(string $value): string + { + $choices = $this->getChoices(); + if (!isset($choices[$value])) { + throw InvalidEnumValueException::create($this, $value); + } + + return $choices[$value]; + } } diff --git a/tests/Form/Extension/EnumTypeGuesserTest.php b/tests/Form/Extension/EnumTypeGuesserTest.php index d028f5c..4ae8e30 100644 --- a/tests/Form/Extension/EnumTypeGuesserTest.php +++ b/tests/Form/Extension/EnumTypeGuesserTest.php @@ -87,7 +87,7 @@ public function testGuessTypeDirect(): void Guess::HIGH_CONFIDENCE ); - $this->assertEquals($guess, $this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY_DIRECT)); + self::assertEquals($guess, $this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY_DIRECT)); } public function testGuessTypeCompound(): void @@ -105,22 +105,22 @@ public function testGuessTypeCompound(): void Guess::HIGH_CONFIDENCE ); - $this->assertEquals($guess, $this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY_COMPOUND)); + self::assertEquals($guess, $this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY_COMPOUND)); } public function testGuessRequired(): void { - $this->assertNull($this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY_DIRECT)); + self::assertNull($this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY_DIRECT)); } public function testGuessMaxLength(): void { - $this->assertNull($this->guesser->guessMaxLength(self::TEST_CLASS, self::TEST_PROPERTY_DIRECT)); + self::assertNull($this->guesser->guessMaxLength(self::TEST_CLASS, self::TEST_PROPERTY_DIRECT)); } public function testGuessPattern(): void { - $this->assertNull($this->guesser->guessPattern(self::TEST_CLASS, self::TEST_PROPERTY_DIRECT)); + self::assertNull($this->guesser->guessPattern(self::TEST_CLASS, self::TEST_PROPERTY_DIRECT)); } public function testCreateForm(): void @@ -129,7 +129,7 @@ public function testCreateForm(): void $form = $this->factory->create(FormType::class, new $class, ['data_class' => $class]) ->add(self::TEST_PROPERTY_DIRECT); - $this->assertEquals( + self::assertEquals( ['Male' => 'male', 'Female' => 'female'], $form->get(self::TEST_PROPERTY_DIRECT)->getConfig()->getOption('choices') ); diff --git a/tests/Form/Type/EnumTypeTest.php b/tests/Form/Type/EnumTypeTest.php index acade77..58c59af 100644 --- a/tests/Form/Type/EnumTypeTest.php +++ b/tests/Form/Type/EnumTypeTest.php @@ -47,7 +47,7 @@ public function testEnumOptionValid(): void { $form = $this->createForm(GenderEnum::class); - $this->assertEquals(['Male' => 'male', 'Female' => 'female'], $form->getConfig()->getOption('choices')); + self::assertEquals(['Male' => 'male', 'Female' => 'female'], $form->getConfig()->getOption('choices')); } protected function getExtensions(): array diff --git a/tests/Twig/Extension/EnumExtensionTest.php b/tests/Twig/Extension/EnumExtensionTest.php index a46809e..1cb25a1 100644 --- a/tests/Twig/Extension/EnumExtensionTest.php +++ b/tests/Twig/Extension/EnumExtensionTest.php @@ -7,6 +7,7 @@ use Twig\Loader\ArrayLoader; use Yokai\EnumBundle\EnumInterface; use Yokai\EnumBundle\EnumRegistry; +use Yokai\EnumBundle\Exception\InvalidEnumValueException; use Yokai\EnumBundle\Tests\TestCase; use Yokai\EnumBundle\Twig\Extension\EnumExtension; @@ -28,28 +29,29 @@ protected function setUp(): void public function testEnumLabel(): void { $enum = $this->prophesize(EnumInterface::class); - $enum->getChoices() - ->willReturn(['foo' => 'FOO', 'bar' => 'BAR']); + $enum->getLabel('foo')->willReturn('FOO'); + $enum->getLabel('bar')->willReturn('BAR'); + $enum->getLabel('not_exist')->willThrow(new InvalidEnumValueException()); $this->registry->get('test') ->willReturn($enum->reveal()); $twig = $this->createEnvironment(); - $this->assertSame( + self::assertSame( 'FOO', $twig->createTemplate("{{ 'foo'|enum_label('test') }}")->render([]) ); - $this->assertSame( + self::assertSame( 'BAR', $twig->createTemplate("{{ enum_label('bar', 'test') }}")->render([]) ); - $this->assertSame( + self::assertSame( 'not_exist', $twig->createTemplate("{{ 'not_exist'|enum_label('test') }}")->render([]) ); - $this->assertSame( + self::assertSame( 'not_exist', $twig->createTemplate("{{ enum_label('not_exist', 'test') }}")->render([]) ); @@ -66,7 +68,7 @@ public function testEnumChoices(): void $twig = $this->createEnvironment(); - $this->assertSame( + self::assertSame( 'foo,FOO|bar,BAR|', $twig->createTemplate("{% for value,label in enum_choices('test') %}{{ value }},{{ label }}|{% endfor %}")->render([]) );