From f773d56fc1f2335726c9de7ea31e779ee40c157f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Tue, 27 Aug 2024 11:32:41 +0200 Subject: [PATCH] Remove PHP/Symfony versions matrix hacks --- src/Enum.php | 15 +- src/EnumInterface.php | 2 +- src/EnumRegistry.php | 2 +- src/Form/Type/EnumType.php | 5 +- src/TranslatedEnum.php | 19 +- src/Twig/Extension/EnumExtension.php | 5 +- src/Validator/Constraints/Enum.php | 5 +- src/Validator/Constraints/EnumValidator.php | 7 +- .../config/packages/annotations.yaml | 6 - tests/Integration/config/services-8.1.yaml | 8 - .../{services-8.0.yaml => services.yaml} | 1 + tests/Integration/src/Kernel.php | 15 +- ...equestPhp80.php => MyCLabsPullRequest.php} | 9 +- ...estPhp81.php => NativeEnumPullRequest.php} | 9 +- .../Integration/src/Model/PullRequestPhp7.php | 29 --- .../Integration/tests/PullRequestFormTest.php | 202 +++++++++--------- tests/Unit/Fixtures/StateEnum.php | 2 +- .../Form/Extension/EnumTypeGuesserTest.php | 19 +- tests/Unit/Form/TestExtension.php | 12 +- tests/Unit/Form/Type/EnumTypeTest.php | 12 +- tests/Unit/NativeEnumTest.php | 20 -- tests/Unit/NativeTranslatedEnumTest.php | 20 -- tests/Unit/Translator.php | 85 ++------ .../Unit/Twig/Extension/EnumExtensionTest.php | 8 +- 24 files changed, 172 insertions(+), 345 deletions(-) delete mode 100644 tests/Integration/config/packages/annotations.yaml delete mode 100644 tests/Integration/config/services-8.1.yaml rename tests/Integration/config/{services-8.0.yaml => services.yaml} (78%) rename tests/Integration/src/Model/{PullRequestPhp80.php => MyCLabsPullRequest.php} (81%) rename tests/Integration/src/Model/{PullRequestPhp81.php => NativeEnumPullRequest.php} (82%) delete mode 100644 tests/Integration/src/Model/PullRequestPhp7.php diff --git a/src/Enum.php b/src/Enum.php index a262681..12e2e0a 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -12,15 +12,12 @@ */ class Enum implements EnumInterface { - /** - * @var string - */ - private $name; + private string $name; /** * @var array|null */ - private $choices; + private array|null $choices; /** * @param array|null $choices Allowed to be null if you are extending this class @@ -41,8 +38,10 @@ public function __construct(?array $choices, ?string $name = null) if ($name === null) { $name = static::class; if ( - \strpos($name, 'Yokai\\EnumBundle\\') === 0 // using FQCN as name is only allowed for other namespaces - && \strpos($name, 'Yokai\\EnumBundle\\Tests\\') !== 0 // except for our tests + // using FQCN as name is only allowed for other namespaces + \str_starts_with($name, 'Yokai\\EnumBundle\\') + // except for our tests + && !\str_starts_with($name, 'Yokai\\EnumBundle\\Tests\\') ) { throw new LogicException( 'When using ' . static::class . ', $name argument in ' . __METHOD__ . ' method cannot be null' @@ -67,7 +66,7 @@ public function getValues(): array return \array_values($this->choices); } - public function getLabel($value): string + public function getLabel(mixed $value): string { $this->init(); diff --git a/src/EnumInterface.php b/src/EnumInterface.php index 3bc727e..56687cd 100644 --- a/src/EnumInterface.php +++ b/src/EnumInterface.php @@ -28,7 +28,7 @@ public function getValues(): array; * * @param mixed $value */ - public function getLabel($value): string; + public function getLabel(mixed $value): string; /** * Returns enum identifier (must be unique across app). diff --git a/src/EnumRegistry.php b/src/EnumRegistry.php index 64116c7..f97e9df 100644 --- a/src/EnumRegistry.php +++ b/src/EnumRegistry.php @@ -15,7 +15,7 @@ final class EnumRegistry /** * @var EnumInterface[] */ - private $enums = []; + private array $enums = []; /** * @throws LogicException diff --git a/src/Form/Type/EnumType.php b/src/Form/Type/EnumType.php index 53ef7c9..5078ac4 100644 --- a/src/Form/Type/EnumType.php +++ b/src/Form/Type/EnumType.php @@ -16,10 +16,7 @@ */ final class EnumType extends AbstractType { - /** - * @var EnumRegistry - */ - private $enumRegistry; + private EnumRegistry $enumRegistry; public function __construct(EnumRegistry $enumRegistry) { diff --git a/src/TranslatedEnum.php b/src/TranslatedEnum.php index c1069ce..cadb770 100644 --- a/src/TranslatedEnum.php +++ b/src/TranslatedEnum.php @@ -15,22 +15,13 @@ class TranslatedEnum extends Enum /** * @var array */ - private $values; + private array $values; - /** - * @var TranslatorInterface - */ - private $translator; + private TranslatorInterface $translator; - /** - * @var string - */ - private $transPattern; + private string $transPattern; - /** - * @var string - */ - private $transDomain; + private string $transDomain; /** * @param array $values @@ -44,7 +35,7 @@ public function __construct( string $transDomain = 'messages', ?string $name = null ) { - if (\strpos($transPattern, '%s') === false) { + if (!\str_contains($transPattern, '%s')) { throw LogicException::placeholderRequired($transPattern); } diff --git a/src/Twig/Extension/EnumExtension.php b/src/Twig/Extension/EnumExtension.php index 0cdf2d6..37adf67 100644 --- a/src/Twig/Extension/EnumExtension.php +++ b/src/Twig/Extension/EnumExtension.php @@ -14,10 +14,7 @@ */ final class EnumExtension extends AbstractExtension { - /** - * @var EnumRegistry - */ - private $registry; + private EnumRegistry $registry; public function __construct(EnumRegistry $registry) { diff --git a/src/Validator/Constraints/Enum.php b/src/Validator/Constraints/Enum.php index 2db9fc9..2bcb530 100644 --- a/src/Validator/Constraints/Enum.php +++ b/src/Validator/Constraints/Enum.php @@ -15,10 +15,7 @@ #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)] final class Enum extends Choice { - /** - * @var string - */ - public $enum; + public string $enum; /** * @param array $options diff --git a/src/Validator/Constraints/EnumValidator.php b/src/Validator/Constraints/EnumValidator.php index 7e01408..a566625 100644 --- a/src/Validator/Constraints/EnumValidator.php +++ b/src/Validator/Constraints/EnumValidator.php @@ -15,10 +15,7 @@ */ final class EnumValidator extends ChoiceValidator { - /** - * @var EnumRegistry - */ - private $enumRegistry; + private EnumRegistry $enumRegistry; public function __construct(EnumRegistry $enumRegistry) { @@ -34,7 +31,7 @@ public function validate(mixed $value, Constraint $constraint): void $constraint->choices = null; $constraint->callback = null; - if (!$constraint->enum) { + if (!isset($constraint->enum)) { throw new ConstraintDefinitionException('"enum" must be specified on constraint Enum'); } diff --git a/tests/Integration/config/packages/annotations.yaml b/tests/Integration/config/packages/annotations.yaml deleted file mode 100644 index 1aa1e4f..0000000 --- a/tests/Integration/config/packages/annotations.yaml +++ /dev/null @@ -1,6 +0,0 @@ -framework: - annotations: true - -sensio_framework_extra: - router: - annotations: true diff --git a/tests/Integration/config/services-8.1.yaml b/tests/Integration/config/services-8.1.yaml deleted file mode 100644 index 6086310..0000000 --- a/tests/Integration/config/services-8.1.yaml +++ /dev/null @@ -1,8 +0,0 @@ -services: - _defaults: - autowire: true - autoconfigure: true - - Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestLabelEnum: ~ - Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestNativeStatusEnum: ~ - Yokai\EnumBundle\Tests\Integration\App\Form\PullRequestType: ~ diff --git a/tests/Integration/config/services-8.0.yaml b/tests/Integration/config/services.yaml similarity index 78% rename from tests/Integration/config/services-8.0.yaml rename to tests/Integration/config/services.yaml index 0e5b26f..2e7e0a6 100644 --- a/tests/Integration/config/services-8.0.yaml +++ b/tests/Integration/config/services.yaml @@ -5,4 +5,5 @@ services: Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestLabelEnum: ~ Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestMyCLabsStatusEnum: ~ + Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestNativeStatusEnum: ~ Yokai\EnumBundle\Tests\Integration\App\Form\PullRequestType: ~ diff --git a/tests/Integration/src/Kernel.php b/tests/Integration/src/Kernel.php index c3b7670..9a01d3b 100644 --- a/tests/Integration/src/Kernel.php +++ b/tests/Integration/src/Kernel.php @@ -18,9 +18,6 @@ public function registerBundles(): iterable { yield new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(); yield new \Yokai\EnumBundle\YokaiEnumBundle(); - if (\PHP_VERSION_ID < 80000) { - yield new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(); - } } public function getProjectDir(): string @@ -32,22 +29,14 @@ public function registerContainerConfiguration(LoaderInterface $loader): void { $loader->load(__DIR__ . '/../config/packages/framework.yaml'); $loader->load(__DIR__ . '/../config/packages/translation.yaml'); - if (\PHP_VERSION_ID < 80000) { - $loader->load(__DIR__ . '/../config/packages/annotations.yaml'); - } - - if (\PHP_VERSION_ID < 80100) { - $loader->load(__DIR__ . '/../config/services-8.0.yaml'); - } else { - $loader->load(__DIR__ . '/../config/services-8.1.yaml'); - } + $loader->load(__DIR__ . '/../config/services.yaml'); } protected function build(ContainerBuilder $container): void { $container->addCompilerPass( new class() implements CompilerPassInterface { - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { $container->findDefinition('form.factory')->setPublic(true); } diff --git a/tests/Integration/src/Model/PullRequestPhp80.php b/tests/Integration/src/Model/MyCLabsPullRequest.php similarity index 81% rename from tests/Integration/src/Model/PullRequestPhp80.php rename to tests/Integration/src/Model/MyCLabsPullRequest.php index 1f86dfb..8be8fc0 100644 --- a/tests/Integration/src/Model/PullRequestPhp80.php +++ b/tests/Integration/src/Model/MyCLabsPullRequest.php @@ -11,17 +11,14 @@ /** * @author Yann Eugoné */ -final class PullRequestPhp80 +final class MyCLabsPullRequest { - /** - * @var MyCLabsStatus - */ #[Enum(enum: PullRequestMyCLabsStatusEnum::class)] - public $status; + public MyCLabsStatus $status; /** * @var string[] */ #[Enum(enum: PullRequestLabelEnum::class, multiple: true)] - public $labels; + public array $labels; } diff --git a/tests/Integration/src/Model/PullRequestPhp81.php b/tests/Integration/src/Model/NativeEnumPullRequest.php similarity index 82% rename from tests/Integration/src/Model/PullRequestPhp81.php rename to tests/Integration/src/Model/NativeEnumPullRequest.php index b8aed16..607d2c2 100644 --- a/tests/Integration/src/Model/PullRequestPhp81.php +++ b/tests/Integration/src/Model/NativeEnumPullRequest.php @@ -11,17 +11,14 @@ /** * @author Yann Eugoné */ -final class PullRequestPhp81 +final class NativeEnumPullRequest { - /** - * @var NativeStatus - */ #[Enum(enum: PullRequestNativeStatusEnum::class)] - public $status; + public NativeStatus $status; /** * @var string[] */ #[Enum(enum: PullRequestLabelEnum::class, multiple: true)] - public $labels; + public array $labels; } diff --git a/tests/Integration/src/Model/PullRequestPhp7.php b/tests/Integration/src/Model/PullRequestPhp7.php deleted file mode 100644 index 1f6ae37..0000000 --- a/tests/Integration/src/Model/PullRequestPhp7.php +++ /dev/null @@ -1,29 +0,0 @@ - - */ -final class PullRequestPhp7 -{ - /** - * @var MyCLabsStatus - * - * @Enum(PullRequestMyCLabsStatusEnum::class) - */ - public $status; - - /** - * @var string[] - * - * @Enum(PullRequestLabelEnum::class, multiple=true) - */ - public $labels; -} diff --git a/tests/Integration/tests/PullRequestFormTest.php b/tests/Integration/tests/PullRequestFormTest.php index c4a27f2..72abe94 100644 --- a/tests/Integration/tests/PullRequestFormTest.php +++ b/tests/Integration/tests/PullRequestFormTest.php @@ -14,49 +14,53 @@ use Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestMyCLabsStatusEnum; use Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestNativeStatusEnum; use Yokai\EnumBundle\Tests\Integration\App\Form\PullRequestType; -use Yokai\EnumBundle\Tests\Integration\App\Kernel; +use Yokai\EnumBundle\Tests\Integration\App\Model\MyCLabsPullRequest; use Yokai\EnumBundle\Tests\Integration\App\Model\MyCLabsStatus; +use Yokai\EnumBundle\Tests\Integration\App\Model\NativeEnumPullRequest; use Yokai\EnumBundle\Tests\Integration\App\Model\NativeStatus; -use Yokai\EnumBundle\Tests\Integration\App\Model\PullRequestPhp7; -use Yokai\EnumBundle\Tests\Integration\App\Model\PullRequestPhp80; -use Yokai\EnumBundle\Tests\Integration\App\Model\PullRequestPhp81; /** * @author Yann Eugoné */ final class PullRequestFormTest extends KernelTestCase { - public function testBuildForm(): void + /** + * @dataProvider classes + */ + public function testBuildForm(string $class): void { $container = self::bootKernel()->getContainer(); - $model = self::pullRequest('merged', ['3.x', 'feature']); + $model = self::pullRequest($class, 'merged', ['3.x', 'feature']); $form = self::form($container, $model); $status = $form->get('status'); - if (\PHP_VERSION_ID < 80100) { - self::assertEquals(MyCLabsStatus::MERGED(), $status->getData()); - self::assertEquals(MyCLabsStatus::MERGED(), $status->getNormData()); - self::assertSame(EnumType::class, \get_class($status->getConfig()->getType()->getInnerType())); - self::assertSame(PullRequestMyCLabsStatusEnum::class, $status->getConfig()->getOption('enum')); - self::assertEquals( - [ - 'Opened' => MyCLabsStatus::OPENED(), - 'Merged' => MyCLabsStatus::MERGED(), - 'Closed' => MyCLabsStatus::CLOSED(), - ], - $status->getConfig()->getOption('choices') - ); - } else { - self::assertEquals(NativeStatus::MERGED, $status->getData()); - self::assertEquals(NativeStatus::MERGED, $status->getNormData()); - self::assertSame(EnumType::class, \get_class($status->getConfig()->getType()->getInnerType())); - self::assertSame(PullRequestNativeStatusEnum::class, $status->getConfig()->getOption('enum')); - self::assertEquals( - ['Opened' => NativeStatus::OPENED, 'Merged' => NativeStatus::MERGED, 'Closed' => NativeStatus::CLOSED], - $status->getConfig()->getOption('choices') - ); - } + (match ($class) { + MyCLabsPullRequest::class => function () use ($status) { + self::assertEquals(MyCLabsStatus::MERGED(), $status->getData()); + self::assertEquals(MyCLabsStatus::MERGED(), $status->getNormData()); + self::assertSame(EnumType::class, \get_class($status->getConfig()->getType()->getInnerType())); + self::assertSame(PullRequestMyCLabsStatusEnum::class, $status->getConfig()->getOption('enum')); + self::assertEquals( + [ + 'Opened' => MyCLabsStatus::OPENED(), + 'Merged' => MyCLabsStatus::MERGED(), + 'Closed' => MyCLabsStatus::CLOSED(), + ], + $status->getConfig()->getOption('choices') + ); + }, + NativeEnumPullRequest::class => function () use ($status) { + self::assertEquals(NativeStatus::MERGED, $status->getData()); + self::assertEquals(NativeStatus::MERGED, $status->getNormData()); + self::assertSame(EnumType::class, \get_class($status->getConfig()->getType()->getInnerType())); + self::assertSame(PullRequestNativeStatusEnum::class, $status->getConfig()->getOption('enum')); + self::assertEquals( + ['Opened' => NativeStatus::OPENED, 'Merged' => NativeStatus::MERGED, 'Closed' => NativeStatus::CLOSED], + $status->getConfig()->getOption('choices') + ); + }, + })(); $labels = $form->get('labels'); self::assertEquals(['3.x', 'feature'], $labels->getData()); @@ -76,11 +80,20 @@ public function testBuildForm(): void ); } + public function classes(): Generator + { + yield [MyCLabsPullRequest::class]; + yield [NativeEnumPullRequest::class]; + } + /** * @dataProvider valid */ - public function testSubmitValidData(array $formData, $model, $expected): void - { + public function testSubmitValidData( + array $formData, + MyCLabsPullRequest|NativeEnumPullRequest $model, + MyCLabsPullRequest|NativeEnumPullRequest $expected, + ): void { $container = self::bootKernel()->getContainer(); $form = self::form($container, $model); @@ -93,24 +106,29 @@ public function testSubmitValidData(array $formData, $model, $expected): void public function valid(): Generator { - yield [ - ['status' => 0, 'labels' => ['bugfix', '1.x']], - self::pullRequest(), - self::pullRequest('opened', ['bugfix', '1.x']), - ]; - - yield [ - ['status' => 2, 'labels' => ['bugfix', '2.x']], - self::pullRequest('opened', ['bugfix', '1.x']), - self::pullRequest('closed', ['bugfix', '2.x']), - ]; + foreach ($this->classes() as [$class]) { + yield [ + ['status' => 0, 'labels' => ['bugfix', '1.x']], + self::pullRequest($class), + self::pullRequest($class, 'opened', ['bugfix', '1.x']), + ]; + + yield [ + ['status' => 2, 'labels' => ['bugfix', '2.x']], + self::pullRequest($class, 'opened', ['bugfix', '1.x']), + self::pullRequest($class, 'closed', ['bugfix', '2.x']), + ]; + } } /** * @dataProvider invalid */ - public function testSubmitInvalidData(array $formData, $model, array $errors): void - { + public function testSubmitInvalidData( + array $formData, + MyCLabsPullRequest|NativeEnumPullRequest $model, + array $errors, + ): void { $container = self::bootKernel()->getContainer(); $form = self::form($container, $model); @@ -130,63 +148,55 @@ public function testSubmitInvalidData(array $formData, $model, array $errors): v public function invalid(): Generator { - $message = 'This value is not valid.'; - if (Kernel::MAJOR_VERSION >= 6) { - $message = 'The selected choice is invalid.'; - } - - yield [ - ['status' => 3, 'labels' => ['bugfix', '5.x']], - self::pullRequest(), - ['status' => $message, 'labels' => 'The choices "5.x" do not exist in the choice list.'], - ]; - - yield [ - ['status' => 3, 'labels' => ['bugfix', '5.x']], - self::pullRequest('opened', ['bugfix', '1.x']), - ['status' => $message, 'labels' => 'The choices "5.x" do not exist in the choice list.'], - ]; - } - - /** - * @return PullRequestPhp7|PullRequestPhp80|PullRequestPhp81 - */ - private static function pullRequest(string $status = null, array $labels = []) - { - if (Kernel::VERSION_ID < 50200) { - $pullRequest = new PullRequestPhp7(); - $status && $pullRequest->status = new MyCLabsStatus($status); - $pullRequest->labels = $labels; - - return $pullRequest; - } - if (\PHP_VERSION_ID < 80000) { - $pullRequest = new PullRequestPhp7(); - $status && $pullRequest->status = new MyCLabsStatus($status); - $pullRequest->labels = $labels; - - return $pullRequest; - } - if (\PHP_VERSION_ID < 80100) { - $pullRequest = new PullRequestPhp80(); - $status && $pullRequest->status = new MyCLabsStatus($status); - $pullRequest->labels = $labels; + foreach ($this->classes() as [$class]) { + yield [ + ['status' => 3, 'labels' => ['bugfix', '5.x']], + self::pullRequest($class), + [ + 'status' => 'The selected choice is invalid.', + 'labels' => 'The choices "5.x" do not exist in the choice list.', + ], + ]; - return $pullRequest; + yield [ + ['status' => 3, 'labels' => ['bugfix', '5.x']], + self::pullRequest($class, 'opened', ['bugfix', '1.x']), + [ + 'status' => 'The selected choice is invalid.', + 'labels' => 'The choices "5.x" do not exist in the choice list.', + ], + ]; } - - $pullRequest = new PullRequestPhp81(); - $status && $pullRequest->status = NativeStatus::from($status); - $pullRequest->labels = $labels; - - return $pullRequest; } - private static function form(ContainerInterface $container, $model): FormInterface - { - $class = \get_class(self::pullRequest()); + private static function pullRequest( + string $class, + string $status = null, + array $labels = [], + ): MyCLabsPullRequest|NativeEnumPullRequest { + return (match ($class) { + MyCLabsPullRequest::class => function () use ($status, $labels) { + $pullRequest = new MyCLabsPullRequest(); + $status && $pullRequest->status = new MyCLabsStatus($status); + $pullRequest->labels = $labels; + + return $pullRequest; + }, + NativeEnumPullRequest::class => function () use ($status, $labels) { + $pullRequest = new NativeEnumPullRequest(); + $status && $pullRequest->status = NativeStatus::from($status); + $pullRequest->labels = $labels; + + return $pullRequest; + }, + })(); + } + private static function form( + ContainerInterface $container, + MyCLabsPullRequest|NativeEnumPullRequest $model, + ): FormInterface { return $container->get('form.factory') - ->create(PullRequestType::class, $model, ['data_class' => $class]); + ->create(PullRequestType::class, $model, ['data_class' => $model::class]); } } diff --git a/tests/Unit/Fixtures/StateEnum.php b/tests/Unit/Fixtures/StateEnum.php index 8ff51f6..bfd12fc 100644 --- a/tests/Unit/Fixtures/StateEnum.php +++ b/tests/Unit/Fixtures/StateEnum.php @@ -31,7 +31,7 @@ public function getChoices(): array return ['New' => 'new', 'Validated' => 'validated', 'Disabled' => 'disabled']; } - public function getLabel($value): string + public function getLabel(mixed $value): string { $choices = $this->getChoices(); $label = \array_search($value, $choices, true); diff --git a/tests/Unit/Form/Extension/EnumTypeGuesserTest.php b/tests/Unit/Form/Extension/EnumTypeGuesserTest.php index a06da63..8fbecd1 100644 --- a/tests/Unit/Form/Extension/EnumTypeGuesserTest.php +++ b/tests/Unit/Form/Extension/EnumTypeGuesserTest.php @@ -31,20 +31,11 @@ class EnumTypeGuesserTest extends TypeTestCase private const TEST_PROPERTY_COMPOUND = 'compound'; private const TEST_PROPERTY_NONE = 'none'; - /** - * @var EnumTypeGuesser - */ - private $guesser; - - /** - * @var EnumRegistry - */ - private $enumRegistry; - - /** - * @var MockObject|MetadataFactoryInterface - */ - private $metadataFactory; + private EnumTypeGuesser $guesser; + + private EnumRegistry $enumRegistry; + + private MockObject|MetadataFactoryInterface $metadataFactory; protected function setUp(): void { diff --git a/tests/Unit/Form/TestExtension.php b/tests/Unit/Form/TestExtension.php index 7f71df3..d142c3b 100644 --- a/tests/Unit/Form/TestExtension.php +++ b/tests/Unit/Form/TestExtension.php @@ -15,15 +15,9 @@ */ class TestExtension extends AbstractExtension { - /** - * @var EnumRegistry - */ - private $enumRegistry; - - /** - * @var MetadataFactoryInterface|null - */ - private $metadataFactory; + private EnumRegistry $enumRegistry; + + private MetadataFactoryInterface|null $metadataFactory; public function __construct(EnumRegistry $enumRegistry, MetadataFactoryInterface $metadataFactory = null) { diff --git a/tests/Unit/Form/Type/EnumTypeTest.php b/tests/Unit/Form/Type/EnumTypeTest.php index 5009386..306c9ef 100644 --- a/tests/Unit/Form/Type/EnumTypeTest.php +++ b/tests/Unit/Form/Type/EnumTypeTest.php @@ -85,10 +85,6 @@ public static function submit(): \Generator Action::EDIT(), ]; - if (\PHP_VERSION_ID < 80100) { - return; - } - yield [ Picture::class, [], @@ -137,11 +133,9 @@ protected function getExtensions(): array 'action.VIEW' => 'Voir', 'action.EDIT' => 'Modifier', ]))); - if (\PHP_VERSION_ID >= 80100) { - $enumRegistry->add(new NativeEnum(Picture::class)); - $enumRegistry->add(new NativeEnum(HTTPMethod::class)); - $enumRegistry->add(new NativeEnum(HTTPStatus::class)); - } + $enumRegistry->add(new NativeEnum(Picture::class)); + $enumRegistry->add(new NativeEnum(HTTPMethod::class)); + $enumRegistry->add(new NativeEnum(HTTPStatus::class)); return [ new TestExtension($enumRegistry), diff --git a/tests/Unit/NativeEnumTest.php b/tests/Unit/NativeEnumTest.php index c72efe9..59f9e86 100644 --- a/tests/Unit/NativeEnumTest.php +++ b/tests/Unit/NativeEnumTest.php @@ -20,10 +20,6 @@ class NativeEnumTest extends TestCase { public function testEnum(): void { - if (\PHP_VERSION_ID < 80100) { - self::markTestSkipped('Only for PHP >= 8.1'); - } - $enum = new NativeEnum(Picture::class); self::assertSame(Picture::class, $enum->getName()); @@ -35,10 +31,6 @@ public function testEnum(): void public function testStringBackedEnum(): void { - if (\PHP_VERSION_ID < 80100) { - self::markTestSkipped('Only for PHP >= 8.1'); - } - $enum = new NativeEnum(HTTPMethod::class); self::assertSame(HTTPMethod::class, $enum->getName()); @@ -50,10 +42,6 @@ public function testStringBackedEnum(): void public function testIntBackedEnum(): void { - if (\PHP_VERSION_ID < 80100) { - self::markTestSkipped('Only for PHP >= 8.1'); - } - $enum = new NativeEnum(HTTPStatus::class); self::assertSame(HTTPStatus::class, $enum->getName()); @@ -65,20 +53,12 @@ public function testIntBackedEnum(): void public function testEnumClassMustBeValid(): void { - if (\PHP_VERSION_ID < 80100) { - self::markTestSkipped('Only for PHP >= 8.1'); - } - $this->expectException(LogicException::class); new NativeEnum(Vehicle::class); } public function testLabelNotFound(): void { - if (\PHP_VERSION_ID < 80100) { - self::markTestSkipped('Only for PHP >= 8.1'); - } - $this->expectException(InvalidArgumentException::class); $enum = new NativeEnum(Picture::class); $enum->getLabel('unknown enum value'); diff --git a/tests/Unit/NativeTranslatedEnumTest.php b/tests/Unit/NativeTranslatedEnumTest.php index 1017357..1539e98 100644 --- a/tests/Unit/NativeTranslatedEnumTest.php +++ b/tests/Unit/NativeTranslatedEnumTest.php @@ -20,10 +20,6 @@ class NativeTranslatedEnumTest extends TestCase { public function testEnum(): void { - if (\PHP_VERSION_ID < 80100) { - self::markTestSkipped('Only for PHP >= 8.1'); - } - $translator = new Translator(['picture.Landscape' => 'Paysage', 'picture.Portrait' => 'Portrait']); $enum = new NativeTranslatedEnum(Picture::class, $translator, 'picture.%s'); @@ -36,10 +32,6 @@ public function testEnum(): void public function testStringBackedEnum(): void { - if (\PHP_VERSION_ID < 80100) { - self::markTestSkipped('Only for PHP >= 8.1'); - } - $translator = new Translator(['method.GET' => 'get', 'method.POST' => 'post']); $enum = new NativeTranslatedEnum(HTTPMethod::class, $translator, 'method.%s'); @@ -52,10 +44,6 @@ public function testStringBackedEnum(): void public function testIntBackedEnum(): void { - if (\PHP_VERSION_ID < 80100) { - self::markTestSkipped('Only for PHP >= 8.1'); - } - $translator = new Translator(['status.OK' => 'OK', 'status.NOT_FOUND' => 'Introuvable']); $enum = new NativeTranslatedEnum(HTTPStatus::class, $translator, 'status.%s'); @@ -68,20 +56,12 @@ public function testIntBackedEnum(): void public function testEnumClassMustBeValid(): void { - if (\PHP_VERSION_ID < 80100) { - self::markTestSkipped('Only for PHP >= 8.1'); - } - $this->expectException(LogicException::class); new NativeTranslatedEnum(Vehicle::class, new Translator([]), 'dummy.%s'); } public function testLabelNotFound(): void { - if (\PHP_VERSION_ID < 80100) { - self::markTestSkipped('Only for PHP >= 8.1'); - } - $this->expectException(InvalidArgumentException::class); $translator = new Translator(['status.OK' => 'OK', 'status.NOT_FOUND' => 'Introuvable']); $enum = new NativeTranslatedEnum(HTTPStatus::class, $translator, 'status.%s'); diff --git a/tests/Unit/Translator.php b/tests/Unit/Translator.php index f1a71a0..2e7080c 100644 --- a/tests/Unit/Translator.php +++ b/tests/Unit/Translator.php @@ -1,84 +1,43 @@ = 6) { +/** + * @author Yann Eugoné + */ +class Translator implements TranslatorInterface +{ /** - * @author Yann Eugoné + * @var array */ - class Translator implements TranslatorInterface - { - /** - * @var array - */ - private $map; + private array $map; - /** - * @var string - */ - private $domain; + private string $domain; - public function __construct(array $map, string $domain = 'messages') - { - $this->map = $map; - $this->domain = $domain; - } - - public function getLocale(): string - { - return 'fr'; - } - - public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string - { - if ($domain !== $this->domain) { - return $id; - } - - return $this->map[$id] ?? $id; - } - } -} else { /** - * @author Yann Eugoné + * @param array $map */ - class Translator implements TranslatorInterface + public function __construct(array $map, string $domain = 'messages') { - /** - * @var array - */ - private $map; - - /** - * @var string - */ - private $domain; + $this->map = $map; + $this->domain = $domain; + } - public function __construct(array $map, string $domain = 'messages') - { - $this->map = $map; - $this->domain = $domain; - } + public function getLocale(): string + { + return 'fr'; + } - public function getLocale(): string - { - return 'fr'; + public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string + { + if ($domain !== $this->domain) { + return $id; } - public function trans($id, array $parameters = [], $domain = null, $locale = null) - { - if ($domain !== $this->domain) { - return $id; - } - - return $this->map[$id] ?? $id; - } + return $this->map[$id] ?? $id; } } diff --git a/tests/Unit/Twig/Extension/EnumExtensionTest.php b/tests/Unit/Twig/Extension/EnumExtensionTest.php index e6f28c6..a4bb935 100644 --- a/tests/Unit/Twig/Extension/EnumExtensionTest.php +++ b/tests/Unit/Twig/Extension/EnumExtensionTest.php @@ -28,11 +28,11 @@ public function testEnumLabel(): void self::assertSame( 'Customer', - $twig->createTemplate("{{ 'customer'|enum_label('type') }}")->render([]) + $twig->createTemplate("{{ 'customer'|enum_label('type') }}")->render() ); self::assertSame( 'Prospect', - $twig->createTemplate("{{ 'prospect'|enum_label('type') }}")->render([]) + $twig->createTemplate("{{ 'prospect'|enum_label('type') }}")->render() ); } @@ -54,7 +54,7 @@ public function testEnumChoices(): void <<render([]) + )->render() ); } @@ -71,7 +71,7 @@ public function testEnumValues(): void <<render([]) + )->render() ); }