From c7a63af824ee0607b0628179fabeded4f9e71961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Sat, 3 Mar 2018 19:50:25 +0100 Subject: [PATCH 01/15] Restructured code + Removed deprecated features --- .scrutinizer.yml | 6 +- .../ConventionedEnumCollectorCompilerPass.php | 42 ---- .../DeclarativeEnumCollectorCompilerPass.php | 211 ------------------ README.md | 27 ++- Registry/EnumRegistryInterface.php | 40 ---- ...ventionedEnumCollectorCompilerPassTest.php | 89 -------- .../Bundles/Acme/AppBundle/AcmeAppBundle.php | 12 - .../AppBundle/Enum/Customer/GenderEnum.php | 21 -- .../AppBundle/Enum/Customer/StateEnum.php | 21 -- .../Bundles/Acme/AppBundle/Enum/DummyEnum.php | 21 -- .../Acme/Bundle/AppBundle/AcmeAppBundle.php | 12 - .../AppBundle/Enum/Customer/GenderEnum.php | 21 -- .../AppBundle/Enum/Customer/StateEnum.php | 21 -- .../Acme/Bundle/AppBundle/Enum/DummyEnum.php | 21 -- .../Fixtures/Bundles/AppBundle/AppBundle.php | 12 - .../AppBundle/Enum/Customer/GenderEnum.php | 21 -- .../AppBundle/Enum/Customer/StateEnum.php | 21 -- .../Bundles/AppBundle/Enum/DummyEnum.php | 21 -- composer.json | 26 ++- {Resources/doc => doc}/declaring-enum.md | 8 +- .../doc => doc}/declaring-translated-enum.md | 8 +- {Resources/doc => doc}/sonata-admin.md | 2 +- phpunit.xml.dist | 10 +- {Enum => src}/AbstractTranslatedEnum.php | 4 +- .../TaggedEnumCollectorCompilerPass.php | 2 +- .../DependencyInjection}/Configuration.php | 6 +- .../DependencyInjection}/EnumExtension.php | 35 ++- .../Symfony/Bundle/Resources/config/enum.xml | 7 +- .../Symfony/Bundle/Resources/config/form.xml | 9 +- .../Symfony/Bundle/Resources}/config/twig.xml | 5 +- .../Bundle/Resources/config/validator.xml | 5 +- .../Bridge/Symfony/Bundle/YokaiEnumBundle.php | 8 +- .../Form}/Extension/EnumTypeGuesser.php | 14 +- .../Bridge/Symfony/Form}/Type/EnumType.php | 10 +- .../Symfony/Validator}/Constraints/Enum.php | 2 +- .../Validator}/Constraints/EnumValidator.php | 10 +- .../Bridge/Twig}/Extension/EnumExtension.php | 10 +- {Enum => src}/ConfigurableEnum.php | 2 +- {Enum => src}/ConfigurableTranslatedEnum.php | 2 +- {Enum => src}/EnumInterface.php | 2 +- {Registry => src}/EnumRegistry.php | 24 +- {Enum => src}/EnumWithClassAsNameTrait.php | 2 +- .../Exception}/DuplicatedEnumException.php | 2 +- .../Exception}/InvalidEnumException.php | 2 +- .../InvalidTranslatePatternException.php | 2 +- .../TaggedEnumCollectorCompilerPassTest.php | 4 +- .../DependencyInjection/EnumExtensionTest.php | 28 ++- .../Form/Extension/EnumTypeGuesserTest.php | 20 +- .../Bridge/Symfony}/Form/TestExtension.php | 14 +- .../Symfony}/Form/Type/EnumTypeTest.php | 11 +- .../Constraints/EnumValidatorTest.php | 15 +- .../Twig/Extension/EnumExtensionTest.php | 16 +- .../Enum => tests}/ConfigurableEnumTest.php | 4 +- .../ConfigurableTranslatedEnumTest.php | 6 +- .../Registry => tests}/EnumRegistryTest.php | 16 +- {Tests => tests}/Fixtures/GenderEnum.php | 6 +- {Tests => tests}/Fixtures/StateEnum.php | 4 +- .../Fixtures/SubscriptionEnum.php | 4 +- {Tests => tests}/Fixtures/TypeEnum.php | 4 +- {Tests => tests}/bootstrap.php | 0 60 files changed, 217 insertions(+), 794 deletions(-) delete mode 100644 DependencyInjection/CompilerPass/ConventionedEnumCollectorCompilerPass.php delete mode 100644 DependencyInjection/CompilerPass/DeclarativeEnumCollectorCompilerPass.php delete mode 100644 Registry/EnumRegistryInterface.php delete mode 100644 Tests/DependencyInjection/CompilerPass/ConventionedEnumCollectorCompilerPassTest.php delete mode 100644 Tests/Fixtures/Bundles/Acme/AppBundle/AcmeAppBundle.php delete mode 100644 Tests/Fixtures/Bundles/Acme/AppBundle/Enum/Customer/GenderEnum.php delete mode 100644 Tests/Fixtures/Bundles/Acme/AppBundle/Enum/Customer/StateEnum.php delete mode 100644 Tests/Fixtures/Bundles/Acme/AppBundle/Enum/DummyEnum.php delete mode 100644 Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/AcmeAppBundle.php delete mode 100644 Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/Customer/GenderEnum.php delete mode 100644 Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/Customer/StateEnum.php delete mode 100644 Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/DummyEnum.php delete mode 100644 Tests/Fixtures/Bundles/AppBundle/AppBundle.php delete mode 100644 Tests/Fixtures/Bundles/AppBundle/Enum/Customer/GenderEnum.php delete mode 100644 Tests/Fixtures/Bundles/AppBundle/Enum/Customer/StateEnum.php delete mode 100644 Tests/Fixtures/Bundles/AppBundle/Enum/DummyEnum.php rename {Resources/doc => doc}/declaring-enum.md (89%) rename {Resources/doc => doc}/declaring-translated-enum.md (91%) rename {Resources/doc => doc}/sonata-admin.md (97%) rename {Enum => src}/AbstractTranslatedEnum.php (94%) rename {DependencyInjection => src/Bridge/Symfony/Bundle/DependencyInjection}/CompilerPass/TaggedEnumCollectorCompilerPass.php (90%) rename {DependencyInjection => src/Bridge/Symfony/Bundle/DependencyInjection}/Configuration.php (81%) rename {DependencyInjection => src/Bridge/Symfony/Bundle/DependencyInjection}/EnumExtension.php (50%) rename Resources/config/services.xml => src/Bridge/Symfony/Bundle/Resources/config/enum.xml (76%) rename Resources/config/forms.xml => src/Bridge/Symfony/Bundle/Resources/config/form.xml (82%) rename {Resources => src/Bridge/Symfony/Bundle/Resources}/config/twig.xml (81%) rename Resources/config/validators.xml => src/Bridge/Symfony/Bundle/Resources/config/validator.xml (81%) rename YokaiEnumBundle.php => src/Bridge/Symfony/Bundle/YokaiEnumBundle.php (65%) rename {Form => src/Bridge/Symfony/Form}/Extension/EnumTypeGuesser.php (85%) rename {Form => src/Bridge/Symfony/Form}/Type/EnumType.php (86%) rename {Validator => src/Bridge/Symfony/Validator}/Constraints/Enum.php (88%) rename {Validator => src/Bridge/Symfony/Validator}/Constraints/EnumValidator.php (83%) rename {Twig => src/Bridge/Twig}/Extension/EnumExtension.php (84%) rename {Enum => src}/ConfigurableEnum.php (95%) rename {Enum => src}/ConfigurableTranslatedEnum.php (96%) rename {Enum => src}/EnumInterface.php (87%) rename {Registry => src}/EnumRegistry.php (65%) rename {Enum => src}/EnumWithClassAsNameTrait.php (86%) rename {Exception => src/Exception}/DuplicatedEnumException.php (91%) rename {Exception => src/Exception}/InvalidEnumException.php (90%) rename {Exception => src/Exception}/InvalidTranslatePatternException.php (92%) rename {Tests => tests/Bridge/Symfony/Bundle}/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php (90%) rename {Tests => tests/Bridge/Symfony/Bundle}/DependencyInjection/EnumExtensionTest.php (70%) rename {Tests => tests/Bridge/Symfony}/Form/Extension/EnumTypeGuesserTest.php (86%) rename {Tests => tests/Bridge/Symfony}/Form/TestExtension.php (70%) rename {Tests => tests/Bridge/Symfony}/Form/Type/EnumTypeTest.php (84%) rename {Tests => tests/Bridge/Symfony}/Validator/Constraints/EnumValidatorTest.php (87%) rename {Tests => tests/Bridge}/Twig/Extension/EnumExtensionTest.php (81%) rename {Tests/Enum => tests}/ConfigurableEnumTest.php (80%) rename {Tests/Enum => tests}/ConfigurableTranslatedEnumTest.php (92%) rename {Tests/Registry => tests}/EnumRegistryTest.php (80%) rename {Tests => tests}/Fixtures/GenderEnum.php (64%) rename {Tests => tests}/Fixtures/StateEnum.php (84%) rename {Tests => tests}/Fixtures/SubscriptionEnum.php (83%) rename {Tests => tests}/Fixtures/TypeEnum.php (73%) rename {Tests => tests}/bootstrap.php (100%) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 39dd4c9..e457268 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,5 +1,5 @@ filter: - excluded_paths: [vendor/*, Tests/*] + excluded_paths: [vendor/*, tests/*] before_commands: - 'composer install --dev --prefer-source' @@ -13,7 +13,7 @@ tools: php_pdepend: true php_loc: enabled: true - excluded_dirs: [vendor, Tests] + excluded_dirs: [vendor, tests] php_cpd: enabled: true - excluded_dirs: [vendor, Tests] + excluded_dirs: [vendor, tests] diff --git a/DependencyInjection/CompilerPass/ConventionedEnumCollectorCompilerPass.php b/DependencyInjection/CompilerPass/ConventionedEnumCollectorCompilerPass.php deleted file mode 100644 index ca6df01..0000000 --- a/DependencyInjection/CompilerPass/ConventionedEnumCollectorCompilerPass.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * @deprecated - */ -class ConventionedEnumCollectorCompilerPass implements CompilerPassInterface -{ - /** - * @inheritdoc - */ - public function process(ContainerBuilder $container) - { - $bundles = $container->getParameter('enum.register_bundles'); - - if (!$bundles) { - return; - } - - @trigger_error( - '"' . __CLASS__ . '" is deprecated since v2.2. Please use Symfony\'s PSR4 Service discovery instead.', - E_USER_DEPRECATED - ); - - if (true === $bundles) { - $bundles = $container->getParameter('kernel.bundles'); - } else { - $bundles = (array) $bundles; - } - - foreach ($bundles as $bundleClass) { - $declarativePass = new DeclarativeEnumCollectorCompilerPass($bundleClass); - $declarativePass->process($container); - } - } -} diff --git a/DependencyInjection/CompilerPass/DeclarativeEnumCollectorCompilerPass.php b/DependencyInjection/CompilerPass/DeclarativeEnumCollectorCompilerPass.php deleted file mode 100644 index 56f4a07..0000000 --- a/DependencyInjection/CompilerPass/DeclarativeEnumCollectorCompilerPass.php +++ /dev/null @@ -1,211 +0,0 @@ - - * - * @deprecated - */ -class DeclarativeEnumCollectorCompilerPass implements CompilerPassInterface -{ - /** - * @var string - */ - private $bundleDir; - - /** - * @var string - */ - private $bundleNamespace; - - /** - * @var string - */ - private $bundleName; - - /** - * @var string - */ - private $transDomain; - - /** - * @param string $bundle - * @param string|null $transDomain - */ - public function __construct($bundle, $transDomain = null) - { - $reflection = new ReflectionClass($bundle); - $this->bundleDir = dirname($reflection->getFileName()); - $this->bundleNamespace = $reflection->getNamespaceName(); - $this->bundleName = $reflection->getShortName(); - - $this->transDomain = $transDomain; - } - - /** - * @inheritdoc - */ - public function process(ContainerBuilder $container) - { - @trigger_error( - '"' . __CLASS__ . '" is deprecated since v2.2. Please use Symfony\'s PSR4 Service discovery instead.', - E_USER_DEPRECATED - ); - - if (!class_exists('Symfony\Component\Finder\Finder')) { - throw new RuntimeException('You need the symfony/finder component to register enums.'); - } - - $enumDir = $this->bundleDir . '/Enum'; - - if (!is_dir($enumDir)) { - return; - } - - $finder = new Finder(); - $finder->files()->name('*Enum.php')->in($enumDir); - - foreach ($finder as $file) { - /** @var SplFileInfo $file */ - $enumNamespace = $this->bundleNamespace . '\\Enum'; - if ($relativePath = $file->getRelativePath()) { - $enumNamespace .= '\\' . strtr($relativePath, '/', '\\'); - } - - $enumClass = $enumNamespace . '\\' . $file->getBasename('.php'); - $enumReflection = new ReflectionClass($enumClass); - - if (!$enumReflection->isSubclassOf(EnumInterface::class) || $enumReflection->isAbstract()) { - continue; //Not an enum or abstract enum - } - - $definition = null; - $requiredParameters = 0; - if ($enumReflection->getConstructor()) { - $requiredParameters = $enumReflection->getConstructor()->getNumberOfRequiredParameters(); - } - - if ($requiredParameters === 0) { - $definition = new Definition($enumClass); - } elseif ($requiredParameters === 2 && $enumReflection->isSubclassOf(AbstractTranslatedEnum::class)) { - if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) { - // ChildDefinition was introduced as Symfony 3.3 - $definition = new ChildDefinition('enum.abstract_translated'); - } else { - // DefinitionDecorator was deprecated as Symfony 3.3 - $definition = new DefinitionDecorator('enum.abstract_translated'); - } - $definition->setClass($enumClass); - $definition->addArgument( - $this->getTransPattern($enumClass) - ); - - if ($this->transDomain) { - $definition->addMethodCall( - 'setTransDomain', - [ - $this->transDomain - ] - ); - } - } - - if (!$definition) { - continue; //Could not determine how to create definition for the enum - } - - $definition->addTag('enum'); - - $container->setDefinition( - $this->getServiceId($enumClass), - $definition - ); - } - } - - /** - * @param string $enumClass - * - * @return string - */ - private function getServiceId($enumClass) - { - $enumNamespace = $this->bundleNamespace.'\\Enum\\'; - - return sprintf('%s.enum.%s', - Container::underscore( - substr($this->bundleName, 0, -6) - ), - Container::underscore( - str_replace( - '\\', - '', - str_replace( - $enumNamespace, - '', - substr($enumClass, 0, -4) - ) - ) - ) - ); - } - - /** - * @param string $enumClass - * - * @return string - */ - private function getTransPattern($enumClass) - { - $parts = array_filter( - array_map( - [$this, 'underscore'], - explode( - '\\', - str_replace( - $this->bundleNamespace . '\\', - '', - $enumClass - ) - ) - ) - ); - - $enum = array_pop($parts); - - return implode('_', $parts) . '.' . $enum . '.label_%s'; - } - - /** - * @param string $input - * - * @return string - */ - private function underscore($input) - { - return strtolower( - preg_replace( - '~(?<=\\w)([A-Z])~', '_$1', - preg_replace( - '~(Enum|Bundle)~', - '', - $input - ) - ) - ); - } -} diff --git a/README.md b/README.md index d11ed37..ec65af2 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,16 @@ YokaiEnumBundle [![Code Coverage](https://scrutinizer-ci.com/g/yokai-php/enum-bundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/yokai-php/enum-bundle/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/596d2076-90ee-49d9-a8b2-e3bcbd390874/mini.png)](https://insight.sensiolabs.com/projects/596d2076-90ee-49d9-a8b2-e3bcbd390874) -This repository aims to provide simple enumeration implementation to Symfony : +This library aims to provide simple enumeration implementation for PHP projects. + +First created as a Symfony bundle only, the doc is focused on integration with it. +But if you love this library, feel free to propose a bridge for you framework. Installation ------------ -### Add the bundle as dependency with Composer +### Add the library as a dependency with Composer ``` bash $ composer require yokai/enum-bundle @@ -31,7 +34,7 @@ $ composer require yokai/enum-bundle return [ // ... - Yokai\EnumBundle\YokaiEnumBundle::class => ['all' => true], + Yokai\Enum\Bridge\Symfony\Bundle\YokaiEnumBundle::class => ['all' => true], ]; ``` @@ -48,11 +51,11 @@ We first need to create the classes that will handle our enums : ```php = 2.8 -use Yokai\EnumBundle\Form\Type\EnumType; +use Yokai\Enum\Bridge\Symfony\Form\Type\EnumType; class MemberType extends AbstractType { @@ -131,8 +134,8 @@ Displaying the label for an enum value within a template : Recipes ------- -- Usage in [SonataAdminBundle](https://github.com/sonata-project/SonataAdminBundle) : see [doc](Resources/doc/sonata-admin.md) -- All the ways to declare [enums](Resources/doc/declaring-enum.md) or [translated enums](Resources/doc/declaring-translated-enum.md) +- Usage in [SonataAdminBundle](https://github.com/sonata-project/SonataAdminBundle) : see [doc](doc/sonata-admin.md) +- All the ways to declare [enums](doc/declaring-enum.md) or [translated enums](doc/declaring-translated-enum.md) MIT License diff --git a/Registry/EnumRegistryInterface.php b/Registry/EnumRegistryInterface.php deleted file mode 100644 index 270621f..0000000 --- a/Registry/EnumRegistryInterface.php +++ /dev/null @@ -1,40 +0,0 @@ - - */ -interface EnumRegistryInterface -{ - /** - * @param EnumInterface $enum - * - * @throws DuplicatedEnumException - */ - public function add(EnumInterface $enum); - - /** - * @param string $name - * - * @return EnumInterface - * @throws InvalidEnumException - */ - public function get($name); - - /** - * @param string $name - * - * @return bool - */ - public function has($name); - - /** - * @return EnumInterface[] - */ - public function all(); -} diff --git a/Tests/DependencyInjection/CompilerPass/ConventionedEnumCollectorCompilerPassTest.php b/Tests/DependencyInjection/CompilerPass/ConventionedEnumCollectorCompilerPassTest.php deleted file mode 100644 index 901566c..0000000 --- a/Tests/DependencyInjection/CompilerPass/ConventionedEnumCollectorCompilerPassTest.php +++ /dev/null @@ -1,89 +0,0 @@ - - */ -class ConventionedEnumCollectorCompilerPassTest extends \PHPUnit_Framework_TestCase -{ - /** - * @dataProvider getBundles - */ - public function testCollectThroughBundles($bundle, $prefix) - { - $container = $this->prophesize('Symfony\Component\DependencyInjection\ContainerBuilder'); - - $namespace = (new \ReflectionClass($bundle))->getNamespaceName(); - - $container->getParameter('enum.register_bundles') - ->shouldBeCalled() - ->willReturn([$bundle]); - - $container - ->setDefinition( - $prefix.'.enum.dummy', - Argument::allOf( - Argument::type('Symfony\Component\DependencyInjection\Definition'), - Argument::which('getTags', ['enum' => [[]]]), - Argument::which('getClass', $namespace.'\Enum\DummyEnum') - ) - ) - ->shouldBeCalled(); - $container - ->setDefinition( - $prefix.'.enum.customer_gender', - Argument::allOf( - Argument::type('Symfony\Component\DependencyInjection\Definition'), - Argument::which('getTags', ['enum' => [[]]]), - Argument::which('getClass', $namespace.'\Enum\Customer\GenderEnum') - ) - ) - ->shouldBeCalled(); - $class = 'Symfony\Component\DependencyInjection\DefinitionDecorator'; - if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) { - $class = 'Symfony\Component\DependencyInjection\ChildDefinition'; - } - $container - ->setDefinition( - $prefix.'.enum.customer_state', - Argument::allOf( - Argument::type($class), - Argument::which('getTags', ['enum' => [[]]]), - Argument::which('getClass', $namespace.'\Enum\Customer\StateEnum'), - Argument::which( - 'getArguments', - [ - 'customer.state.label_%s' - ] - ), - Argument::which('getParent', 'enum.abstract_translated') - ) - ) - ->shouldBeCalled(); - - $compiler = new ConventionedEnumCollectorCompilerPass(); - $compiler->process($container->reveal()); - } - - public function getBundles() - { - return [ - ['AppBundle\AppBundle', 'app'], - ['Acme\AppBundle\AcmeAppBundle', 'acme_app'], - ['Acme\Bundle\AppBundle\AcmeAppBundle', 'acme_app'], - ]; - } -} diff --git a/Tests/Fixtures/Bundles/Acme/AppBundle/AcmeAppBundle.php b/Tests/Fixtures/Bundles/Acme/AppBundle/AcmeAppBundle.php deleted file mode 100644 index 8a2a29f..0000000 --- a/Tests/Fixtures/Bundles/Acme/AppBundle/AcmeAppBundle.php +++ /dev/null @@ -1,12 +0,0 @@ - - */ -class AcmeAppBundle extends Bundle -{ -} diff --git a/Tests/Fixtures/Bundles/Acme/AppBundle/Enum/Customer/GenderEnum.php b/Tests/Fixtures/Bundles/Acme/AppBundle/Enum/Customer/GenderEnum.php deleted file mode 100644 index 052e963..0000000 --- a/Tests/Fixtures/Bundles/Acme/AppBundle/Enum/Customer/GenderEnum.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ -class GenderEnum implements EnumInterface -{ - public function getChoices() - { - return ['male' => 'Male', 'female' => 'Female']; - } - - public function getName() - { - return 'gender'; - } -} diff --git a/Tests/Fixtures/Bundles/Acme/AppBundle/Enum/Customer/StateEnum.php b/Tests/Fixtures/Bundles/Acme/AppBundle/Enum/Customer/StateEnum.php deleted file mode 100644 index 87f313f..0000000 --- a/Tests/Fixtures/Bundles/Acme/AppBundle/Enum/Customer/StateEnum.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ -class StateEnum extends AbstractTranslatedEnum -{ - protected function getValues() - { - return ['new', 'validated', 'disabled']; - } - - public function getName() - { - return 'state'; - } -} diff --git a/Tests/Fixtures/Bundles/Acme/AppBundle/Enum/DummyEnum.php b/Tests/Fixtures/Bundles/Acme/AppBundle/Enum/DummyEnum.php deleted file mode 100644 index fded8fb..0000000 --- a/Tests/Fixtures/Bundles/Acme/AppBundle/Enum/DummyEnum.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ -class DummyEnum implements EnumInterface -{ - public function getChoices() - { - return ['foo' => 'Foo', 'bar' => 'Bar']; - } - - public function getName() - { - return 'dummy'; - } -} diff --git a/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/AcmeAppBundle.php b/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/AcmeAppBundle.php deleted file mode 100644 index f6c6259..0000000 --- a/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/AcmeAppBundle.php +++ /dev/null @@ -1,12 +0,0 @@ - - */ -class AcmeAppBundle extends Bundle -{ -} diff --git a/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/Customer/GenderEnum.php b/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/Customer/GenderEnum.php deleted file mode 100644 index 073110d..0000000 --- a/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/Customer/GenderEnum.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ -class GenderEnum implements EnumInterface -{ - public function getChoices() - { - return ['male' => 'Male', 'female' => 'Female']; - } - - public function getName() - { - return 'gender'; - } -} diff --git a/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/Customer/StateEnum.php b/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/Customer/StateEnum.php deleted file mode 100644 index cd2f134..0000000 --- a/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/Customer/StateEnum.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ -class StateEnum extends AbstractTranslatedEnum -{ - protected function getValues() - { - return ['new', 'validated', 'disabled']; - } - - public function getName() - { - return 'state'; - } -} diff --git a/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/DummyEnum.php b/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/DummyEnum.php deleted file mode 100644 index c1ef2e4..0000000 --- a/Tests/Fixtures/Bundles/Acme/Bundle/AppBundle/Enum/DummyEnum.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ -class DummyEnum implements EnumInterface -{ - public function getChoices() - { - return ['foo' => 'Foo', 'bar' => 'Bar']; - } - - public function getName() - { - return 'dummy'; - } -} diff --git a/Tests/Fixtures/Bundles/AppBundle/AppBundle.php b/Tests/Fixtures/Bundles/AppBundle/AppBundle.php deleted file mode 100644 index c38ab20..0000000 --- a/Tests/Fixtures/Bundles/AppBundle/AppBundle.php +++ /dev/null @@ -1,12 +0,0 @@ - - */ -class AppBundle extends Bundle -{ -} diff --git a/Tests/Fixtures/Bundles/AppBundle/Enum/Customer/GenderEnum.php b/Tests/Fixtures/Bundles/AppBundle/Enum/Customer/GenderEnum.php deleted file mode 100644 index 5d8eef2..0000000 --- a/Tests/Fixtures/Bundles/AppBundle/Enum/Customer/GenderEnum.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ -class GenderEnum implements EnumInterface -{ - public function getChoices() - { - return ['male' => 'Male', 'female' => 'Female']; - } - - public function getName() - { - return 'gender'; - } -} diff --git a/Tests/Fixtures/Bundles/AppBundle/Enum/Customer/StateEnum.php b/Tests/Fixtures/Bundles/AppBundle/Enum/Customer/StateEnum.php deleted file mode 100644 index 8c3c519..0000000 --- a/Tests/Fixtures/Bundles/AppBundle/Enum/Customer/StateEnum.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ -class StateEnum extends AbstractTranslatedEnum -{ - protected function getValues() - { - return ['new', 'validated', 'disabled']; - } - - public function getName() - { - return 'state'; - } -} diff --git a/Tests/Fixtures/Bundles/AppBundle/Enum/DummyEnum.php b/Tests/Fixtures/Bundles/AppBundle/Enum/DummyEnum.php deleted file mode 100644 index 24fafc0..0000000 --- a/Tests/Fixtures/Bundles/AppBundle/Enum/DummyEnum.php +++ /dev/null @@ -1,21 +0,0 @@ - - */ -class DummyEnum implements EnumInterface -{ - public function getChoices() - { - return ['foo' => 'Foo', 'bar' => 'Bar']; - } - - public function getName() - { - return 'dummy'; - } -} diff --git a/composer.json b/composer.json index 7bdcbe7..7fb574c 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "yokai/enum-bundle", - "description": "Bring simple enumeration implementation to Symfony", - "type": "symfony-bundle", + "description": "Simple enumeration system with Symfony integration", + "type": "library", "license": "MIT", "authors": [ { @@ -10,18 +10,28 @@ } ], "require": { - "php": ">=5.6", + "php": ">=5.6" + }, + "require-dev": { + "doctrine/annotations": "~1.3", + "phpunit/phpunit": "~5.0", "symfony/framework-bundle": "~2.7|~3.0|~4.0", "symfony/form": "~2.7|~3.0|~4.0", "symfony/validator": "~2.7|~3.0|~4.0", - "twig/twig": "~1.20|~2.0" + "twig/twig": "~1.20|~2.0", + "symfony/twig-bundle": "~2.7|~3.0|~4.0" }, - "require-dev": { - "doctrine/annotations": "~1.3", - "phpunit/phpunit": "~5.0" + "suggest": { + "symfony/framework-bundle": "Integrate with Symfony Framework", + "symfony/form": "Add enum form type", + "symfony/validator": "Add enum validation", + "twig/twig": "Add enum util functions" }, "autoload": { - "psr-4": { "Yokai\\EnumBundle\\": "" } + "psr-4": { "Yokai\\Enum\\": "src" } + }, + "autoload-dev": { + "psr-4": { "Yokai\\Enum\\Tests\\": "tests" } }, "minimum-stability": "stable", "extra": { diff --git a/Resources/doc/declaring-enum.md b/doc/declaring-enum.md similarity index 89% rename from Resources/doc/declaring-enum.md rename to doc/declaring-enum.md index 14e7fe8..b2711db 100644 --- a/Resources/doc/declaring-enum.md +++ b/doc/declaring-enum.md @@ -19,7 +19,7 @@ Create a new class, implement both `getName` & `getChoices` methods. namespace App\Enum; -use Yokai\EnumBundle\Enum\EnumInterface; +use Yokai\Enum\EnumInterface; class GenderEnum implements EnumInterface { @@ -56,8 +56,8 @@ Create a new class, use `EnumWithClassAsNameTrait` trait and implement `getChoic namespace App\Enum; -use Yokai\EnumBundle\Enum\EnumInterface; -use Yokai\EnumBundle\Enum\EnumWithClassAsNameTrait; +use Yokai\Enum\EnumInterface; +use Yokai\Enum\EnumWithClassAsNameTrait; class GenderEnum implements EnumInterface { @@ -89,7 +89,7 @@ No need for a class, just use the `ConfigurableEnum` class and define a new enum ```yaml services: enum.member.gender: - class: 'Yokai\EnumBundle\Enum\ConfigurableEnum' + class: 'Yokai\Enum\ConfigurableEnum' public: false tags: ['enum'] arguments: diff --git a/Resources/doc/declaring-translated-enum.md b/doc/declaring-translated-enum.md similarity index 91% rename from Resources/doc/declaring-translated-enum.md rename to doc/declaring-translated-enum.md index 700ff70..42a4c1c 100644 --- a/Resources/doc/declaring-translated-enum.md +++ b/doc/declaring-translated-enum.md @@ -20,7 +20,7 @@ Create a new class, implement both `getName` & `getValues` methods and specify t namespace App\Enum; use Symfony\Component\Translation\TranslatorInterface; -use Yokai\EnumBundle\Enum\AbstractTranslatedEnum; +use Yokai\Enum\AbstractTranslatedEnum; class GenderEnum extends AbstractTranslatedEnum { @@ -65,8 +65,8 @@ Create a new class, use `EnumWithClassAsNameTrait` trait, implement `getValues` namespace App\Enum; use Symfony\Component\Translation\TranslatorInterface; -use Yokai\EnumBundle\Enum\AbstractTranslatedEnum; -use Yokai\EnumBundle\Enum\EnumWithClassAsNameTrait; +use Yokai\Enum\AbstractTranslatedEnum; +use Yokai\Enum\EnumWithClassAsNameTrait; class GenderEnum extends AbstractTranslatedEnum { @@ -105,7 +105,7 @@ No need for a class, just use the `ConfigurableTranslatedEnum` class and define ```yaml services: enum.member.gender: - class: 'Yokai\EnumBundle\Enum\ConfigurableTranslatedEnum' + class: 'Yokai\Enum\ConfigurableTranslatedEnum' public: false tags: ['enum'] arguments: diff --git a/Resources/doc/sonata-admin.md b/doc/sonata-admin.md similarity index 97% rename from Resources/doc/sonata-admin.md rename to doc/sonata-admin.md index e07e106..6044a14 100644 --- a/Resources/doc/sonata-admin.md +++ b/doc/sonata-admin.md @@ -16,7 +16,7 @@ use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Form\FormMapper; use Sonata\AdminBundle\Show\ShowMapper; -use Yokai\EnumBundle\Form\Type\EnumType; +use Yokai\Enum\Bridge\Symfony\Form\Type\EnumType; class MemberAdmin extends AbstractAdmin { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 74ed0e5..1a57d43 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,22 +9,18 @@ stopOnFailure="false" syntaxCheck="false" colors="true" - bootstrap="Tests/bootstrap.php" + bootstrap="tests/bootstrap.php" > - ./Tests + ./tests - ./ - - ./Tests - ./vendor - + ./src diff --git a/Enum/AbstractTranslatedEnum.php b/src/AbstractTranslatedEnum.php similarity index 94% rename from Enum/AbstractTranslatedEnum.php rename to src/AbstractTranslatedEnum.php index 4d629e8..aa9b1ff 100644 --- a/Enum/AbstractTranslatedEnum.php +++ b/src/AbstractTranslatedEnum.php @@ -1,9 +1,9 @@ diff --git a/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php b/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php similarity index 90% rename from DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php rename to src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php index 63863c7..43435ca 100644 --- a/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php @@ -1,6 +1,6 @@ children() - ->variableNode('register_bundles') - ->info('[DEPRECATED] bundles for which to auto-register enums.') - ->defaultFalse() - ->end() ->booleanNode('enum_autoconfiguration') ->info('If set to true, all services that implements EnumInterface, will obtain the "enum" tag automatically.') ->defaultTrue() diff --git a/DependencyInjection/EnumExtension.php b/src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php similarity index 50% rename from DependencyInjection/EnumExtension.php rename to src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php index 889aa39..beff043 100644 --- a/DependencyInjection/EnumExtension.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php @@ -1,13 +1,16 @@ @@ -22,13 +25,25 @@ public function load(array $configs, ContainerBuilder $container) $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $container->setParameter('enum.register_bundles', $config['register_bundles']); - $xmlLoader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); - $xmlLoader->load('services.xml'); - $xmlLoader->load('forms.xml'); - $xmlLoader->load('validators.xml'); - $xmlLoader->load('twig.xml'); + $xmlLoader->load('enum.xml'); + + $requiresForm = interface_exists(FormInterface::class); + $requiresValidator = interface_exists(ValidatorInterface::class); + $requiresTwig = class_exists(TwigBundle::class); + + if ($requiresForm) { + $xmlLoader->load('form.xml'); + if (!$requiresValidator) { + $container->removeDefinition('form_extention.type_guesser.enum'); + } + } + if ($requiresValidator) { + $xmlLoader->load('validator.xml'); + } + if ($requiresTwig) { + $xmlLoader->load('twig.xml'); + } if ($config['enum_autoconfiguration'] && method_exists($container, 'registerForAutoconfiguration')) { $container->registerForAutoconfiguration(EnumInterface::class) @@ -36,7 +51,7 @@ public function load(array $configs, ContainerBuilder $container) } if ($config['enum_registry_autoconfigurable']) { - $container->setAlias(EnumRegistryInterface::class, 'enum.registry'); + $container->setAlias(EnumRegistry::class, 'enum.registry'); } } } diff --git a/Resources/config/services.xml b/src/Bridge/Symfony/Bundle/Resources/config/enum.xml similarity index 76% rename from Resources/config/services.xml rename to src/Bridge/Symfony/Bundle/Resources/config/enum.xml index 76f17cc..b0e190e 100644 --- a/Resources/config/services.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/enum.xml @@ -5,13 +5,10 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - - - + + - diff --git a/Resources/config/forms.xml b/src/Bridge/Symfony/Bundle/Resources/config/form.xml similarity index 82% rename from Resources/config/forms.xml rename to src/Bridge/Symfony/Bundle/Resources/config/form.xml index 0c88ac9..99a8b73 100644 --- a/Resources/config/forms.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/form.xml @@ -5,20 +5,15 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - + - - - + - - diff --git a/Resources/config/twig.xml b/src/Bridge/Symfony/Bundle/Resources/config/twig.xml similarity index 81% rename from Resources/config/twig.xml rename to src/Bridge/Symfony/Bundle/Resources/config/twig.xml index f4e282c..e1f3039 100644 --- a/Resources/config/twig.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/twig.xml @@ -5,13 +5,10 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - + - - diff --git a/Resources/config/validators.xml b/src/Bridge/Symfony/Bundle/Resources/config/validator.xml similarity index 81% rename from Resources/config/validators.xml rename to src/Bridge/Symfony/Bundle/Resources/config/validator.xml index 42f99bf..7329b48 100644 --- a/Resources/config/validators.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/validator.xml @@ -5,13 +5,10 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - + - - diff --git a/YokaiEnumBundle.php b/src/Bridge/Symfony/Bundle/YokaiEnumBundle.php similarity index 65% rename from YokaiEnumBundle.php rename to src/Bridge/Symfony/Bundle/YokaiEnumBundle.php index a010f52..580f717 100644 --- a/YokaiEnumBundle.php +++ b/src/Bridge/Symfony/Bundle/YokaiEnumBundle.php @@ -1,12 +1,12 @@ diff --git a/Form/Extension/EnumTypeGuesser.php b/src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php similarity index 85% rename from Form/Extension/EnumTypeGuesser.php rename to src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php index d88e9cb..963a99b 100644 --- a/Form/Extension/EnumTypeGuesser.php +++ b/src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php @@ -1,6 +1,6 @@ @@ -18,7 +18,7 @@ class EnumTypeGuesser extends ValidatorTypeGuesser { /** - * @var EnumRegistryInterface + * @var EnumRegistry */ private $enumRegistry; @@ -29,9 +29,9 @@ class EnumTypeGuesser extends ValidatorTypeGuesser /** * @param MetadataFactoryInterface $metadataFactory - * @param EnumRegistryInterface $enumRegistry + * @param EnumRegistry $enumRegistry */ - public function __construct(MetadataFactoryInterface $metadataFactory, EnumRegistryInterface $enumRegistry) + public function __construct(MetadataFactoryInterface $metadataFactory, EnumRegistry $enumRegistry) { parent::__construct($metadataFactory); diff --git a/Form/Type/EnumType.php b/src/Bridge/Symfony/Form/Type/EnumType.php similarity index 86% rename from Form/Type/EnumType.php rename to src/Bridge/Symfony/Form/Type/EnumType.php index 4262897..007aae9 100644 --- a/Form/Type/EnumType.php +++ b/src/Bridge/Symfony/Form/Type/EnumType.php @@ -1,12 +1,12 @@ @@ -14,14 +14,14 @@ class EnumType extends AbstractType { /** - * @var EnumRegistryInterface + * @var EnumRegistry */ private $enumRegistry; /** - * @param EnumRegistryInterface $enumRegistry + * @param EnumRegistry $enumRegistry */ - public function __construct(EnumRegistryInterface $enumRegistry) + public function __construct(EnumRegistry $enumRegistry) { $this->enumRegistry = $enumRegistry; } diff --git a/Validator/Constraints/Enum.php b/src/Bridge/Symfony/Validator/Constraints/Enum.php similarity index 88% rename from Validator/Constraints/Enum.php rename to src/Bridge/Symfony/Validator/Constraints/Enum.php index 4373196..eb19a85 100644 --- a/Validator/Constraints/Enum.php +++ b/src/Bridge/Symfony/Validator/Constraints/Enum.php @@ -1,6 +1,6 @@ @@ -14,14 +14,14 @@ class EnumValidator extends ChoiceValidator { /** - * @var EnumRegistryInterface + * @var EnumRegistry */ private $enumRegistry; /** - * @param EnumRegistryInterface $enumRegistry + * @param EnumRegistry $enumRegistry */ - public function __construct(EnumRegistryInterface $enumRegistry) + public function __construct(EnumRegistry $enumRegistry) { $this->enumRegistry = $enumRegistry; } diff --git a/Twig/Extension/EnumExtension.php b/src/Bridge/Twig/Extension/EnumExtension.php similarity index 84% rename from Twig/Extension/EnumExtension.php rename to src/Bridge/Twig/Extension/EnumExtension.php index 60d8972..3cb6039 100644 --- a/Twig/Extension/EnumExtension.php +++ b/src/Bridge/Twig/Extension/EnumExtension.php @@ -1,11 +1,11 @@ @@ -13,14 +13,14 @@ class EnumExtension extends Twig_Extension { /** - * @var EnumRegistryInterface + * @var EnumRegistry */ private $registry; /** - * @param EnumRegistryInterface $registry + * @param EnumRegistry $registry */ - public function __construct(EnumRegistryInterface $registry) + public function __construct(EnumRegistry $registry) { $this->registry = $registry; } diff --git a/Enum/ConfigurableEnum.php b/src/ConfigurableEnum.php similarity index 95% rename from Enum/ConfigurableEnum.php rename to src/ConfigurableEnum.php index 1683071..fad4006 100644 --- a/Enum/ConfigurableEnum.php +++ b/src/ConfigurableEnum.php @@ -1,6 +1,6 @@ diff --git a/Enum/ConfigurableTranslatedEnum.php b/src/ConfigurableTranslatedEnum.php similarity index 96% rename from Enum/ConfigurableTranslatedEnum.php rename to src/ConfigurableTranslatedEnum.php index 1b3b752..956e7b1 100644 --- a/Enum/ConfigurableTranslatedEnum.php +++ b/src/ConfigurableTranslatedEnum.php @@ -1,6 +1,6 @@ diff --git a/Registry/EnumRegistry.php b/src/EnumRegistry.php similarity index 65% rename from Registry/EnumRegistry.php rename to src/EnumRegistry.php index a80afb2..26d1bd2 100644 --- a/Registry/EnumRegistry.php +++ b/src/EnumRegistry.php @@ -1,15 +1,14 @@ */ -class EnumRegistry implements EnumRegistryInterface +class EnumRegistry { /** * @var EnumInterface[] @@ -17,7 +16,9 @@ class EnumRegistry implements EnumRegistryInterface private $enums; /** - * @inheritdoc + * @param EnumInterface $enum + * + * @throws DuplicatedEnumException */ public function add(EnumInterface $enum) { @@ -29,7 +30,10 @@ public function add(EnumInterface $enum) } /** - * @inheritdoc + * @param string $name + * + * @return EnumInterface + * @throws InvalidEnumException */ public function get($name) { @@ -41,7 +45,9 @@ public function get($name) } /** - * @inheritdoc + * @param string $name + * + * @return bool */ public function has($name) { @@ -49,7 +55,7 @@ public function has($name) } /** - * @inheritDoc + * @return EnumInterface[] */ public function all() { diff --git a/Enum/EnumWithClassAsNameTrait.php b/src/EnumWithClassAsNameTrait.php similarity index 86% rename from Enum/EnumWithClassAsNameTrait.php rename to src/EnumWithClassAsNameTrait.php index 2e8898f..c2e45c6 100644 --- a/Enum/EnumWithClassAsNameTrait.php +++ b/src/EnumWithClassAsNameTrait.php @@ -1,6 +1,6 @@ diff --git a/Exception/DuplicatedEnumException.php b/src/Exception/DuplicatedEnumException.php similarity index 91% rename from Exception/DuplicatedEnumException.php rename to src/Exception/DuplicatedEnumException.php index 9a87a83..436aa0d 100644 --- a/Exception/DuplicatedEnumException.php +++ b/src/Exception/DuplicatedEnumException.php @@ -1,6 +1,6 @@ diff --git a/Tests/DependencyInjection/EnumExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php similarity index 70% rename from Tests/DependencyInjection/EnumExtensionTest.php rename to tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php index 2e14b23..2a077e7 100644 --- a/Tests/DependencyInjection/EnumExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php @@ -1,11 +1,12 @@ @@ -60,7 +61,7 @@ public function it_register_enum_registry_alias_for_autowire_by_default() { $container = new ContainerBuilder(); $this->extension()->load([[]], $container); - $this->assertTrue($container->hasAlias(EnumRegistryInterface::class)); + $this->assertTrue($container->hasAlias(EnumRegistry::class)); } /** @@ -70,6 +71,21 @@ public function it_do_not_register_enum_registry_alias_for_autowire_if_asked_to( { $container = new ContainerBuilder(); $this->extension()->load([['enum_registry_autoconfigurable' => false]], $container); - $this->assertFalse($container->hasAlias(EnumRegistryInterface::class)); + $this->assertFalse($container->hasAlias(EnumRegistry::class)); + } + + + /** + * @test + */ + public function it_register_services() + { + $container = new ContainerBuilder(); + $this->extension()->load([[]], $container); + $services = ['form_type.enum', 'form_extention.type_guesser.enum', 'validator.enum', 'twig.extension.enum']; + + foreach ($services as $service) { + self::assertTrue($container->has($service), sprintf('Service "%s" is registered', $service)); + } } } diff --git a/Tests/Form/Extension/EnumTypeGuesserTest.php b/tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php similarity index 86% rename from Tests/Form/Extension/EnumTypeGuesserTest.php rename to tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php index 63ac462..04d7c95 100644 --- a/Tests/Form/Extension/EnumTypeGuesserTest.php +++ b/tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php @@ -1,6 +1,6 @@ */ class EnumTypeGuesserTest extends TypeTestCase { - const TEST_CLASS = 'Yokai\EnumBundle\Tests\Form\Extension\EnumTypeGuesserTest_TestClass'; + const TEST_CLASS = 'Yokai\Enum\Tests\Bridge\Symfony\Form\Extension\EnumTypeGuesserTest_TestClass'; const TEST_PROPERTY = 'property'; @@ -32,7 +32,7 @@ class EnumTypeGuesserTest extends TypeTestCase private $guesser; /** - * @var ObjectProphecy|EnumRegistryInterface + * @var ObjectProphecy|EnumRegistry */ private $enumRegistry; @@ -48,7 +48,7 @@ class EnumTypeGuesserTest extends TypeTestCase protected function setUp() { - $this->enumRegistry = $this->prophesize('Yokai\EnumBundle\Registry\EnumRegistryInterface'); + $this->enumRegistry = $this->prophesize(EnumRegistry::class); $this->enumRegistry->has('state')->willReturn(false); $this->enumRegistry->has(GenderEnum::class)->willReturn(true); $this->enumRegistry->get(GenderEnum::class)->willReturn(new GenderEnum); diff --git a/Tests/Form/TestExtension.php b/tests/Bridge/Symfony/Form/TestExtension.php similarity index 70% rename from Tests/Form/TestExtension.php rename to tests/Bridge/Symfony/Form/TestExtension.php index 4b49be8..d97642b 100644 --- a/Tests/Form/TestExtension.php +++ b/tests/Bridge/Symfony/Form/TestExtension.php @@ -1,12 +1,12 @@ @@ -14,7 +14,7 @@ class TestExtension extends AbstractExtension { /** - * @var EnumRegistryInterface + * @var EnumRegistry */ private $enumRegistry; @@ -24,10 +24,10 @@ class TestExtension extends AbstractExtension private $metadataFactory; /** - * @param EnumRegistryInterface $enumRegistry + * @param EnumRegistry $enumRegistry * @param MetadataFactoryInterface|null $metadataFactory */ - public function __construct(EnumRegistryInterface $enumRegistry, MetadataFactoryInterface $metadataFactory = null) + public function __construct(EnumRegistry $enumRegistry, MetadataFactoryInterface $metadataFactory = null) { $this->enumRegistry = $enumRegistry; $this->metadataFactory = $metadataFactory; diff --git a/Tests/Form/Type/EnumTypeTest.php b/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php similarity index 84% rename from Tests/Form/Type/EnumTypeTest.php rename to tests/Bridge/Symfony/Form/Type/EnumTypeTest.php index c6a0399..d993a36 100644 --- a/Tests/Form/Type/EnumTypeTest.php +++ b/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php @@ -1,12 +1,13 @@ @@ -17,7 +18,7 @@ class EnumTypeTest extends TypeTestCase protected function setUp() { - $this->enumRegistry = $this->prophesize('Yokai\EnumBundle\Registry\EnumRegistryInterface'); + $this->enumRegistry = $this->prophesize(EnumRegistry::class); $this->enumRegistry->has('state')->willReturn(false); $this->enumRegistry->has(GenderEnum::class)->willReturn(true); $this->enumRegistry->get(GenderEnum::class)->willReturn(new GenderEnum); diff --git a/Tests/Validator/Constraints/EnumValidatorTest.php b/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php similarity index 87% rename from Tests/Validator/Constraints/EnumValidatorTest.php rename to tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php index 3f660c8..c0a5ddd 100644 --- a/Tests/Validator/Constraints/EnumValidatorTest.php +++ b/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php @@ -1,23 +1,23 @@ */ class EnumValidatorTest extends AbstractConstraintValidatorTest { - protected function createValidator() { - $registry = $this->prophesize('Yokai\EnumBundle\Registry\EnumRegistryInterface'); + $registry = $this->prophesize(EnumRegistry::class); $registry->has('state')->willReturn(false); $registry->has(GenderEnum::class)->willReturn(true); $registry->has('type')->willReturn(true); @@ -91,5 +91,4 @@ public function testInvalidMultipleEnum() ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->assertRaised(); } - } diff --git a/Tests/Twig/Extension/EnumExtensionTest.php b/tests/Bridge/Twig/Extension/EnumExtensionTest.php similarity index 81% rename from Tests/Twig/Extension/EnumExtensionTest.php rename to tests/Bridge/Twig/Extension/EnumExtensionTest.php index fdb50b7..ccc066e 100644 --- a/Tests/Twig/Extension/EnumExtensionTest.php +++ b/tests/Bridge/Twig/Extension/EnumExtensionTest.php @@ -1,9 +1,11 @@ @@ -11,13 +13,13 @@ class EnumExtensionTest extends \PHPUnit_Framework_TestCase { /** - * @var EnumRegistryInterface|\Prophecy\Prophecy\ObjectProphecy + * @var EnumRegistry|ObjectProphecy */ private $registry; protected function setUp() { - $this->registry = $this->prophesize('Yokai\EnumBundle\Registry\EnumRegistryInterface'); + $this->registry = $this->prophesize(EnumRegistry::class); } protected function tearDown() @@ -29,7 +31,7 @@ protected function tearDown() public function testEnumLabel() { - $enum = $this->prophesize('Yokai\EnumBundle\Enum\EnumInterface'); + $enum = $this->prophesize(EnumInterface::class); $enum->getChoices() ->willReturn(['foo' => 'FOO', 'bar' => 'BAR']); @@ -59,7 +61,7 @@ public function testEnumLabel() public function testEnumChoices() { - $enum = $this->prophesize('Yokai\EnumBundle\Enum\EnumInterface'); + $enum = $this->prophesize(EnumInterface::class); $enum->getChoices() ->willReturn(['foo' => 'FOO', 'bar' => 'BAR']); diff --git a/Tests/Enum/ConfigurableEnumTest.php b/tests/ConfigurableEnumTest.php similarity index 80% rename from Tests/Enum/ConfigurableEnumTest.php rename to tests/ConfigurableEnumTest.php index a5ab0db..aa6f5eb 100644 --- a/Tests/Enum/ConfigurableEnumTest.php +++ b/tests/ConfigurableEnumTest.php @@ -1,8 +1,8 @@ @@ -13,7 +13,7 @@ class ConfigurableTranslatedEnumTest extends \PHPUnit_Framework_TestCase { public function testConstructedWithInvalidPattern() { - $this->expectException('Yokai\EnumBundle\Exception\InvalidTranslatePatternException'); + $this->expectException('Yokai\Enum\Exception\InvalidTranslatePatternException'); $translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface'); new ConfigurableTranslatedEnum($translator->reveal(), 'invalid.pattern', 'invalid', ['foo', 'bar']); } diff --git a/Tests/Registry/EnumRegistryTest.php b/tests/EnumRegistryTest.php similarity index 80% rename from Tests/Registry/EnumRegistryTest.php rename to tests/EnumRegistryTest.php index 3a0192c..f159b33 100644 --- a/Tests/Registry/EnumRegistryTest.php +++ b/tests/EnumRegistryTest.php @@ -1,12 +1,12 @@ @@ -30,14 +30,14 @@ protected function tearDown() public function testAddDuplicatedException() { - $this->expectException('Yokai\EnumBundle\Exception\DuplicatedEnumException'); + $this->expectException('Yokai\Enum\Exception\DuplicatedEnumException'); $this->registry->add(new GenderEnum); $this->registry->add(new GenderEnum); } public function testGetInvalidException() { - $this->expectException('Yokai\EnumBundle\Exception\InvalidEnumException'); + $this->expectException('Yokai\Enum\Exception\InvalidEnumException'); $this->registry->add(new GenderEnum); $this->registry->get('type'); } diff --git a/Tests/Fixtures/GenderEnum.php b/tests/Fixtures/GenderEnum.php similarity index 64% rename from Tests/Fixtures/GenderEnum.php rename to tests/Fixtures/GenderEnum.php index d56042a..edc1ee3 100644 --- a/Tests/Fixtures/GenderEnum.php +++ b/tests/Fixtures/GenderEnum.php @@ -1,9 +1,9 @@ diff --git a/Tests/Fixtures/StateEnum.php b/tests/Fixtures/StateEnum.php similarity index 84% rename from Tests/Fixtures/StateEnum.php rename to tests/Fixtures/StateEnum.php index 465fe19..311a701 100644 --- a/Tests/Fixtures/StateEnum.php +++ b/tests/Fixtures/StateEnum.php @@ -1,9 +1,9 @@ diff --git a/Tests/Fixtures/SubscriptionEnum.php b/tests/Fixtures/SubscriptionEnum.php similarity index 83% rename from Tests/Fixtures/SubscriptionEnum.php rename to tests/Fixtures/SubscriptionEnum.php index d42128b..a90b9ce 100644 --- a/Tests/Fixtures/SubscriptionEnum.php +++ b/tests/Fixtures/SubscriptionEnum.php @@ -1,9 +1,9 @@ diff --git a/Tests/Fixtures/TypeEnum.php b/tests/Fixtures/TypeEnum.php similarity index 73% rename from Tests/Fixtures/TypeEnum.php rename to tests/Fixtures/TypeEnum.php index 426a14c..e62b800 100644 --- a/Tests/Fixtures/TypeEnum.php +++ b/tests/Fixtures/TypeEnum.php @@ -1,8 +1,8 @@ diff --git a/Tests/bootstrap.php b/tests/bootstrap.php similarity index 100% rename from Tests/bootstrap.php rename to tests/bootstrap.php From 254ac439c1ebcbfee7dcfeaf9c2d875a73f1b132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Fri, 31 May 2019 12:17:22 +0200 Subject: [PATCH 02/15] Updated php + symfony + phpunit versions minimum requirements --- .travis.yml | 16 +++------------- composer.json | 16 ++++++++-------- phpunit.xml.dist | 1 - src/AbstractTranslatedEnum.php | 2 +- src/ConfigurableTranslatedEnum.php | 3 ++- .../TaggedEnumCollectorCompilerPassTest.php | 3 ++- .../DependencyInjection/EnumExtensionTest.php | 3 ++- .../Validator/Constraints/EnumValidatorTest.php | 4 ++++ .../Bridge/Twig/Extension/EnumExtensionTest.php | 3 ++- tests/ConfigurableEnumTest.php | 3 ++- tests/ConfigurableTranslatedEnumTest.php | 11 ++++++----- tests/EnumRegistryTest.php | 6 ++++-- tests/Fixtures/StateEnum.php | 2 +- tests/Fixtures/SubscriptionEnum.php | 2 +- 14 files changed, 38 insertions(+), 37 deletions(-) diff --git a/.travis.yml b/.travis.yml index be44bc1..53bbdc5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,23 +1,13 @@ language: php php: - - 5.6 - - 7.0 - 7.1 - 7.2 + - 7.3 env: - - SYMFONY_VERSION="2.7.*" - - SYMFONY_VERSION="2.8.*" - - SYMFONY_VERSION="3.4.*" - - SYMFONY_VERSION="4.0.*" - -matrix: - exclude: - - php: 5.6 - env: SYMFONY_VERSION="4.0.*" # Symfony >= 4.0 PHP requirement is ^7.1.3 - - php: 7.0 - env: SYMFONY_VERSION="4.0.*" # Symfony >= 4.0 PHP requirement is ^7.1.3 + - SYMFONY_VERSION="4.2.*" + - SYMFONY_VERSION="4.3.*" sudo: false diff --git a/composer.json b/composer.json index 7fb574c..0f81a13 100644 --- a/composer.json +++ b/composer.json @@ -10,16 +10,16 @@ } ], "require": { - "php": ">=5.6" + "php": "^7.1.3" }, "require-dev": { "doctrine/annotations": "~1.3", - "phpunit/phpunit": "~5.0", - "symfony/framework-bundle": "~2.7|~3.0|~4.0", - "symfony/form": "~2.7|~3.0|~4.0", - "symfony/validator": "~2.7|~3.0|~4.0", - "twig/twig": "~1.20|~2.0", - "symfony/twig-bundle": "~2.7|~3.0|~4.0" + "phpunit/phpunit": "~7.0", + "symfony/framework-bundle": "^4.1", + "symfony/form": "^4.1", + "symfony/validator": "^4.1", + "twig/twig": "^2.0", + "symfony/twig-bundle": "^4.1" }, "suggest": { "symfony/framework-bundle": "Integrate with Symfony Framework", @@ -36,7 +36,7 @@ "minimum-stability": "stable", "extra": { "branch-alias": { - "dev-master": "2.x-dev" + "dev-master": "3.x-dev" } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1a57d43..827c012 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,7 +7,6 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" colors="true" bootstrap="tests/bootstrap.php" > diff --git a/src/AbstractTranslatedEnum.php b/src/AbstractTranslatedEnum.php index aa9b1ff..ed335e5 100644 --- a/src/AbstractTranslatedEnum.php +++ b/src/AbstractTranslatedEnum.php @@ -2,7 +2,7 @@ namespace Yokai\Enum; -use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\Enum\Exception\InvalidTranslatePatternException; /** diff --git a/src/ConfigurableTranslatedEnum.php b/src/ConfigurableTranslatedEnum.php index 956e7b1..621722e 100644 --- a/src/ConfigurableTranslatedEnum.php +++ b/src/ConfigurableTranslatedEnum.php @@ -2,7 +2,8 @@ namespace Yokai\Enum; -use Symfony\Component\Translation\TranslatorInterface; + +use Symfony\Contracts\Translation\TranslatorInterface; /** * @author Yann Eugoné diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php index 3e2d311..7163f0f 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php @@ -2,13 +2,14 @@ namespace Yokai\Enum\Tests\Bridge\Symfony\Bundle\DependencyInjection\CompilerPass; +use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\Reference; use Yokai\Enum\Bridge\Symfony\Bundle\DependencyInjection\CompilerPass\TaggedEnumCollectorCompilerPass; /** * @author Yann Eugoné */ -class TaggedEnumCollectorCompilerPassTest extends \PHPUnit_Framework_TestCase +class TaggedEnumCollectorCompilerPassTest extends TestCase { /** * @var TaggedEnumCollectorCompilerPass diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php index 2a077e7..9094e07 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php @@ -2,6 +2,7 @@ namespace Yokai\Enum\Tests\Bridge\Symfony\Bundle\DependencyInjection; +use PHPUnit\Framework\TestCase; use Symfony\Bundle\TwigBundle\TwigBundle; use Symfony\Component\DependencyInjection\ContainerBuilder; use Yokai\Enum\Bridge\Symfony\Bundle\DependencyInjection\EnumExtension; @@ -11,7 +12,7 @@ /** * @author Yann Eugoné */ -class EnumExtensionTest extends \PHPUnit_Framework_TestCase +class EnumExtensionTest extends TestCase { /** * @return EnumExtension diff --git a/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php b/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php index c0a5ddd..072a692 100644 --- a/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php +++ b/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php @@ -2,6 +2,7 @@ namespace Yokai\Enum\Tests\Bridge\Symfony\Validator\Constraints; +use Prophecy\Prophecy\ObjectProphecy; use Symfony\Component\Validator\Constraints\Choice; use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest; use Yokai\Enum\Bridge\Symfony\Validator\Constraints\Enum; @@ -17,6 +18,7 @@ class EnumValidatorTest extends AbstractConstraintValidatorTest { protected function createValidator() { + /** @var EnumRegistry|ObjectProphecy $registry */ $registry = $this->prophesize(EnumRegistry::class); $registry->has('state')->willReturn(false); $registry->has(GenderEnum::class)->willReturn(true); @@ -66,6 +68,7 @@ public function testInvalidSingleEnum() $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"foo"') + ->setParameter('{{ choices }}', '"customer", "prospect"') ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->assertRaised(); } @@ -87,6 +90,7 @@ public function testInvalidMultipleEnum() $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"foo"') + ->setParameter('{{ choices }}', '"customer", "prospect"') ->setInvalidValue('foo') ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->assertRaised(); diff --git a/tests/Bridge/Twig/Extension/EnumExtensionTest.php b/tests/Bridge/Twig/Extension/EnumExtensionTest.php index ccc066e..375052c 100644 --- a/tests/Bridge/Twig/Extension/EnumExtensionTest.php +++ b/tests/Bridge/Twig/Extension/EnumExtensionTest.php @@ -2,6 +2,7 @@ namespace Yokai\Enum\Tests\Bridge\Symfony\Twig\Extension; +use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; use Yokai\Enum\Bridge\Twig\Extension\EnumExtension; use Yokai\Enum\EnumInterface; @@ -10,7 +11,7 @@ /** * @author Yann Eugoné */ -class EnumExtensionTest extends \PHPUnit_Framework_TestCase +class EnumExtensionTest extends TestCase { /** * @var EnumRegistry|ObjectProphecy diff --git a/tests/ConfigurableEnumTest.php b/tests/ConfigurableEnumTest.php index aa6f5eb..76334f7 100644 --- a/tests/ConfigurableEnumTest.php +++ b/tests/ConfigurableEnumTest.php @@ -2,9 +2,10 @@ namespace Yokai\Enum\Tests; +use PHPUnit\Framework\TestCase; use Yokai\Enum\ConfigurableEnum; -class ConfigurableEnumTest extends \PHPUnit_Framework_TestCase +class ConfigurableEnumTest extends TestCase { public function testConfigurability() { diff --git a/tests/ConfigurableTranslatedEnumTest.php b/tests/ConfigurableTranslatedEnumTest.php index b907d64..0ab8bb8 100644 --- a/tests/ConfigurableTranslatedEnumTest.php +++ b/tests/ConfigurableTranslatedEnumTest.php @@ -2,26 +2,27 @@ namespace Yokai\Enum\Tests; +use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; -use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\Enum\ConfigurableTranslatedEnum; /** * @author Yann Eugoné */ -class ConfigurableTranslatedEnumTest extends \PHPUnit_Framework_TestCase +class ConfigurableTranslatedEnumTest extends TestCase { public function testConstructedWithInvalidPattern() { $this->expectException('Yokai\Enum\Exception\InvalidTranslatePatternException'); - $translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface'); + $translator = $this->prophesize(TranslatorInterface::class); new ConfigurableTranslatedEnum($translator->reveal(), 'invalid.pattern', 'invalid', ['foo', 'bar']); } public function testTranslatedChoices() { /** @var ObjectProphecy|TranslatorInterface $translator */ - $translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface'); + $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']); @@ -36,7 +37,7 @@ public function testTranslatedChoices() public function testTranslatedWithDomainChoices() { /** @var ObjectProphecy|TranslatorInterface $translator */ - $translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface'); + $translator = $this->prophesize(TranslatorInterface::class); $translator->trans('choice.something.foo', [], 'messages', null)->shouldNotBeCalled(); $translator->trans('choice.something.bar', [], 'messages', null)->shouldNotBeCalled(); $translator->trans('something.foo', [], 'choices', null)->shouldBeCalled()->willReturn('FOO translated'); diff --git a/tests/EnumRegistryTest.php b/tests/EnumRegistryTest.php index f159b33..ea82a14 100644 --- a/tests/EnumRegistryTest.php +++ b/tests/EnumRegistryTest.php @@ -2,6 +2,8 @@ namespace Yokai\Enum\Tests; +use PHPUnit\Framework\TestCase; +use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\Enum\EnumRegistry; use Yokai\Enum\Tests\Fixtures\GenderEnum; use Yokai\Enum\Tests\Fixtures\StateEnum; @@ -11,7 +13,7 @@ /** * @author Yann Eugoné */ -class EnumRegistryTest extends \PHPUnit_Framework_TestCase +class EnumRegistryTest extends TestCase { /** * @var EnumRegistry @@ -44,7 +46,7 @@ public function testGetInvalidException() public function testAddNominal() { - $translator = $this->prophesize('Symfony\Component\Translation\TranslatorInterface')->reveal(); + $translator = $this->prophesize(TranslatorInterface::class)->reveal(); $gender = new GenderEnum; $state = new StateEnum($translator); $subscription = new SubscriptionEnum($translator); diff --git a/tests/Fixtures/StateEnum.php b/tests/Fixtures/StateEnum.php index 311a701..5115606 100644 --- a/tests/Fixtures/StateEnum.php +++ b/tests/Fixtures/StateEnum.php @@ -2,7 +2,7 @@ namespace Yokai\Enum\Tests\Fixtures; -use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\Enum\AbstractTranslatedEnum; /** diff --git a/tests/Fixtures/SubscriptionEnum.php b/tests/Fixtures/SubscriptionEnum.php index a90b9ce..c0a3ee7 100644 --- a/tests/Fixtures/SubscriptionEnum.php +++ b/tests/Fixtures/SubscriptionEnum.php @@ -2,7 +2,7 @@ namespace Yokai\Enum\Tests\Fixtures; -use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\Enum\ConfigurableTranslatedEnum; /** From 4bb33f2f6d9c1603b1c60809c2cdf6967ff5bdbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Fri, 31 May 2019 12:28:29 +0200 Subject: [PATCH 03/15] Strict types everywhere --- doc/declaring-enum.md | 6 ++--- doc/declaring-translated-enum.md | 10 ++++---- doc/sonata-admin.md | 8 +++---- src/AbstractTranslatedEnum.php | 12 +++++----- .../TaggedEnumCollectorCompilerPass.php | 4 ++-- .../DependencyInjection/Configuration.php | 4 ++-- .../DependencyInjection/EnumExtension.php | 4 ++-- src/Bridge/Symfony/Bundle/YokaiEnumBundle.php | 8 +++---- .../Form/Extension/EnumTypeGuesser.php | 11 +++++---- src/Bridge/Symfony/Form/Type/EnumType.php | 14 +++++------ .../Symfony/Validator/Constraints/Enum.php | 9 ++++--- .../Validator/Constraints/EnumValidator.php | 4 ++-- src/Bridge/Twig/Extension/EnumExtension.php | 20 ++++++---------- src/ConfigurableEnum.php | 8 +++---- src/ConfigurableTranslatedEnum.php | 8 +++---- src/EnumInterface.php | 6 ++--- src/EnumRegistry.php | 10 ++++---- src/EnumWithClassAsNameTrait.php | 4 ++-- src/Exception/DuplicatedEnumException.php | 4 ++-- src/Exception/InvalidEnumException.php | 4 ++-- .../InvalidTranslatePatternException.php | 4 ++-- .../TaggedEnumCollectorCompilerPassTest.php | 10 ++++---- .../DependencyInjection/EnumExtensionTest.php | 18 ++++++-------- .../Form/Extension/EnumTypeGuesserTest.php | 20 ++++++++-------- tests/Bridge/Symfony/Form/TestExtension.php | 6 ++--- .../Bridge/Symfony/Form/Type/EnumTypeTest.php | 15 ++++++------ .../Constraints/EnumValidatorTest.php | 24 +++++++++---------- .../Twig/Extension/EnumExtensionTest.php | 14 +++++------ tests/ConfigurableEnumTest.php | 4 ++-- tests/ConfigurableTranslatedEnumTest.php | 8 +++---- tests/EnumRegistryTest.php | 12 +++++----- tests/Fixtures/GenderEnum.php | 4 ++-- tests/Fixtures/StateEnum.php | 6 ++--- tests/Fixtures/SubscriptionEnum.php | 2 +- tests/Fixtures/TypeEnum.php | 2 +- tests/bootstrap.php | 21 ++++------------ 36 files changed, 155 insertions(+), 173 deletions(-) diff --git a/doc/declaring-enum.md b/doc/declaring-enum.md index b2711db..b6d77cc 100644 --- a/doc/declaring-enum.md +++ b/doc/declaring-enum.md @@ -23,12 +23,12 @@ use Yokai\Enum\EnumInterface; class GenderEnum implements EnumInterface { - public function getName() + public function getName(): string { return 'gender'; } - public function getChoices() + public function getChoices(): array { return ['m' => 'Male', 'f' => 'Female']; } @@ -63,7 +63,7 @@ class GenderEnum implements EnumInterface { use EnumWithClassAsNameTrait; - public function getChoices() + public function getChoices(): array { return ['m' => 'Male', 'f' => 'Female']; } diff --git a/doc/declaring-translated-enum.md b/doc/declaring-translated-enum.md index 42a4c1c..afcd14f 100644 --- a/doc/declaring-translated-enum.md +++ b/doc/declaring-translated-enum.md @@ -19,7 +19,7 @@ Create a new class, implement both `getName` & `getValues` methods and specify t namespace App\Enum; -use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\Enum\AbstractTranslatedEnum; class GenderEnum extends AbstractTranslatedEnum @@ -29,12 +29,12 @@ class GenderEnum extends AbstractTranslatedEnum parent::__construct($translator, 'enum.gender.%s'); } - public function getName() + public function getName(): string { return 'gender'; } - public function getValues() + public function getValues(): array { return ['m', 'f']; } @@ -64,7 +64,7 @@ Create a new class, use `EnumWithClassAsNameTrait` trait, implement `getValues` namespace App\Enum; -use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\Enum\AbstractTranslatedEnum; use Yokai\Enum\EnumWithClassAsNameTrait; @@ -77,7 +77,7 @@ class GenderEnum extends AbstractTranslatedEnum parent::__construct($translator, 'enum.gender.%s'); } - public function getValues() + public function getValues(): array { return ['m', 'f']; } diff --git a/doc/sonata-admin.md b/doc/sonata-admin.md index 6044a14..701fc62 100644 --- a/doc/sonata-admin.md +++ b/doc/sonata-admin.md @@ -20,7 +20,7 @@ use Yokai\Enum\Bridge\Symfony\Form\Type\EnumType; class MemberAdmin extends AbstractAdmin { - protected function configureListFields(ListMapper $list) + protected function configureListFields(ListMapper $list): void { $list ->add('gender', null, [ @@ -30,7 +30,7 @@ class MemberAdmin extends AbstractAdmin ; } - protected function configureDatagridFilters(DatagridMapper $filter) + protected function configureDatagridFilters(DatagridMapper $filter): void { $filter ->add('gender', 'doctrine_orm_choice', [ @@ -43,7 +43,7 @@ class MemberAdmin extends AbstractAdmin ; } - protected function configureFormFields(FormMapper $form) + protected function configureFormFields(FormMapper $form): void { $form // Let the bundle guess the form type for you (requires that you configured the validation) @@ -52,7 +52,7 @@ class MemberAdmin extends AbstractAdmin ; } - protected function configureShowFields(ShowMapper $form) + protected function configureShowFields(ShowMapper $form): void { $form ->add('gender', null, [ diff --git a/src/AbstractTranslatedEnum.php b/src/AbstractTranslatedEnum.php index ed335e5..f24b388 100644 --- a/src/AbstractTranslatedEnum.php +++ b/src/AbstractTranslatedEnum.php @@ -1,4 +1,4 @@ -getValues(), array_map( - function ($value) { + function (string $value): string { return $this->translator->trans( sprintf($this->transPattern, $value), [], @@ -62,7 +62,7 @@ function ($value) { /** * @param string $transDomain */ - public function setTransDomain($transDomain) + public function setTransDomain(string $transDomain): void { $this->transDomain = $transDomain; } @@ -70,5 +70,5 @@ public function setTransDomain($transDomain) /** * @return array */ - abstract protected function getValues(); + abstract protected function getValues(): array; } diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php b/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php index 43435ca..4825a90 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php @@ -1,4 +1,4 @@ -hasDefinition('enum.registry')) { return; diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php b/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php index 2637170..4aa3b9a 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php @@ -1,4 +1,4 @@ -root('yokai_enum'); diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php b/src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php index beff043..1135590 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php @@ -1,4 +1,4 @@ -processConfiguration($configuration, $configs); diff --git a/src/Bridge/Symfony/Bundle/YokaiEnumBundle.php b/src/Bridge/Symfony/Bundle/YokaiEnumBundle.php index 580f717..f2ee0a4 100644 --- a/src/Bridge/Symfony/Bundle/YokaiEnumBundle.php +++ b/src/Bridge/Symfony/Bundle/YokaiEnumBundle.php @@ -1,10 +1,9 @@ -addCompilerPass(new ConventionedEnumCollectorCompilerPass()) ->addCompilerPass(new TaggedEnumCollectorCompilerPass) ; } @@ -27,7 +25,7 @@ public function build(ContainerBuilder $container) /** * @inheritdoc */ - public function getContainerExtension() + public function getContainerExtension(): EnumExtension { return new EnumExtension; } diff --git a/src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php b/src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php index 963a99b..8ad433b 100644 --- a/src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php +++ b/src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php @@ -1,4 +1,4 @@ -setRequired('enum') ->setDefault('choices_as_values', true) ->setAllowedValues( 'enum', - function ($name) { + function (string $name): bool { return $this->enumRegistry->has($name); } ) ->setDefault( 'choices', - function (Options $options) { + function (Options $options): array { return array_flip($this->enumRegistry->get($options['enum'])->getChoices()); } ) @@ -52,7 +52,7 @@ function (Options $options) { /** * @inheritdoc */ - public function getParent() + public function getParent(): string { if (!method_exists(AbstractType::class, 'getBlockPrefix')) { return 'choice'; //Symfony 2.x support @@ -64,7 +64,7 @@ public function getParent() /** * @inheritdoc */ - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'enum'; } @@ -72,7 +72,7 @@ public function getBlockPrefix() /** * @inheritdoc */ - public function getName() + public function getName(): string { return $this->getBlockPrefix(); } diff --git a/src/Bridge/Symfony/Validator/Constraints/Enum.php b/src/Bridge/Symfony/Validator/Constraints/Enum.php index eb19a85..fb7c334 100644 --- a/src/Bridge/Symfony/Validator/Constraints/Enum.php +++ b/src/Bridge/Symfony/Validator/Constraints/Enum.php @@ -1,4 +1,4 @@ -getChoices($enum); - - if (isset($choices[$value])) { - return $choices[$value]; - } - - return $value; + return $this->getChoices($enum)[$value] ?? $value; } /** @@ -68,7 +62,7 @@ public function getLabel($value, $enum) * * @return array */ - public function getChoices($enum) + public function getChoices(string $enum): array { return $this->registry->get($enum)->getChoices(); } @@ -76,7 +70,7 @@ public function getChoices($enum) /** * @inheritdoc */ - public function getName() + public function getName(): string { return 'enum'; } diff --git a/src/ConfigurableEnum.php b/src/ConfigurableEnum.php index fad4006..9905042 100644 --- a/src/ConfigurableEnum.php +++ b/src/ConfigurableEnum.php @@ -1,4 +1,4 @@ -name = $name; $this->choices = $choices; @@ -30,7 +30,7 @@ public function __construct($name, array $choices) /** * @inheritdoc */ - public function getName() + public function getName(): string { return $this->name; } @@ -38,7 +38,7 @@ public function getName() /** * @inheritdoc */ - public function getChoices() + public function getChoices(): array { return $this->choices; } diff --git a/src/ConfigurableTranslatedEnum.php b/src/ConfigurableTranslatedEnum.php index 621722e..aa6a230 100644 --- a/src/ConfigurableTranslatedEnum.php +++ b/src/ConfigurableTranslatedEnum.php @@ -1,4 +1,4 @@ -name; } @@ -45,7 +45,7 @@ public function getName() /** * @inheritdoc */ - public function getValues() + protected function getValues(): array { return $this->values; } diff --git a/src/EnumInterface.php b/src/EnumInterface.php index 3d92f84..f8b06cb 100644 --- a/src/EnumInterface.php +++ b/src/EnumInterface.php @@ -1,4 +1,4 @@ -has($enum->getName())) { throw DuplicatedEnumException::alreadyRegistered($enum->getName()); @@ -35,7 +35,7 @@ public function add(EnumInterface $enum) * @return EnumInterface * @throws InvalidEnumException */ - public function get($name) + public function get(string $name): EnumInterface { if (!$this->has($name)) { throw InvalidEnumException::nonexistent($name); @@ -49,7 +49,7 @@ public function get($name) * * @return bool */ - public function has($name) + public function has($name): bool { return isset($this->enums[$name]); } @@ -57,7 +57,7 @@ public function has($name) /** * @return EnumInterface[] */ - public function all() + public function all(): array { return $this->enums; } diff --git a/src/EnumWithClassAsNameTrait.php b/src/EnumWithClassAsNameTrait.php index c2e45c6..1576f2d 100644 --- a/src/EnumWithClassAsNameTrait.php +++ b/src/EnumWithClassAsNameTrait.php @@ -1,4 +1,4 @@ -compiler = new TaggedEnumCollectorCompilerPass; } - protected function tearDown() + protected function tearDown(): void { unset($this->compiler); } - public function testCollectWhenServiceNotAvailable() + public function testCollectWhenServiceNotAvailable(): void { $compiler = $this->prophesize('Symfony\Component\DependencyInjection\ContainerBuilder'); $compiler->hasDefinition('enum.registry')->shouldBeCalled()->willReturn(false); @@ -34,7 +34,7 @@ public function testCollectWhenServiceNotAvailable() $this->compiler->process($compiler->reveal()); } - public function testCollectEnums() + public function testCollectEnums(): void { $registry = $this->prophesize('Symfony\Component\DependencyInjection\Definition'); $registry->addMethodCall('add', [new Reference('enum.gender')])->shouldBeCalled(); diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php index 9094e07..729ab72 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php @@ -1,9 +1,8 @@ -extension()->load([[]], $container); @@ -42,7 +38,7 @@ public function it_register_enum_for_autoconfiguration_by_default() /** * @test */ - public function it_do_not_register_enum_for_autoconfiguration_if_asked_to() + public function it_do_not_register_enum_for_autoconfiguration_if_asked_to(): void { $container = new ContainerBuilder(); $this->extension()->load([['enum_autoconfiguration' => false]], $container); @@ -58,7 +54,7 @@ public function it_do_not_register_enum_for_autoconfiguration_if_asked_to() /** * @test */ - public function it_register_enum_registry_alias_for_autowire_by_default() + public function it_register_enum_registry_alias_for_autowire_by_default(): void { $container = new ContainerBuilder(); $this->extension()->load([[]], $container); @@ -68,7 +64,7 @@ public function it_register_enum_registry_alias_for_autowire_by_default() /** * @test */ - public function it_do_not_register_enum_registry_alias_for_autowire_if_asked_to() + public function it_do_not_register_enum_registry_alias_for_autowire_if_asked_to(): void { $container = new ContainerBuilder(); $this->extension()->load([['enum_registry_autoconfigurable' => false]], $container); @@ -79,7 +75,7 @@ public function it_do_not_register_enum_registry_alias_for_autowire_if_asked_to( /** * @test */ - public function it_register_services() + public function it_register_services(): void { $container = new ContainerBuilder(); $this->extension()->load([[]], $container); diff --git a/tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php b/tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php index 04d7c95..ff25ccb 100644 --- a/tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php +++ b/tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php @@ -1,4 +1,4 @@ -enumRegistry = $this->prophesize(EnumRegistry::class); $this->enumRegistry->has('state')->willReturn(false); @@ -64,7 +64,7 @@ protected function setUp() parent::setUp(); } - public function testGuessType() + public function testGuessType(): void { $guess = new TypeGuess( $this->getEnumType(), @@ -78,22 +78,22 @@ public function testGuessType() $this->assertEquals($guess, $this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY)); } - public function testGuessRequired() + public function testGuessRequired(): void { $this->assertNull($this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY)); } - public function testGuessMaxLength() + public function testGuessMaxLength(): void { $this->assertNull($this->guesser->guessMaxLength(self::TEST_CLASS, self::TEST_PROPERTY)); } - public function testGuessPattern() + public function testGuessPattern(): void { $this->assertNull($this->guesser->guessPattern(self::TEST_CLASS, self::TEST_PROPERTY)); } - public function testCreateForm() + public function testCreateForm(): void { $class = self::TEST_CLASS; $form = $this->factory->create($this->getFormType(), new $class, ['data_class' => $class]) @@ -102,7 +102,7 @@ public function testCreateForm() $this->assertEquals(['Male' => 'male', 'Female' => 'female'], $form->get(self::TEST_PROPERTY)->getConfig()->getOption('choices')); } - protected function getFormType() + protected function getFormType(): string { if (method_exists(AbstractType::class, 'getBlockPrefix')) { $name = FormType::class; //Symfony 3.x support @@ -113,7 +113,7 @@ protected function getFormType() return $name; } - protected function getEnumType() + protected function getEnumType(): string { if (method_exists(AbstractType::class, 'getBlockPrefix')) { $name = EnumType::class; //Symfony 3.x support @@ -124,7 +124,7 @@ protected function getEnumType() return $name; } - protected function getExtensions() + protected function getExtensions(): array { return [ new TestExtension($this->enumRegistry->reveal(), $this->metadataFactory->reveal()), diff --git a/tests/Bridge/Symfony/Form/TestExtension.php b/tests/Bridge/Symfony/Form/TestExtension.php index d97642b..ed8e6a2 100644 --- a/tests/Bridge/Symfony/Form/TestExtension.php +++ b/tests/Bridge/Symfony/Form/TestExtension.php @@ -1,4 +1,4 @@ -enumRegistry), @@ -46,7 +46,7 @@ protected function loadTypes() /** * @inheritdoc */ - protected function loadTypeGuesser() + protected function loadTypeGuesser(): ?EnumTypeGuesser { if ($this->metadataFactory === null) { return null; diff --git a/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php b/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php index d993a36..f016f28 100644 --- a/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php +++ b/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php @@ -1,8 +1,9 @@ -enumRegistry = $this->prophesize(EnumRegistry::class); $this->enumRegistry->has('state')->willReturn(false); @@ -26,33 +27,33 @@ protected function setUp() parent::setUp(); } - public function testEnumOptionIsRequired() + public function testEnumOptionIsRequired(): void { $this->expectException('Symfony\Component\OptionsResolver\Exception\MissingOptionsException'); $this->createForm(); } - public function testEnumOptionIsInvalid() + public function testEnumOptionIsInvalid(): void { $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException'); $this->createForm('state'); } - public function testEnumOptionValid() + public function testEnumOptionValid(): void { $form = $this->createForm(GenderEnum::class); $this->assertEquals(['Male' => 'male', 'Female' => 'female'], $form->getConfig()->getOption('choices')); } - protected function getExtensions() + protected function getExtensions(): array { return [ new TestExtension($this->enumRegistry->reveal()) ]; } - private function createForm($enum = null) + private function createForm($enum = null): FormInterface { $options = []; if ($enum) { diff --git a/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php b/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php index 072a692..d220126 100644 --- a/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php +++ b/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php @@ -1,10 +1,10 @@ - */ -class EnumValidatorTest extends AbstractConstraintValidatorTest +class EnumValidatorTest extends ConstraintValidatorTestCase { - protected function createValidator() + protected function createValidator(): EnumValidator { /** @var EnumRegistry|ObjectProphecy $registry */ $registry = $this->prophesize(EnumRegistry::class); @@ -28,39 +28,39 @@ protected function createValidator() return new EnumValidator($registry->reveal()); } - public function testAcceptOnlyEnum() + public function testAcceptOnlyEnum(): void { $this->expectException('Symfony\Component\Validator\Exception\UnexpectedTypeException'); $this->validator->validate(null, new Choice); } - public function testEnumIsRequired() + public function testEnumIsRequired(): void { $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); $this->validator->validate('foo', new Enum); } - public function testValidEnumIsRequired() + public function testValidEnumIsRequired(): void { $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); $this->validator->validate('foo', new Enum('state')); } - public function testNullIsValid() + public function testNullIsValid(): void { $this->validator->validate(null, new Enum('type')); $this->assertNoViolation(); } - public function testValidSingleEnum() + public function testValidSingleEnum(): void { $this->validator->validate('customer', new Enum('type')); $this->assertNoViolation(); } - public function testInvalidSingleEnum() + public function testInvalidSingleEnum(): void { $constraint = new Enum(['enum' => 'type', 'message' => 'myMessage']); @@ -73,7 +73,7 @@ public function testInvalidSingleEnum() ->assertRaised(); } - public function testValidMultipleEnum() + public function testValidMultipleEnum(): void { $constraint = new Enum(['enum' => 'type', 'multiple' => true]); @@ -82,7 +82,7 @@ public function testValidMultipleEnum() $this->assertNoViolation(); } - public function testInvalidMultipleEnum() + public function testInvalidMultipleEnum(): void { $constraint = new Enum(['enum' => 'type', 'multiple' => true, 'multipleMessage' => 'myMessage']); diff --git a/tests/Bridge/Twig/Extension/EnumExtensionTest.php b/tests/Bridge/Twig/Extension/EnumExtensionTest.php index 375052c..e835ced 100644 --- a/tests/Bridge/Twig/Extension/EnumExtensionTest.php +++ b/tests/Bridge/Twig/Extension/EnumExtensionTest.php @@ -1,4 +1,4 @@ -registry = $this->prophesize(EnumRegistry::class); } - protected function tearDown() + protected function tearDown(): void { unset( $this->registry ); } - public function testEnumLabel() + public function testEnumLabel(): void { $enum = $this->prophesize(EnumInterface::class); $enum->getChoices() @@ -60,7 +60,7 @@ public function testEnumLabel() ); } - public function testEnumChoices() + public function testEnumChoices(): void { $enum = $this->prophesize(EnumInterface::class); $enum->getChoices() @@ -80,7 +80,7 @@ public function testEnumChoices() /** * @return \Twig_Environment */ - protected function createEnvironment() + protected function createEnvironment(): \Twig_Environment { $loader = new \Twig_Loader_Array([]); $twig = new \Twig_Environment($loader, ['debug' => true, 'cache' => false, 'autoescape' => false]); @@ -92,7 +92,7 @@ protected function createEnvironment() /** * @return EnumExtension */ - private function createExtension() + private function createExtension(): EnumExtension { return new EnumExtension($this->registry->reveal()); } diff --git a/tests/ConfigurableEnumTest.php b/tests/ConfigurableEnumTest.php index 76334f7..4630464 100644 --- a/tests/ConfigurableEnumTest.php +++ b/tests/ConfigurableEnumTest.php @@ -1,4 +1,4 @@ - 'FOO', 'bar' => 'BAR']); $this->assertSame('foo', $fooEnum->getName()); diff --git a/tests/ConfigurableTranslatedEnumTest.php b/tests/ConfigurableTranslatedEnumTest.php index 0ab8bb8..3d37d67 100644 --- a/tests/ConfigurableTranslatedEnumTest.php +++ b/tests/ConfigurableTranslatedEnumTest.php @@ -1,4 +1,4 @@ -expectException('Yokai\Enum\Exception\InvalidTranslatePatternException'); $translator = $this->prophesize(TranslatorInterface::class); new ConfigurableTranslatedEnum($translator->reveal(), 'invalid.pattern', 'invalid', ['foo', 'bar']); } - public function testTranslatedChoices() + public function testTranslatedChoices(): void { /** @var ObjectProphecy|TranslatorInterface $translator */ $translator = $this->prophesize(TranslatorInterface::class); @@ -34,7 +34,7 @@ public function testTranslatedChoices() $this->assertEquals($expectedChoices, $type->getChoices()); } - public function testTranslatedWithDomainChoices() + public function testTranslatedWithDomainChoices(): void { /** @var ObjectProphecy|TranslatorInterface $translator */ $translator = $this->prophesize(TranslatorInterface::class); diff --git a/tests/EnumRegistryTest.php b/tests/EnumRegistryTest.php index ea82a14..70c6cbf 100644 --- a/tests/EnumRegistryTest.php +++ b/tests/EnumRegistryTest.php @@ -1,4 +1,4 @@ -registry = new EnumRegistry; } - protected function tearDown() + protected function tearDown(): void { unset($this->registry); } - public function testAddDuplicatedException() + public function testAddDuplicatedException(): void { $this->expectException('Yokai\Enum\Exception\DuplicatedEnumException'); $this->registry->add(new GenderEnum); $this->registry->add(new GenderEnum); } - public function testGetInvalidException() + public function testGetInvalidException(): void { $this->expectException('Yokai\Enum\Exception\InvalidEnumException'); $this->registry->add(new GenderEnum); $this->registry->get('type'); } - public function testAddNominal() + public function testAddNominal(): void { $translator = $this->prophesize(TranslatorInterface::class)->reveal(); $gender = new GenderEnum; diff --git a/tests/Fixtures/GenderEnum.php b/tests/Fixtures/GenderEnum.php index edc1ee3..8906e97 100644 --- a/tests/Fixtures/GenderEnum.php +++ b/tests/Fixtures/GenderEnum.php @@ -1,4 +1,4 @@ - 'Male', 'female' => 'Female']; } diff --git a/tests/Fixtures/StateEnum.php b/tests/Fixtures/StateEnum.php index 5115606..4863fe2 100644 --- a/tests/Fixtures/StateEnum.php +++ b/tests/Fixtures/StateEnum.php @@ -1,4 +1,4 @@ -= 4.0 support -if (class_exists('Symfony\Component\Validator\Test\ConstraintValidatorTestCase')) { - class_alias( - 'Symfony\Component\Validator\Test\ConstraintValidatorTestCase', - 'Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest' - ); +$autoloadFile = __DIR__.'/../vendor/autoload.php'; +if (!file_exists($autoloadFile)) { + throw new \RuntimeException('Did not find vendor/autoload.php. Did you run "composer install --dev"?'); } + +require_once $autoloadFile; From a012098ef153bd5a78be4e41e0be038b6e892e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Fri, 31 May 2019 12:40:45 +0200 Subject: [PATCH 04/15] Fixed deprecations and remove compatibility code from lower versions --- .../DependencyInjection/Configuration.php | 4 +-- .../DependencyInjection/EnumExtension.php | 2 +- .../Form/Extension/EnumTypeGuesser.php | 14 +-------- src/Bridge/Symfony/Form/Type/EnumType.php | 13 --------- src/Bridge/Twig/Extension/EnumExtension.php | 22 +++++--------- .../DependencyInjection/EnumExtensionTest.php | 18 ++++-------- .../Form/Extension/EnumTypeGuesserTest.php | 29 ++----------------- tests/Bridge/Symfony/Form/TestExtension.php | 2 +- .../Bridge/Symfony/Form/Type/EnumTypeTest.php | 9 +----- .../Twig/Extension/EnumExtensionTest.php | 10 ++++--- tests/EnumRegistryTest.php | 2 ++ 11 files changed, 29 insertions(+), 96 deletions(-) diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php b/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php index 4aa3b9a..6bad97e 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php @@ -15,8 +15,8 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder(): TreeBuilder { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('yokai_enum'); + $treeBuilder = new TreeBuilder('yokai_enum'); + $rootNode = $treeBuilder->getRootNode(); $rootNode ->children() diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php b/src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php index 1135590..a496f32 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/EnumExtension.php @@ -45,7 +45,7 @@ public function load(array $configs, ContainerBuilder $container): void $xmlLoader->load('twig.xml'); } - if ($config['enum_autoconfiguration'] && method_exists($container, 'registerForAutoconfiguration')) { + if ($config['enum_autoconfiguration']) { $container->registerForAutoconfiguration(EnumInterface::class) ->addTag('enum'); } diff --git a/src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php b/src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php index 8ad433b..8c306c1 100644 --- a/src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php +++ b/src/Bridge/Symfony/Form/Extension/EnumTypeGuesser.php @@ -2,7 +2,6 @@ namespace Yokai\Enum\Bridge\Symfony\Form\Extension; -use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser; use Symfony\Component\Form\Guess\Guess; use Symfony\Component\Form\Guess\TypeGuess; @@ -23,11 +22,6 @@ class EnumTypeGuesser extends ValidatorTypeGuesser */ private $enumRegistry; - /** - * @var string - */ - private $enumFormType; - /** * @param MetadataFactoryInterface $metadataFactory * @param EnumRegistry $enumRegistry @@ -37,12 +31,6 @@ public function __construct(MetadataFactoryInterface $metadataFactory, EnumRegis parent::__construct($metadataFactory); $this->enumRegistry = $enumRegistry; - - if (method_exists(AbstractType::class, 'getBlockPrefix')) { - $this->enumFormType = EnumType::class; //Symfony 3.x support - } else { - $this->enumFormType = 'enum'; //Symfony 2.x support - } } /** @@ -55,7 +43,7 @@ public function guessTypeForConstraint(Constraint $constraint): ?TypeGuess } return new TypeGuess( - $this->enumFormType, + EnumType::class, [ 'enum' => $constraint->enum, 'multiple' => $constraint->multiple, diff --git a/src/Bridge/Symfony/Form/Type/EnumType.php b/src/Bridge/Symfony/Form/Type/EnumType.php index 1c01945..b20f003 100644 --- a/src/Bridge/Symfony/Form/Type/EnumType.php +++ b/src/Bridge/Symfony/Form/Type/EnumType.php @@ -33,7 +33,6 @@ public function configureOptions(OptionsResolver $resolver): void { $resolver ->setRequired('enum') - ->setDefault('choices_as_values', true) ->setAllowedValues( 'enum', function (string $name): bool { @@ -54,10 +53,6 @@ function (Options $options): array { */ public function getParent(): string { - if (!method_exists(AbstractType::class, 'getBlockPrefix')) { - return 'choice'; //Symfony 2.x support - } - return ChoiceType::class; } @@ -68,12 +63,4 @@ public function getBlockPrefix(): string { return 'enum'; } - - /** - * @inheritdoc - */ - public function getName(): string - { - return $this->getBlockPrefix(); - } } diff --git a/src/Bridge/Twig/Extension/EnumExtension.php b/src/Bridge/Twig/Extension/EnumExtension.php index ac8d302..0840547 100644 --- a/src/Bridge/Twig/Extension/EnumExtension.php +++ b/src/Bridge/Twig/Extension/EnumExtension.php @@ -2,15 +2,15 @@ namespace Yokai\Enum\Bridge\Twig\Extension; -use Twig_Extension; -use Twig_SimpleFilter; -use Twig_SimpleFunction; +use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; +use Twig\TwigFunction; use Yokai\Enum\EnumRegistry; /** * @author Yann Eugoné */ -class EnumExtension extends Twig_Extension +class EnumExtension extends AbstractExtension { /** * @var EnumRegistry @@ -31,8 +31,8 @@ public function __construct(EnumRegistry $registry) public function getFunctions(): array { return [ - new Twig_SimpleFunction('enum_label', [$this, 'getLabel']), - new Twig_SimpleFunction('enum_choices', [$this, 'getChoices']), + new TwigFunction('enum_label', [$this, 'getLabel']), + new TwigFunction('enum_choices', [$this, 'getChoices']), ]; } @@ -42,7 +42,7 @@ public function getFunctions(): array public function getFilters(): array { return [ - new Twig_SimpleFilter('enum_label', [$this, 'getLabel']), + new TwigFilter('enum_label', [$this, 'getLabel']), ]; } @@ -66,12 +66,4 @@ public function getChoices(string $enum): array { return $this->registry->get($enum)->getChoices(); } - - /** - * @inheritdoc - */ - public function getName(): string - { - return 'enum'; - } } diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php index 729ab72..8b64892 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php @@ -26,13 +26,9 @@ public function it_register_enum_for_autoconfiguration_by_default(): void $container = new ContainerBuilder(); $this->extension()->load([[]], $container); - if (method_exists($container, 'getAutoconfiguredInstanceof')) { - $autoconfigure = $container->getAutoconfiguredInstanceof(); - $this->assertArrayHasKey(EnumInterface::class, $autoconfigure); - $this->assertEquals(['enum' => [[]]], $autoconfigure[EnumInterface::class]->getTags()); - } else { - $this->assertTrue(true); // not in this version - } + $autoconfigure = $container->getAutoconfiguredInstanceof(); + $this->assertArrayHasKey(EnumInterface::class, $autoconfigure); + $this->assertEquals(['enum' => [[]]], $autoconfigure[EnumInterface::class]->getTags()); } /** @@ -43,12 +39,8 @@ public function it_do_not_register_enum_for_autoconfiguration_if_asked_to(): voi $container = new ContainerBuilder(); $this->extension()->load([['enum_autoconfiguration' => false]], $container); - if (method_exists($container, 'getAutoconfiguredInstanceof')) { - $autoconfigure = $container->getAutoconfiguredInstanceof(); - $this->assertArrayNotHasKey(EnumInterface::class, $autoconfigure); - } else { - $this->assertTrue(true); // not in this version - } + $autoconfigure = $container->getAutoconfiguredInstanceof(); + $this->assertArrayNotHasKey(EnumInterface::class, $autoconfigure); } /** diff --git a/tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php b/tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php index ff25ccb..0491fba 100644 --- a/tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php +++ b/tests/Bridge/Symfony/Form/Extension/EnumTypeGuesserTest.php @@ -3,15 +3,14 @@ namespace Yokai\Enum\Tests\Bridge\Symfony\Form\Extension; use Prophecy\Prophecy\ObjectProphecy; -use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Guess\Guess; use Symfony\Component\Form\Guess\TypeGuess; use Symfony\Component\Form\Test\TypeTestCase; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; -use Yokai\Enum\Bridge\Symfony\Form\Type\EnumType; use Yokai\Enum\Bridge\Symfony\Form\Extension\EnumTypeGuesser; +use Yokai\Enum\Bridge\Symfony\Form\Type\EnumType; use Yokai\Enum\Bridge\Symfony\Validator\Constraints\Enum; use Yokai\Enum\EnumRegistry; use Yokai\Enum\Tests\Bridge\Symfony\Form\TestExtension; @@ -67,7 +66,7 @@ protected function setUp(): void public function testGuessType(): void { $guess = new TypeGuess( - $this->getEnumType(), + EnumType::class, [ 'enum' => GenderEnum::class, 'multiple' => false, @@ -96,34 +95,12 @@ public function testGuessPattern(): void public function testCreateForm(): void { $class = self::TEST_CLASS; - $form = $this->factory->create($this->getFormType(), new $class, ['data_class' => $class]) + $form = $this->factory->create(FormType::class, new $class, ['data_class' => $class]) ->add(self::TEST_PROPERTY); $this->assertEquals(['Male' => 'male', 'Female' => 'female'], $form->get(self::TEST_PROPERTY)->getConfig()->getOption('choices')); } - protected function getFormType(): string - { - if (method_exists(AbstractType::class, 'getBlockPrefix')) { - $name = FormType::class; //Symfony 3.x support - } else { - $name = 'form'; //Symfony 2.x support - } - - return $name; - } - - protected function getEnumType(): string - { - if (method_exists(AbstractType::class, 'getBlockPrefix')) { - $name = EnumType::class; //Symfony 3.x support - } else { - $name = 'enum'; //Symfony 2.x support - } - - return $name; - } - protected function getExtensions(): array { return [ diff --git a/tests/Bridge/Symfony/Form/TestExtension.php b/tests/Bridge/Symfony/Form/TestExtension.php index ed8e6a2..dc2e278 100644 --- a/tests/Bridge/Symfony/Form/TestExtension.php +++ b/tests/Bridge/Symfony/Form/TestExtension.php @@ -4,8 +4,8 @@ use Symfony\Component\Form\AbstractExtension; use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; -use Yokai\Enum\Bridge\Symfony\Form\Type\EnumType; use Yokai\Enum\Bridge\Symfony\Form\Extension\EnumTypeGuesser; +use Yokai\Enum\Bridge\Symfony\Form\Type\EnumType; use Yokai\Enum\EnumRegistry; /** diff --git a/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php b/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php index f016f28..2a089fe 100644 --- a/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php +++ b/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php @@ -2,7 +2,6 @@ namespace Yokai\Enum\Tests\Bridge\Symfony\Form\Type; -use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\Test\TypeTestCase; use Yokai\Enum\Bridge\Symfony\Form\Type\EnumType; @@ -60,12 +59,6 @@ private function createForm($enum = null): FormInterface $options['enum'] = $enum; } - if (method_exists(AbstractType::class, 'getBlockPrefix')) { - $name = EnumType::class; //Symfony 3.x support - } else { - $name = 'enum'; //Symfony 2.x support - } - - return $this->factory->create($name, null, $options); + return $this->factory->create(EnumType::class, null, $options); } } diff --git a/tests/Bridge/Twig/Extension/EnumExtensionTest.php b/tests/Bridge/Twig/Extension/EnumExtensionTest.php index e835ced..a2a3ff5 100644 --- a/tests/Bridge/Twig/Extension/EnumExtensionTest.php +++ b/tests/Bridge/Twig/Extension/EnumExtensionTest.php @@ -4,6 +4,8 @@ use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; +use Twig\Environment; +use Twig\Loader\ArrayLoader; use Yokai\Enum\Bridge\Twig\Extension\EnumExtension; use Yokai\Enum\EnumInterface; use Yokai\Enum\EnumRegistry; @@ -78,12 +80,12 @@ public function testEnumChoices(): void } /** - * @return \Twig_Environment + * @return Environment */ - protected function createEnvironment(): \Twig_Environment + protected function createEnvironment(): Environment { - $loader = new \Twig_Loader_Array([]); - $twig = new \Twig_Environment($loader, ['debug' => true, 'cache' => false, 'autoescape' => false]); + $loader = new ArrayLoader([]); + $twig = new Environment($loader, ['debug' => true, 'cache' => false, 'autoescape' => false]); $twig->addExtension($this->createExtension()); return $twig; diff --git a/tests/EnumRegistryTest.php b/tests/EnumRegistryTest.php index 70c6cbf..70fba77 100644 --- a/tests/EnumRegistryTest.php +++ b/tests/EnumRegistryTest.php @@ -3,6 +3,7 @@ namespace Yokai\Enum\Tests; use PHPUnit\Framework\TestCase; +use Prophecy\Prophecy\ObjectProphecy; use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\Enum\EnumRegistry; use Yokai\Enum\Tests\Fixtures\GenderEnum; @@ -46,6 +47,7 @@ public function testGetInvalidException(): void public function testAddNominal(): void { + /** @var TranslatorInterface|ObjectProphecy $translator */ $translator = $this->prophesize(TranslatorInterface::class)->reveal(); $gender = new GenderEnum; $state = new StateEnum($translator); From e6129ca88f15df90c59729980a38043a05fa6e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Fri, 31 May 2019 17:04:48 +0200 Subject: [PATCH 05/15] Added configurable enums with constant extraction capabilities --- doc/declaring-enum.md | 19 +++ doc/declaring-translated-enum.md | 22 +++- .../Symfony/Bundle/Resources/config/enum.xml | 1 + src/ConstantExtractor.php | 76 +++++++++++ src/ConstantListEnum.php | 15 +++ src/ConstantListTranslatedEnum.php | 21 +++ .../CannotExtractConstantsException.php | 30 +++++ tests/ConstantExtractorTest.php | 121 ++++++++++++++++++ tests/ConstantListEnumTest.php | 40 ++++++ tests/ConstantListTranslatedEnumTest.php | 71 ++++++++++ tests/Fixtures/Vehicle.php | 17 +++ 11 files changed, 432 insertions(+), 1 deletion(-) create mode 100644 src/ConstantExtractor.php create mode 100644 src/ConstantListEnum.php create mode 100644 src/ConstantListTranslatedEnum.php create mode 100644 src/Exception/CannotExtractConstantsException.php create mode 100644 tests/ConstantExtractorTest.php create mode 100644 tests/ConstantListEnumTest.php create mode 100644 tests/ConstantListTranslatedEnumTest.php create mode 100644 tests/Fixtures/Vehicle.php diff --git a/doc/declaring-enum.md b/doc/declaring-enum.md index b6d77cc..1793d09 100644 --- a/doc/declaring-enum.md +++ b/doc/declaring-enum.md @@ -97,3 +97,22 @@ services: - m: 'Male' f: 'Female' ``` + + +The configurable way extracting constant list +-------------------- + +Let say that you already have a list of constant that for the gender. +No need for a class, just use the `ConstantListEnum` class and define a new enum service. + +```yaml +services: + enum.member.gender: + class: 'Yokai\Enum\ConstantListEnum' + public: false + tags: ['enum'] + arguments: + - '@enum.constant_extractor' + - 'App\\Model\\Person::GENDER_*' + - "gender" +``` diff --git a/doc/declaring-translated-enum.md b/doc/declaring-translated-enum.md index afcd14f..e78f9e1 100644 --- a/doc/declaring-translated-enum.md +++ b/doc/declaring-translated-enum.md @@ -6,7 +6,7 @@ If you wish to use Symfony's Translator for your enum labels, this bundle provid There is a lot of way to create and declare such classes. > **Note :** It is pretty much like [declaring classic enums](declaring-enum.md), -> but with a `extends` instead of an `implements`. +> but with a dependency to `symfony/translator`. The classic way @@ -114,3 +114,23 @@ services: - "gender" - ['m', 'f'] ``` + +The configurable way extracting constant list +-------------------- + +Let say that you already have a list of constant that for the gender. +No need for a class, just use the `ConstantListTranslatedEnum` class and define a new enum service. + +```yaml +services: + enum.member.gender: + class: 'Yokai\Enum\ConstantListTranslatedEnum' + public: false + tags: ['enum'] + arguments: + - '@enum.constant_extractor' + - 'App\\Model\\Person::GENDER_*' + - "@translator" + - "enum.gender.%s" + - "gender" +``` diff --git a/src/Bridge/Symfony/Bundle/Resources/config/enum.xml b/src/Bridge/Symfony/Bundle/Resources/config/enum.xml index b0e190e..737352a 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/enum.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/enum.xml @@ -5,6 +5,7 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + diff --git a/src/ConstantExtractor.php b/src/ConstantExtractor.php new file mode 100644 index 0000000..860c890 --- /dev/null +++ b/src/ConstantExtractor.php @@ -0,0 +1,76 @@ +explode($pattern); + + return $this->filter( + $this->publicConstants($class), + $patternRegex, + $pattern + ); + } + + private function filter(array $constants, string $regexp, string $pattern): array + { + $matchingNames = preg_grep($regexp, array_keys($constants)); + + if (count($matchingNames) === 0) { + throw CannotExtractConstantsException::noConstantMathingPattern($pattern); + } + + return array_values(array_intersect_key($constants, array_flip($matchingNames))); + } + + private function publicConstants(string $class): array + { + try { + $constants = (new ReflectionClass($class))->getReflectionConstants(); + } catch (ReflectionException $exception) { + throw CannotExtractConstantsException::classDoNotExists($class); + } + + $list = []; + foreach ($constants as $constant) { + if (!$constant->isPublic()) { + continue; + } + + $list[$constant->getName()] = $constant->getValue(); + } + + if (count($list) === 0) { + throw CannotExtractConstantsException::classHasNoPublicConstant($class); + } + + return $list; + } + + private function explode(string $pattern): array + { + if (substr_count($pattern, '::') !== 1) { + throw CannotExtractConstantsException::invalidPattern($pattern); + } + + [$class, $constantsNamePattern] = explode('::', $pattern); + + if (substr_count($constantsNamePattern, '*') === 0) { + throw CannotExtractConstantsException::invalidPattern($pattern); + } + + $constantsNameRegexp = sprintf( + '#^%s$#', + str_replace('*', '[0-9a-zA-Z_]+', $constantsNamePattern) + ); + + return [$class, $constantsNameRegexp]; + } +} diff --git a/src/ConstantListEnum.php b/src/ConstantListEnum.php new file mode 100644 index 0000000..b7ca310 --- /dev/null +++ b/src/ConstantListEnum.php @@ -0,0 +1,15 @@ +extract($constantsPattern); + parent::__construct($name, array_combine($values, $values)); + } +} diff --git a/src/ConstantListTranslatedEnum.php b/src/ConstantListTranslatedEnum.php new file mode 100644 index 0000000..a4d1baf --- /dev/null +++ b/src/ConstantListTranslatedEnum.php @@ -0,0 +1,21 @@ +extract($constantsPattern)); + } +} diff --git a/src/Exception/CannotExtractConstantsException.php b/src/Exception/CannotExtractConstantsException.php new file mode 100644 index 0000000..f90e00c --- /dev/null +++ b/src/Exception/CannotExtractConstantsException.php @@ -0,0 +1,30 @@ +expectException(CannotExtractConstantsException::class); + $this->expectExceptionMessageRegExp($exceptionMessage); + + $this->getExtractor()->extract($pattern); + } + + /** + * @dataProvider empty + */ + public function testExtractEmpty(string $pattern, string $exceptionMessage): void + { + $this->expectException(CannotExtractConstantsException::class); + $this->expectExceptionMessageRegExp($exceptionMessage); + + $this->getExtractor()->extract($pattern); + } + + /** + * @dataProvider successful + */ + public function testExtractSuccessful(string $pattern, array $expectedList): void + { + self::assertEquals($expectedList, $this->getExtractor()->extract($pattern)); + } + + public function empty(): Generator + { + yield 'class without constant' => [ + ClassWithoutConstant::class.'::*', + '/Class .+ has no public constant/', + ]; + yield 'class without public constant' => [ + ClassWithNoPublicConstant::class.'::*', + '/Class .+ has no public constant/', + ]; + yield 'class with constant but no match' => [ + ClassWithConstant::class.'::NO_MATCH*', + '/Pattern .+ matches no constant/', + ]; + } + + public function malformed(): Generator + { + $invalidPatternRegexp = '/Constant extraction pattern must look like Fully\\\\Qualified\\\\ClassName::CONSTANT_\*\..+/'; + yield 'no class no constant pattern' => [ + 'not a pattern', + $invalidPatternRegexp, + ]; + yield 'class that do not exists' => [ + 'SomeClassThatDoNotExists::STATUS_*', + '/Class SomeClassThatDoNotExists do not exists\./', + ]; + yield 'missing constant pattern and separator' => [ + ClassWithConstant::class, + $invalidPatternRegexp, + ]; + yield 'missing constant pattern' => [ + ClassWithConstant::class.'::', + $invalidPatternRegexp, + ]; + yield 'two separator' => [ + ClassWithConstant::class.'::STATUS_*::', + $invalidPatternRegexp, + ]; + yield 'no * in pattern' => [ + ClassWithConstant::class.'::STATUS_ONLINE', + $invalidPatternRegexp, + ]; + } + + public function successful(): Generator + { + yield 'starting with status' => [ + ClassWithConstant::class.'::STATUS_*', + [ClassWithConstant::STATUS_ONLINE, ClassWithConstant::STATUS_DRAFT], + ]; + yield 'ending with online' => [ + ClassWithConstant::class.'::*_ONLINE', + [ClassWithConstant::STATUS_ONLINE], + ]; + } +} + +class ClassWithoutConstant +{ +} + +class ClassWithNoPublicConstant +{ + private const PROTECTED_CONST = 'protected'; + private const PRIVATE_CONST = 'private'; +} + +class ClassWithConstant +{ + public const STATUS_ONLINE = 'online'; + public const STATUS_DRAFT = 'draft'; + protected const STATUS_PROTECTED = 'protected'; + private const STATUS_PRIVATE = 'private'; +} diff --git a/tests/ConstantListEnumTest.php b/tests/ConstantListEnumTest.php new file mode 100644 index 0000000..09b7402 --- /dev/null +++ b/tests/ConstantListEnumTest.php @@ -0,0 +1,40 @@ +getEnum(Vehicle::class.'::TYPE_*', 'vehicle.type'); + self::assertSame('vehicle.type', $type->getName()); + self::assertSame( + ['bike' => 'bike', 'car' => 'car', 'bus' => 'bus'], + $type->getChoices() + ); + + $engine = $this->getEnum(Vehicle::class.'::ENGINE_*', 'vehicle.engine'); + self::assertSame('vehicle.engine', $engine->getName()); + self::assertSame( + ['electic' => 'electic', 'combustion' => 'combustion'], + $engine->getChoices() + ); + + $brand = $this->getEnum(Vehicle::class.'::BRAND_*', 'vehicle.brand'); + self::assertSame('vehicle.brand', $brand->getName()); + self::assertSame( + ['renault' => 'renault', 'volkswagen' => 'volkswagen', 'toyota' => 'toyota'], + $brand->getChoices() + ); + } +} diff --git a/tests/ConstantListTranslatedEnumTest.php b/tests/ConstantListTranslatedEnumTest.php new file mode 100644 index 0000000..a0663bc --- /dev/null +++ b/tests/ConstantListTranslatedEnumTest.php @@ -0,0 +1,71 @@ +translator = $this->prophesize(TranslatorInterface::class); + } + + protected function tearDown(): void + { + $this->translator = null; + } + + public function getEnum(string $pattern, string $name): ConstantListTranslatedEnum + { + return new ConstantListTranslatedEnum( + new ConstantExtractor(), + $pattern, + $this->translator->reveal(), + $name.'.%s', + $name + ); + } + + 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'); + self::assertSame('vehicle.type', $type->getName()); + self::assertSame( + ['bike' => 'Moto', 'car' => 'Voiture', 'bus' => 'Bus'], + $type->getChoices() + ); + + $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'); + self::assertSame('vehicle.engine', $engine->getName()); + self::assertSame( + ['electic' => 'Electrique', 'combustion' => 'Combustion'], + $engine->getChoices() + ); + + $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'); + self::assertSame('vehicle.brand', $brand->getName()); + self::assertSame( + ['renault' => 'Renault', 'volkswagen' => 'Volkswagen', 'toyota' => 'Toyota'], + $brand->getChoices() + ); + } +} diff --git a/tests/Fixtures/Vehicle.php b/tests/Fixtures/Vehicle.php new file mode 100644 index 0000000..ef8d2ef --- /dev/null +++ b/tests/Fixtures/Vehicle.php @@ -0,0 +1,17 @@ + Date: Mon, 3 Jun 2019 09:27:15 +0200 Subject: [PATCH 06/15] Fixed dependencies and compatibility with symfony versions --- .travis.yml | 5 +++- composer.json | 10 ++++---- .../Constraints/EnumValidatorTest.php | 25 +++++++++++++------ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index 53bbdc5..258161c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,10 @@ cache: before_install: - composer selfupdate - - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi; + - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/form:${SYMFONY_VERSION}" --no-update; fi; + - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/framework-bundle:${SYMFONY_VERSION}" --no-update; fi; + - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/twig-bundle:${SYMFONY_VERSION}" --no-update; fi; + - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/validator:${SYMFONY_VERSION}" --no-update; fi; install: composer update --prefer-dist --no-interaction $COMPOSER_FLAGS diff --git a/composer.json b/composer.json index 0f81a13..9529cc6 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,11 @@ "require-dev": { "doctrine/annotations": "~1.3", "phpunit/phpunit": "~7.0", - "symfony/framework-bundle": "^4.1", - "symfony/form": "^4.1", - "symfony/validator": "^4.1", - "twig/twig": "^2.0", - "symfony/twig-bundle": "^4.1" + "symfony/form": "^4.2", + "symfony/framework-bundle": "^4.2", + "symfony/twig-bundle": "^4.2", + "symfony/validator": "^4.2", + "twig/twig": "^2.0" }, "suggest": { "symfony/framework-bundle": "Integrate with Symfony Framework", diff --git a/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php b/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php index d220126..b475b86 100644 --- a/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php +++ b/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php @@ -3,6 +3,7 @@ namespace Yokai\Enum\Tests\Bridge\Symfony\Validator\Constraints; use Prophecy\Prophecy\ObjectProphecy; +use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Validator\Constraints\Choice; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; use Yokai\Enum\Bridge\Symfony\Validator\Constraints\Enum; @@ -66,11 +67,15 @@ public function testInvalidSingleEnum(): void $this->validator->validate('foo', $constraint); - $this->buildViolation('myMessage') + $violationAssertion = $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"foo"') - ->setParameter('{{ choices }}', '"customer", "prospect"') - ->setCode(Choice::NO_SUCH_CHOICE_ERROR) - ->assertRaised(); + ->setCode(Choice::NO_SUCH_CHOICE_ERROR); + + if (version_compare(Kernel::VERSION, '4.3', '>=')) { + $violationAssertion->setParameter('{{ choices }}', '"customer", "prospect"'); + } + + $violationAssertion->assertRaised(); } public function testValidMultipleEnum(): void @@ -88,11 +93,15 @@ public function testInvalidMultipleEnum(): void $this->validator->validate(['customer', 'foo'], $constraint); - $this->buildViolation('myMessage') + $violationAssertion = $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"foo"') - ->setParameter('{{ choices }}', '"customer", "prospect"') ->setInvalidValue('foo') - ->setCode(Choice::NO_SUCH_CHOICE_ERROR) - ->assertRaised(); + ->setCode(Choice::NO_SUCH_CHOICE_ERROR); + + if (version_compare(Kernel::VERSION, '4.3', '>=')) { + $violationAssertion->setParameter('{{ choices }}', '"customer", "prospect"'); + } + + $violationAssertion->assertRaised(); } } From 4a7e5605e38b4a4fbc66b3287cc13d403583fd4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Mon, 3 Jun 2019 09:38:19 +0200 Subject: [PATCH 07/15] Fixed travis composer dependencies installation --- .travis.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 258161c..53bbdc5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,10 +17,7 @@ cache: before_install: - composer selfupdate - - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/form:${SYMFONY_VERSION}" --no-update; fi; - - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/framework-bundle:${SYMFONY_VERSION}" --no-update; fi; - - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/twig-bundle:${SYMFONY_VERSION}" --no-update; fi; - - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/validator:${SYMFONY_VERSION}" --no-update; fi; + - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi; install: composer update --prefer-dist --no-interaction $COMPOSER_FLAGS From b0bd659abbd8014f6fc4541a73536a7fed20422f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Thu, 25 Jun 2020 22:09:34 +0200 Subject: [PATCH 08/15] Revert library package type initiative --- README.md | 21 +++++++-------- composer.json | 6 ++--- doc/declaring-enum.md | 10 +++---- doc/declaring-translated-enum.md | 12 ++++----- doc/sonata-admin.md | 2 +- src/AbstractTranslatedEnum.php | 4 +-- src/ConfigurableEnum.php | 2 +- src/ConfigurableTranslatedEnum.php | 3 +-- src/ConstantExtractor.php | 4 +-- src/ConstantListEnum.php | 2 +- src/ConstantListTranslatedEnum.php | 2 +- .../TaggedEnumCollectorCompilerPass.php | 2 +- .../DependencyInjection/Configuration.php | 2 +- .../DependencyInjection/EnumExtension.php | 6 ++--- src/EnumInterface.php | 2 +- src/EnumRegistry.php | 6 ++--- src/EnumWithClassAsNameTrait.php | 2 +- .../CannotExtractConstantsException.php | 4 +-- src/Exception/DuplicatedEnumException.php | 2 +- src/Exception/InvalidEnumException.php | 2 +- .../InvalidTranslatePatternException.php | 2 +- .../Form/Extension/EnumTypeGuesser.php | 8 +++--- .../Symfony => }/Form/Type/EnumType.php | 4 +-- .../Bundle => }/Resources/config/enum.xml | 6 ++--- .../Bundle => }/Resources/config/form.xml | 9 ++++--- .../Bundle => }/Resources/config/twig.xml | 4 +-- .../Resources/config/validator.xml | 4 +-- .../Twig/Extension/EnumExtension.php | 4 +-- .../Validator/Constraints/Enum.php | 2 +- .../Validator/Constraints/EnumValidator.php | 4 +-- .../Symfony/Bundle => }/YokaiEnumBundle.php | 6 ++--- tests/ConfigurableEnumTest.php | 4 +-- tests/ConfigurableTranslatedEnumTest.php | 7 ++--- tests/ConstantExtractorTest.php | 6 ++--- tests/ConstantListEnumTest.php | 8 +++--- tests/ConstantListTranslatedEnumTest.php | 8 +++--- .../TaggedEnumCollectorCompilerPassTest.php | 4 +-- .../DependencyInjection/EnumExtensionTest.php | 15 +++++++---- tests/EnumRegistryTest.php | 18 +++++++------ tests/Fixtures/GenderEnum.php | 6 ++--- tests/Fixtures/StateEnum.php | 4 +-- tests/Fixtures/SubscriptionEnum.php | 4 +-- tests/Fixtures/TypeEnum.php | 4 +-- tests/Fixtures/Vehicle.php | 2 +- .../Form/Extension/EnumTypeGuesserTest.php | 27 +++++++++++-------- .../Symfony => }/Form/TestExtension.php | 8 +++--- .../Symfony => }/Form/Type/EnumTypeTest.php | 10 +++---- .../Twig/Extension/EnumExtensionTest.php | 8 +++--- .../Constraints/EnumValidatorTest.php | 12 ++++----- 49 files changed, 157 insertions(+), 147 deletions(-) rename src/{Bridge/Symfony/Bundle => }/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php (90%) rename src/{Bridge/Symfony/Bundle => }/DependencyInjection/Configuration.php (94%) rename src/{Bridge/Symfony/Bundle => }/DependencyInjection/EnumExtension.php (93%) rename src/{Bridge/Symfony => }/Form/Extension/EnumTypeGuesser.php (90%) rename src/{Bridge/Symfony => }/Form/Type/EnumType.php (94%) rename src/{Bridge/Symfony/Bundle => }/Resources/config/enum.xml (55%) rename src/{Bridge/Symfony/Bundle => }/Resources/config/form.xml (58%) rename src/{Bridge/Symfony/Bundle => }/Resources/config/twig.xml (67%) rename src/{Bridge/Symfony/Bundle => }/Resources/config/validator.xml (67%) rename src/{Bridge => }/Twig/Extension/EnumExtension.php (94%) rename src/{Bridge/Symfony => }/Validator/Constraints/Enum.php (89%) rename src/{Bridge/Symfony => }/Validator/Constraints/EnumValidator.php (94%) rename src/{Bridge/Symfony/Bundle => }/YokaiEnumBundle.php (72%) rename tests/{Bridge/Symfony/Bundle => }/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php (90%) rename tests/{Bridge/Symfony/Bundle => }/DependencyInjection/EnumExtensionTest.php (83%) rename tests/{Bridge/Symfony => }/Form/Extension/EnumTypeGuesserTest.php (79%) rename tests/{Bridge/Symfony => }/Form/TestExtension.php (86%) rename tests/{Bridge/Symfony => }/Form/Type/EnumTypeTest.php (87%) rename tests/{Bridge => }/Twig/Extension/EnumExtensionTest.php (93%) rename tests/{Bridge/Symfony => }/Validator/Constraints/EnumValidatorTest.php (91%) diff --git a/README.md b/README.md index ec65af2..746cb77 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ $ composer require yokai/enum-bundle return [ // ... - Yokai\Enum\Bridge\Symfony\Bundle\YokaiEnumBundle::class => ['all' => true], + Yokai\EnumBundle\YokaiEnumBundle::class => ['all' => true], ]; ``` @@ -54,14 +54,14 @@ We first need to create the classes that will handle our enums : // src/Enum/GenderEnum.php namespace App\Enum; -use Yokai\Enum\EnumInterface; -use Yokai\Enum\EnumWithClassAsNameTrait; +use Yokai\EnumBundle\EnumInterface; +use Yokai\EnumBundle\EnumWithClassAsNameTrait; class GenderEnum implements EnumInterface { use EnumWithClassAsNameTrait; - public function getChoices() + public function getChoices(): array { return ['m' => 'Male', 'f' => 'Female']; } @@ -80,14 +80,15 @@ Adding validation to your model : // src/Model/Member.php namespace App\Model; -use Yokai\Enum\Bridge\Symfony\Validator\Constraints\Enum; +use App\Enum\GenderEnum; +use Yokai\EnumBundle\Validator\Constraints\Enum; class Member { /** * @var string * - * @Enum("App\Enum\GenderEnum") + * @Enum(GenderEnum::class) */ protected $gender; } @@ -103,8 +104,7 @@ namespace App\Form\Type; use App\Enum\GenderEnum; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; -// For Symfony >= 2.8 -use Yokai\Enum\Bridge\Symfony\Form\Type\EnumType; +use Yokai\EnumBundle\Form\Type\EnumType; class MemberType extends AbstractType { @@ -114,11 +114,8 @@ class MemberType extends AbstractType // Let the bundle guess the form type for you (requires that you configured the validation) ->add('gender') - // Manual form type binding for Symfony >= 2.8 + // Manual form type binding ->add('gender', EnumType::class, ['enum' => GenderEnum::class]) - - // Manual form type binding for Symfony 2.7 - ->add('gender', 'enum', ['enum' => GenderEnum::class]) ; } } diff --git a/composer.json b/composer.json index 9529cc6..2f3dd67 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "yokai/enum-bundle", "description": "Simple enumeration system with Symfony integration", - "type": "library", + "type": "symfony-bundle", "license": "MIT", "authors": [ { @@ -28,10 +28,10 @@ "twig/twig": "Add enum util functions" }, "autoload": { - "psr-4": { "Yokai\\Enum\\": "src" } + "psr-4": { "Yokai\\EnumBundle\\": "src" } }, "autoload-dev": { - "psr-4": { "Yokai\\Enum\\Tests\\": "tests" } + "psr-4": { "Yokai\\EnumBundle\\Tests\\": "tests" } }, "minimum-stability": "stable", "extra": { diff --git a/doc/declaring-enum.md b/doc/declaring-enum.md index 1793d09..d1bd3f4 100644 --- a/doc/declaring-enum.md +++ b/doc/declaring-enum.md @@ -19,7 +19,7 @@ Create a new class, implement both `getName` & `getChoices` methods. namespace App\Enum; -use Yokai\Enum\EnumInterface; +use Yokai\EnumBundle\EnumInterface; class GenderEnum implements EnumInterface { @@ -56,8 +56,8 @@ Create a new class, use `EnumWithClassAsNameTrait` trait and implement `getChoic namespace App\Enum; -use Yokai\Enum\EnumInterface; -use Yokai\Enum\EnumWithClassAsNameTrait; +use Yokai\EnumBundle\EnumInterface; +use Yokai\EnumBundle\EnumWithClassAsNameTrait; class GenderEnum implements EnumInterface { @@ -89,7 +89,7 @@ No need for a class, just use the `ConfigurableEnum` class and define a new enum ```yaml services: enum.member.gender: - class: 'Yokai\Enum\ConfigurableEnum' + class: 'Yokai\EnumBundle\ConfigurableEnum' public: false tags: ['enum'] arguments: @@ -108,7 +108,7 @@ No need for a class, just use the `ConstantListEnum` class and define a new enum ```yaml services: enum.member.gender: - class: 'Yokai\Enum\ConstantListEnum' + class: 'Yokai\EnumBundle\ConstantListEnum' public: false tags: ['enum'] arguments: diff --git a/doc/declaring-translated-enum.md b/doc/declaring-translated-enum.md index e78f9e1..8b5cab7 100644 --- a/doc/declaring-translated-enum.md +++ b/doc/declaring-translated-enum.md @@ -20,7 +20,7 @@ Create a new class, implement both `getName` & `getValues` methods and specify t namespace App\Enum; use Symfony\Contracts\Translation\TranslatorInterface; -use Yokai\Enum\AbstractTranslatedEnum; +use Yokai\EnumBundle\AbstractTranslatedEnum; class GenderEnum extends AbstractTranslatedEnum { @@ -65,8 +65,8 @@ Create a new class, use `EnumWithClassAsNameTrait` trait, implement `getValues` namespace App\Enum; use Symfony\Contracts\Translation\TranslatorInterface; -use Yokai\Enum\AbstractTranslatedEnum; -use Yokai\Enum\EnumWithClassAsNameTrait; +use Yokai\EnumBundle\AbstractTranslatedEnum; +use Yokai\EnumBundle\EnumWithClassAsNameTrait; class GenderEnum extends AbstractTranslatedEnum { @@ -105,7 +105,7 @@ No need for a class, just use the `ConfigurableTranslatedEnum` class and define ```yaml services: enum.member.gender: - class: 'Yokai\Enum\ConfigurableTranslatedEnum' + class: 'Yokai\EnumBundle\ConfigurableTranslatedEnum' public: false tags: ['enum'] arguments: @@ -124,11 +124,11 @@ No need for a class, just use the `ConstantListTranslatedEnum` class and define ```yaml services: enum.member.gender: - class: 'Yokai\Enum\ConstantListTranslatedEnum' + class: 'Yokai\EnumBundle\ConstantListTranslatedEnum' public: false tags: ['enum'] arguments: - - '@enum.constant_extractor' + - '@yokai_enum.constant_extractor' - 'App\\Model\\Person::GENDER_*' - "@translator" - "enum.gender.%s" diff --git a/doc/sonata-admin.md b/doc/sonata-admin.md index 701fc62..9f259d1 100644 --- a/doc/sonata-admin.md +++ b/doc/sonata-admin.md @@ -16,7 +16,7 @@ use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Form\FormMapper; use Sonata\AdminBundle\Show\ShowMapper; -use Yokai\Enum\Bridge\Symfony\Form\Type\EnumType; +use Yokai\EnumBundle\Form\Type\EnumType; class MemberAdmin extends AbstractAdmin { diff --git a/src/AbstractTranslatedEnum.php b/src/AbstractTranslatedEnum.php index f24b388..db8c712 100644 --- a/src/AbstractTranslatedEnum.php +++ b/src/AbstractTranslatedEnum.php @@ -1,9 +1,9 @@ diff --git a/src/ConfigurableEnum.php b/src/ConfigurableEnum.php index 9905042..26cf0dd 100644 --- a/src/ConfigurableEnum.php +++ b/src/ConfigurableEnum.php @@ -1,6 +1,6 @@ diff --git a/src/ConfigurableTranslatedEnum.php b/src/ConfigurableTranslatedEnum.php index aa6a230..ccc0e8b 100644 --- a/src/ConfigurableTranslatedEnum.php +++ b/src/ConfigurableTranslatedEnum.php @@ -1,7 +1,6 @@ diff --git a/src/EnumInterface.php b/src/EnumInterface.php index f8b06cb..6e14c5d 100644 --- a/src/EnumInterface.php +++ b/src/EnumInterface.php @@ -1,6 +1,6 @@ diff --git a/src/EnumRegistry.php b/src/EnumRegistry.php index 8fe9c04..58a6790 100644 --- a/src/EnumRegistry.php +++ b/src/EnumRegistry.php @@ -1,9 +1,9 @@ diff --git a/src/EnumWithClassAsNameTrait.php b/src/EnumWithClassAsNameTrait.php index 1576f2d..f8b643d 100644 --- a/src/EnumWithClassAsNameTrait.php +++ b/src/EnumWithClassAsNameTrait.php @@ -1,6 +1,6 @@ diff --git a/src/Exception/CannotExtractConstantsException.php b/src/Exception/CannotExtractConstantsException.php index f90e00c..ba41d15 100644 --- a/src/Exception/CannotExtractConstantsException.php +++ b/src/Exception/CannotExtractConstantsException.php @@ -1,6 +1,6 @@ - diff --git a/src/Bridge/Symfony/Form/Type/EnumType.php b/src/Form/Type/EnumType.php similarity index 94% rename from src/Bridge/Symfony/Form/Type/EnumType.php rename to src/Form/Type/EnumType.php index b20f003..5c7c725 100644 --- a/src/Bridge/Symfony/Form/Type/EnumType.php +++ b/src/Form/Type/EnumType.php @@ -1,12 +1,12 @@ diff --git a/src/Bridge/Symfony/Bundle/Resources/config/enum.xml b/src/Resources/config/enum.xml similarity index 55% rename from src/Bridge/Symfony/Bundle/Resources/config/enum.xml rename to src/Resources/config/enum.xml index 737352a..33237db 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/enum.xml +++ b/src/Resources/config/enum.xml @@ -5,9 +5,9 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - - + + + diff --git a/src/Bridge/Symfony/Bundle/Resources/config/form.xml b/src/Resources/config/form.xml similarity index 58% rename from src/Bridge/Symfony/Bundle/Resources/config/form.xml rename to src/Resources/config/form.xml index 99a8b73..78bcd1f 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/form.xml +++ b/src/Resources/config/form.xml @@ -5,13 +5,14 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - + + - + - + diff --git a/src/Bridge/Symfony/Bundle/Resources/config/twig.xml b/src/Resources/config/twig.xml similarity index 67% rename from src/Bridge/Symfony/Bundle/Resources/config/twig.xml rename to src/Resources/config/twig.xml index e1f3039..47bd009 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/twig.xml +++ b/src/Resources/config/twig.xml @@ -5,8 +5,8 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - + + diff --git a/src/Bridge/Symfony/Bundle/Resources/config/validator.xml b/src/Resources/config/validator.xml similarity index 67% rename from src/Bridge/Symfony/Bundle/Resources/config/validator.xml rename to src/Resources/config/validator.xml index 7329b48..8c5e36d 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/validator.xml +++ b/src/Resources/config/validator.xml @@ -5,8 +5,8 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - + + diff --git a/src/Bridge/Twig/Extension/EnumExtension.php b/src/Twig/Extension/EnumExtension.php similarity index 94% rename from src/Bridge/Twig/Extension/EnumExtension.php rename to src/Twig/Extension/EnumExtension.php index 0840547..b056ff1 100644 --- a/src/Bridge/Twig/Extension/EnumExtension.php +++ b/src/Twig/Extension/EnumExtension.php @@ -1,11 +1,11 @@ diff --git a/src/Bridge/Symfony/Validator/Constraints/Enum.php b/src/Validator/Constraints/Enum.php similarity index 89% rename from src/Bridge/Symfony/Validator/Constraints/Enum.php rename to src/Validator/Constraints/Enum.php index fb7c334..50ebc47 100644 --- a/src/Bridge/Symfony/Validator/Constraints/Enum.php +++ b/src/Validator/Constraints/Enum.php @@ -1,6 +1,6 @@ diff --git a/src/Bridge/Symfony/Bundle/YokaiEnumBundle.php b/src/YokaiEnumBundle.php similarity index 72% rename from src/Bridge/Symfony/Bundle/YokaiEnumBundle.php rename to src/YokaiEnumBundle.php index f2ee0a4..05ba1b6 100644 --- a/src/Bridge/Symfony/Bundle/YokaiEnumBundle.php +++ b/src/YokaiEnumBundle.php @@ -1,11 +1,11 @@ diff --git a/tests/ConfigurableEnumTest.php b/tests/ConfigurableEnumTest.php index 4630464..ae3a690 100644 --- a/tests/ConfigurableEnumTest.php +++ b/tests/ConfigurableEnumTest.php @@ -1,9 +1,9 @@ @@ -14,7 +15,7 @@ class ConfigurableTranslatedEnumTest extends TestCase { public function testConstructedWithInvalidPattern(): void { - $this->expectException('Yokai\Enum\Exception\InvalidTranslatePatternException'); + $this->expectException(InvalidTranslatePatternException::class); $translator = $this->prophesize(TranslatorInterface::class); new ConfigurableTranslatedEnum($translator->reveal(), 'invalid.pattern', 'invalid', ['foo', 'bar']); } diff --git a/tests/ConstantExtractorTest.php b/tests/ConstantExtractorTest.php index da5ebc2..fb6a740 100644 --- a/tests/ConstantExtractorTest.php +++ b/tests/ConstantExtractorTest.php @@ -1,11 +1,11 @@ diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php b/tests/DependencyInjection/EnumExtensionTest.php similarity index 83% rename from tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php rename to tests/DependencyInjection/EnumExtensionTest.php index 8b64892..956a4f2 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/EnumExtensionTest.php +++ b/tests/DependencyInjection/EnumExtensionTest.php @@ -1,12 +1,12 @@ @@ -71,7 +71,12 @@ public function it_register_services(): void { $container = new ContainerBuilder(); $this->extension()->load([[]], $container); - $services = ['form_type.enum', 'form_extention.type_guesser.enum', 'validator.enum', 'twig.extension.enum']; + $services = [ + 'yokai_enum.form_type.enum_type', + 'yokai_enum.form_extension.enum_type_guesser', + 'yokai_enum.validator_constraints.enum_validator', + 'yokai_enum.twig_extension.enum_extension', + ]; foreach ($services as $service) { self::assertTrue($container->has($service), sprintf('Service "%s" is registered', $service)); diff --git a/tests/EnumRegistryTest.php b/tests/EnumRegistryTest.php index 70fba77..f018b4f 100644 --- a/tests/EnumRegistryTest.php +++ b/tests/EnumRegistryTest.php @@ -1,15 +1,17 @@ @@ -33,14 +35,14 @@ protected function tearDown(): void public function testAddDuplicatedException(): void { - $this->expectException('Yokai\Enum\Exception\DuplicatedEnumException'); + $this->expectException(DuplicatedEnumException::class); $this->registry->add(new GenderEnum); $this->registry->add(new GenderEnum); } public function testGetInvalidException(): void { - $this->expectException('Yokai\Enum\Exception\InvalidEnumException'); + $this->expectException(InvalidEnumException::class); $this->registry->add(new GenderEnum); $this->registry->get('type'); } diff --git a/tests/Fixtures/GenderEnum.php b/tests/Fixtures/GenderEnum.php index 8906e97..ab787f1 100644 --- a/tests/Fixtures/GenderEnum.php +++ b/tests/Fixtures/GenderEnum.php @@ -1,9 +1,9 @@ diff --git a/tests/Fixtures/StateEnum.php b/tests/Fixtures/StateEnum.php index 4863fe2..93cec36 100644 --- a/tests/Fixtures/StateEnum.php +++ b/tests/Fixtures/StateEnum.php @@ -1,9 +1,9 @@ diff --git a/tests/Fixtures/SubscriptionEnum.php b/tests/Fixtures/SubscriptionEnum.php index 8f245d2..26f18ca 100644 --- a/tests/Fixtures/SubscriptionEnum.php +++ b/tests/Fixtures/SubscriptionEnum.php @@ -1,9 +1,9 @@ diff --git a/tests/Fixtures/TypeEnum.php b/tests/Fixtures/TypeEnum.php index e42bcab..de4bf65 100644 --- a/tests/Fixtures/TypeEnum.php +++ b/tests/Fixtures/TypeEnum.php @@ -1,8 +1,8 @@ diff --git a/tests/Fixtures/Vehicle.php b/tests/Fixtures/Vehicle.php index ef8d2ef..a8e27e5 100644 --- a/tests/Fixtures/Vehicle.php +++ b/tests/Fixtures/Vehicle.php @@ -1,6 +1,6 @@ */ class EnumTypeGuesserTest extends TypeTestCase { - const TEST_CLASS = 'Yokai\Enum\Tests\Bridge\Symfony\Form\Extension\EnumTypeGuesserTest_TestClass'; + const TEST_CLASS = EnumTypeGuesserTest_TestClass::class; const TEST_PROPERTY = 'property'; @@ -54,7 +54,9 @@ protected function setUp(): void $this->metadata = new ClassMetadata(self::TEST_CLASS); $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Enum(['enum' => GenderEnum::class])); - $this->metadataFactory = $this->prophesize('Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface'); + $this->metadataFactory = $this->prophesize( + 'Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface' + ); $this->metadataFactory->getMetadataFor(self::TEST_CLASS) ->willReturn($this->metadata); @@ -98,7 +100,10 @@ public function testCreateForm(): void $form = $this->factory->create(FormType::class, new $class, ['data_class' => $class]) ->add(self::TEST_PROPERTY); - $this->assertEquals(['Male' => 'male', 'Female' => 'female'], $form->get(self::TEST_PROPERTY)->getConfig()->getOption('choices')); + $this->assertEquals( + ['Male' => 'male', 'Female' => 'female'], + $form->get(self::TEST_PROPERTY)->getConfig()->getOption('choices') + ); } protected function getExtensions(): array diff --git a/tests/Bridge/Symfony/Form/TestExtension.php b/tests/Form/TestExtension.php similarity index 86% rename from tests/Bridge/Symfony/Form/TestExtension.php rename to tests/Form/TestExtension.php index dc2e278..3b1d8ce 100644 --- a/tests/Bridge/Symfony/Form/TestExtension.php +++ b/tests/Form/TestExtension.php @@ -1,12 +1,12 @@ diff --git a/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php b/tests/Form/Type/EnumTypeTest.php similarity index 87% rename from tests/Bridge/Symfony/Form/Type/EnumTypeTest.php rename to tests/Form/Type/EnumTypeTest.php index 2a089fe..1bf87a0 100644 --- a/tests/Bridge/Symfony/Form/Type/EnumTypeTest.php +++ b/tests/Form/Type/EnumTypeTest.php @@ -1,13 +1,13 @@ diff --git a/tests/Bridge/Twig/Extension/EnumExtensionTest.php b/tests/Twig/Extension/EnumExtensionTest.php similarity index 93% rename from tests/Bridge/Twig/Extension/EnumExtensionTest.php rename to tests/Twig/Extension/EnumExtensionTest.php index a2a3ff5..45693f6 100644 --- a/tests/Bridge/Twig/Extension/EnumExtensionTest.php +++ b/tests/Twig/Extension/EnumExtensionTest.php @@ -1,14 +1,14 @@ diff --git a/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php b/tests/Validator/Constraints/EnumValidatorTest.php similarity index 91% rename from tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php rename to tests/Validator/Constraints/EnumValidatorTest.php index b475b86..439f3fa 100644 --- a/tests/Bridge/Symfony/Validator/Constraints/EnumValidatorTest.php +++ b/tests/Validator/Constraints/EnumValidatorTest.php @@ -1,16 +1,16 @@ From e528f0730abf8c782c9b4d8c2135d2a028037476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Thu, 25 Jun 2020 22:20:25 +0200 Subject: [PATCH 09/15] Updated dev dependencies and fixes PHPUnit deprecations --- composer.json | 18 ++++++++++-------- tests/ConfigurableEnumTest.php | 1 - tests/ConfigurableTranslatedEnumTest.php | 1 - tests/ConstantExtractorTest.php | 5 ++--- tests/ConstantListEnumTest.php | 1 - tests/ConstantListTranslatedEnumTest.php | 1 - .../TaggedEnumCollectorCompilerPassTest.php | 2 +- .../DependencyInjection/EnumExtensionTest.php | 2 +- tests/EnumRegistryTest.php | 1 - tests/Form/Extension/EnumTypeGuesserTest.php | 3 +++ tests/Form/Type/EnumTypeTest.php | 3 +++ tests/TestCase.php | 11 +++++++++++ tests/Twig/Extension/EnumExtensionTest.php | 2 +- .../Constraints/EnumValidatorTest.php | 3 +++ 14 files changed, 35 insertions(+), 19 deletions(-) create mode 100644 tests/TestCase.php diff --git a/composer.json b/composer.json index 2f3dd67..1a3d749 100644 --- a/composer.json +++ b/composer.json @@ -10,16 +10,18 @@ } ], "require": { - "php": "^7.1.3" + "php": "^7.1.3", + "symfony/framework-bundle": "^4.4|^5.0" }, "require-dev": { - "doctrine/annotations": "~1.3", - "phpunit/phpunit": "~7.0", - "symfony/form": "^4.2", - "symfony/framework-bundle": "^4.2", - "symfony/twig-bundle": "^4.2", - "symfony/validator": "^4.2", - "twig/twig": "^2.0" + "doctrine/annotations": "^1.3", + "phpunit/phpunit": "^9.0", + "symfony/form": "^4.4", + "symfony/framework-bundle": "^4.4", + "symfony/twig-bundle": "^4.4", + "symfony/validator": "^4.4", + "twig/twig": "^2.0", + "phpspec/prophecy-phpunit": "^2.0" }, "suggest": { "symfony/framework-bundle": "Integrate with Symfony Framework", diff --git a/tests/ConfigurableEnumTest.php b/tests/ConfigurableEnumTest.php index ae3a690..487a357 100644 --- a/tests/ConfigurableEnumTest.php +++ b/tests/ConfigurableEnumTest.php @@ -2,7 +2,6 @@ namespace Yokai\EnumBundle\Tests; -use PHPUnit\Framework\TestCase; use Yokai\EnumBundle\ConfigurableEnum; class ConfigurableEnumTest extends TestCase diff --git a/tests/ConfigurableTranslatedEnumTest.php b/tests/ConfigurableTranslatedEnumTest.php index 48ab2e6..3c2abdd 100644 --- a/tests/ConfigurableTranslatedEnumTest.php +++ b/tests/ConfigurableTranslatedEnumTest.php @@ -2,7 +2,6 @@ namespace Yokai\EnumBundle\Tests; -use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\EnumBundle\ConfigurableTranslatedEnum; diff --git a/tests/ConstantExtractorTest.php b/tests/ConstantExtractorTest.php index fb6a740..c061f9e 100644 --- a/tests/ConstantExtractorTest.php +++ b/tests/ConstantExtractorTest.php @@ -3,7 +3,6 @@ namespace Yokai\EnumBundle\Tests; use Generator; -use PHPUnit\Framework\TestCase; use Yokai\EnumBundle\ConstantExtractor; use Yokai\EnumBundle\Exception\CannotExtractConstantsException; @@ -20,7 +19,7 @@ public function getExtractor(): ConstantExtractor public function testExtractMalformedPattern(string $pattern, string $exceptionMessage): void { $this->expectException(CannotExtractConstantsException::class); - $this->expectExceptionMessageRegExp($exceptionMessage); + $this->expectExceptionMessageMatches($exceptionMessage); $this->getExtractor()->extract($pattern); } @@ -31,7 +30,7 @@ public function testExtractMalformedPattern(string $pattern, string $exceptionMe public function testExtractEmpty(string $pattern, string $exceptionMessage): void { $this->expectException(CannotExtractConstantsException::class); - $this->expectExceptionMessageRegExp($exceptionMessage); + $this->expectExceptionMessageMatches($exceptionMessage); $this->getExtractor()->extract($pattern); } diff --git a/tests/ConstantListEnumTest.php b/tests/ConstantListEnumTest.php index 5d92613..95b44c6 100644 --- a/tests/ConstantListEnumTest.php +++ b/tests/ConstantListEnumTest.php @@ -2,7 +2,6 @@ namespace Yokai\EnumBundle\Tests; -use PHPUnit\Framework\TestCase; use Yokai\EnumBundle\ConstantExtractor; use Yokai\EnumBundle\ConstantListEnum; use Yokai\EnumBundle\Tests\Fixtures\Vehicle; diff --git a/tests/ConstantListTranslatedEnumTest.php b/tests/ConstantListTranslatedEnumTest.php index 3cb0943..beea2d7 100644 --- a/tests/ConstantListTranslatedEnumTest.php +++ b/tests/ConstantListTranslatedEnumTest.php @@ -2,7 +2,6 @@ namespace Yokai\EnumBundle\Tests; -use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\EnumBundle\ConstantExtractor; diff --git a/tests/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php b/tests/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php index 7c1d304..6216d32 100644 --- a/tests/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php +++ b/tests/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php @@ -2,9 +2,9 @@ namespace Yokai\EnumBundle\Tests\DependencyInjection\CompilerPass; -use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\Reference; use Yokai\EnumBundle\DependencyInjection\CompilerPass\TaggedEnumCollectorCompilerPass; +use Yokai\EnumBundle\Tests\TestCase; /** * @author Yann Eugoné diff --git a/tests/DependencyInjection/EnumExtensionTest.php b/tests/DependencyInjection/EnumExtensionTest.php index 956a4f2..f158c08 100644 --- a/tests/DependencyInjection/EnumExtensionTest.php +++ b/tests/DependencyInjection/EnumExtensionTest.php @@ -2,11 +2,11 @@ namespace Yokai\EnumBundle\Tests\DependencyInjection; -use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; use Yokai\EnumBundle\DependencyInjection\EnumExtension; use Yokai\EnumBundle\EnumInterface; use Yokai\EnumBundle\EnumRegistry; +use Yokai\EnumBundle\Tests\TestCase; /** * @author Yann Eugoné diff --git a/tests/EnumRegistryTest.php b/tests/EnumRegistryTest.php index f018b4f..afb0c4e 100644 --- a/tests/EnumRegistryTest.php +++ b/tests/EnumRegistryTest.php @@ -2,7 +2,6 @@ namespace Yokai\EnumBundle\Tests; -use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; use Symfony\Contracts\Translation\TranslatorInterface; use Yokai\EnumBundle\EnumRegistry; diff --git a/tests/Form/Extension/EnumTypeGuesserTest.php b/tests/Form/Extension/EnumTypeGuesserTest.php index 84cb95a..aa7994c 100644 --- a/tests/Form/Extension/EnumTypeGuesserTest.php +++ b/tests/Form/Extension/EnumTypeGuesserTest.php @@ -2,6 +2,7 @@ namespace Yokai\EnumBundle\Tests\Form\Extension; +use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Guess\Guess; @@ -21,6 +22,8 @@ */ class EnumTypeGuesserTest extends TypeTestCase { + use ProphecyTrait; + const TEST_CLASS = EnumTypeGuesserTest_TestClass::class; const TEST_PROPERTY = 'property'; diff --git a/tests/Form/Type/EnumTypeTest.php b/tests/Form/Type/EnumTypeTest.php index 1bf87a0..72aec27 100644 --- a/tests/Form/Type/EnumTypeTest.php +++ b/tests/Form/Type/EnumTypeTest.php @@ -2,6 +2,7 @@ namespace Yokai\EnumBundle\Tests\Form\Type; +use Prophecy\PhpUnit\ProphecyTrait; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\Test\TypeTestCase; use Yokai\EnumBundle\EnumRegistry; @@ -14,6 +15,8 @@ */ class EnumTypeTest extends TypeTestCase { + use ProphecyTrait; + private $enumRegistry; protected function setUp(): void diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..95f0b8a --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,11 @@ + Date: Thu, 25 Jun 2020 22:27:22 +0200 Subject: [PATCH 10/15] PSR12 code style --- .gitignore | 10 ++++----- composer.json | 3 ++- phpcs.xml.dist | 21 +++++++++++++++++++ phpunit.xml.dist | 2 +- src/AbstractTranslatedEnum.php | 4 +++- src/ConfigurableEnum.php | 4 +++- src/ConfigurableTranslatedEnum.php | 4 +++- src/ConstantExtractor.php | 4 +++- src/ConstantListEnum.php | 4 +++- src/ConstantListTranslatedEnum.php | 4 +++- .../TaggedEnumCollectorCompilerPass.php | 4 +++- src/DependencyInjection/Configuration.php | 4 +++- src/DependencyInjection/EnumExtension.php | 4 +++- src/EnumInterface.php | 4 +++- src/EnumRegistry.php | 4 +++- src/EnumWithClassAsNameTrait.php | 4 +++- .../CannotExtractConstantsException.php | 4 +++- src/Exception/DuplicatedEnumException.php | 4 +++- src/Exception/InvalidEnumException.php | 4 +++- .../InvalidTranslatePatternException.php | 4 +++- src/Form/Extension/EnumTypeGuesser.php | 4 +++- src/Form/Type/EnumType.php | 4 +++- src/Twig/Extension/EnumExtension.php | 4 +++- src/Validator/Constraints/Enum.php | 4 +++- src/Validator/Constraints/EnumValidator.php | 6 ++++-- src/YokaiEnumBundle.php | 8 ++++--- tests/bootstrap.php | 8 ------- 27 files changed, 98 insertions(+), 40 deletions(-) create mode 100644 phpcs.xml.dist delete mode 100644 tests/bootstrap.php diff --git a/.gitignore b/.gitignore index 6b44be9..09a9d69 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ -/phpunit.xml -composer.lock +/.idea/ /vendor/ -/build/ -.idea/ -docker-compose.yml +/.phpcs-cache +/composer.lock +/docker-compose.yml +/phpunit.xml diff --git a/composer.json b/composer.json index 1a3d749..c5cc9e5 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "symfony/twig-bundle": "^4.4", "symfony/validator": "^4.4", "twig/twig": "^2.0", - "phpspec/prophecy-phpunit": "^2.0" + "phpspec/prophecy-phpunit": "^2.0", + "squizlabs/php_codesniffer": "^3.5" }, "suggest": { "symfony/framework-bundle": "Integrate with Symfony Framework", diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..3d26c56 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,21 @@ + + + + + + + + + + + + src/ + + + + + + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 827c012..dd1c914 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,7 @@ processIsolation="false" stopOnFailure="false" colors="true" - bootstrap="tests/bootstrap.php" + bootstrap="vendor/autoload.php" > diff --git a/src/AbstractTranslatedEnum.php b/src/AbstractTranslatedEnum.php index db8c712..7e9a0f3 100644 --- a/src/AbstractTranslatedEnum.php +++ b/src/AbstractTranslatedEnum.php @@ -1,4 +1,6 @@ -choices = null; diff --git a/src/YokaiEnumBundle.php b/src/YokaiEnumBundle.php index 05ba1b6..e798e6b 100644 --- a/src/YokaiEnumBundle.php +++ b/src/YokaiEnumBundle.php @@ -1,4 +1,6 @@ -addCompilerPass(new TaggedEnumCollectorCompilerPass) + ->addCompilerPass(new TaggedEnumCollectorCompilerPass()) ; } @@ -27,6 +29,6 @@ public function build(ContainerBuilder $container): void */ public function getContainerExtension(): EnumExtension { - return new EnumExtension; + return new EnumExtension(); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index 9cff224..0000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,8 +0,0 @@ - Date: Thu, 25 Jun 2020 22:41:23 +0200 Subject: [PATCH 11/15] Uniformize tests --- tests/ConfigurableEnumTest.php | 3 ++ tests/ConstantExtractorTest.php | 3 ++ tests/ConstantListEnumTest.php | 3 ++ tests/ConstantListTranslatedEnumTest.php | 8 ++--- .../TaggedEnumCollectorCompilerPassTest.php | 17 ++++------ tests/EnumRegistryTest.php | 5 --- tests/Form/Extension/EnumTypeGuesserTest.php | 4 +-- tests/Form/Type/EnumTypeTest.php | 6 ++-- tests/TestCase.php | 3 ++ tests/Twig/Extension/EnumExtensionTest.php | 7 ---- .../Constraints/EnumValidatorTest.php | 33 ++++++++----------- 11 files changed, 40 insertions(+), 52 deletions(-) diff --git a/tests/ConfigurableEnumTest.php b/tests/ConfigurableEnumTest.php index 487a357..fcd6702 100644 --- a/tests/ConfigurableEnumTest.php +++ b/tests/ConfigurableEnumTest.php @@ -4,6 +4,9 @@ use Yokai\EnumBundle\ConfigurableEnum; +/** + * @author Yann Eugoné + */ class ConfigurableEnumTest extends TestCase { public function testConfigurability(): void diff --git a/tests/ConstantExtractorTest.php b/tests/ConstantExtractorTest.php index c061f9e..0c39755 100644 --- a/tests/ConstantExtractorTest.php +++ b/tests/ConstantExtractorTest.php @@ -6,6 +6,9 @@ use Yokai\EnumBundle\ConstantExtractor; use Yokai\EnumBundle\Exception\CannotExtractConstantsException; +/** + * @author Yann Eugoné + */ class ConstantExtractorTest extends TestCase { public function getExtractor(): ConstantExtractor diff --git a/tests/ConstantListEnumTest.php b/tests/ConstantListEnumTest.php index 95b44c6..e9d68d7 100644 --- a/tests/ConstantListEnumTest.php +++ b/tests/ConstantListEnumTest.php @@ -6,6 +6,9 @@ use Yokai\EnumBundle\ConstantListEnum; use Yokai\EnumBundle\Tests\Fixtures\Vehicle; +/** + * @author Yann Eugoné + */ class ConstantListEnumTest extends TestCase { public function getEnum(string $pattern, string $name): ConstantListEnum diff --git a/tests/ConstantListTranslatedEnumTest.php b/tests/ConstantListTranslatedEnumTest.php index beea2d7..9588996 100644 --- a/tests/ConstantListTranslatedEnumTest.php +++ b/tests/ConstantListTranslatedEnumTest.php @@ -8,6 +8,9 @@ use Yokai\EnumBundle\ConstantListTranslatedEnum; use Yokai\EnumBundle\Tests\Fixtures\Vehicle; +/** + * @author Yann Eugoné + */ class ConstantListTranslatedEnumTest extends TestCase { /** @@ -20,11 +23,6 @@ protected function setUp(): void $this->translator = $this->prophesize(TranslatorInterface::class); } - protected function tearDown(): void - { - $this->translator = null; - } - public function getEnum(string $pattern, string $name): ConstantListTranslatedEnum { return new ConstantListTranslatedEnum( diff --git a/tests/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php b/tests/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php index 6216d32..8e6e31d 100644 --- a/tests/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php +++ b/tests/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php @@ -2,6 +2,8 @@ namespace Yokai\EnumBundle\Tests\DependencyInjection\CompilerPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; use Yokai\EnumBundle\DependencyInjection\CompilerPass\TaggedEnumCollectorCompilerPass; use Yokai\EnumBundle\Tests\TestCase; @@ -21,14 +23,9 @@ protected function setUp(): void $this->compiler = new TaggedEnumCollectorCompilerPass; } - protected function tearDown(): void - { - unset($this->compiler); - } - public function testCollectWhenServiceNotAvailable(): void { - $compiler = $this->prophesize('Symfony\Component\DependencyInjection\ContainerBuilder'); + $compiler = $this->prophesize(ContainerBuilder::class); $compiler->hasDefinition('enum.registry')->shouldBeCalled()->willReturn(false); $this->compiler->process($compiler->reveal()); @@ -36,16 +33,16 @@ public function testCollectWhenServiceNotAvailable(): void public function testCollectEnums(): void { - $registry = $this->prophesize('Symfony\Component\DependencyInjection\Definition'); + $registry = $this->prophesize(Definition::class); $registry->addMethodCall('add', [new Reference('enum.gender')])->shouldBeCalled(); $registry->addMethodCall('add', [new Reference('enum.type')])->shouldBeCalled(); - $compiler = $this->prophesize('Symfony\Component\DependencyInjection\ContainerBuilder'); + $compiler = $this->prophesize(ContainerBuilder::class); $compiler->hasDefinition('enum.registry')->shouldBeCalled()->willReturn(true); $compiler->getDefinition('enum.registry')->shouldBeCalled()->willReturn($registry); $compiler->findTaggedServiceIds('enum')->shouldBeCalled()->willReturn([ - 'enum.gender' => $this->prophesize('Symfony\Component\DependencyInjection\Definition')->reveal(), - 'enum.type' => $this->prophesize('Symfony\Component\DependencyInjection\Definition')->reveal(), + 'enum.gender' => $this->prophesize(Definition::class)->reveal(), + 'enum.type' => $this->prophesize(Definition::class)->reveal(), ]); $this->compiler->process($compiler->reveal()); diff --git a/tests/EnumRegistryTest.php b/tests/EnumRegistryTest.php index afb0c4e..98b11f5 100644 --- a/tests/EnumRegistryTest.php +++ b/tests/EnumRegistryTest.php @@ -27,11 +27,6 @@ protected function setUp(): void $this->registry = new EnumRegistry; } - protected function tearDown(): void - { - unset($this->registry); - } - public function testAddDuplicatedException(): void { $this->expectException(DuplicatedEnumException::class); diff --git a/tests/Form/Extension/EnumTypeGuesserTest.php b/tests/Form/Extension/EnumTypeGuesserTest.php index aa7994c..fc46edc 100644 --- a/tests/Form/Extension/EnumTypeGuesserTest.php +++ b/tests/Form/Extension/EnumTypeGuesserTest.php @@ -57,9 +57,7 @@ protected function setUp(): void $this->metadata = new ClassMetadata(self::TEST_CLASS); $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Enum(['enum' => GenderEnum::class])); - $this->metadataFactory = $this->prophesize( - 'Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface' - ); + $this->metadataFactory = $this->prophesize(MetadataFactoryInterface::class); $this->metadataFactory->getMetadataFor(self::TEST_CLASS) ->willReturn($this->metadata); diff --git a/tests/Form/Type/EnumTypeTest.php b/tests/Form/Type/EnumTypeTest.php index 72aec27..acade77 100644 --- a/tests/Form/Type/EnumTypeTest.php +++ b/tests/Form/Type/EnumTypeTest.php @@ -5,6 +5,8 @@ use Prophecy\PhpUnit\ProphecyTrait; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\Test\TypeTestCase; +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; +use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; use Yokai\EnumBundle\EnumRegistry; use Yokai\EnumBundle\Form\Type\EnumType; use Yokai\EnumBundle\Tests\Fixtures\GenderEnum; @@ -31,13 +33,13 @@ protected function setUp(): void public function testEnumOptionIsRequired(): void { - $this->expectException('Symfony\Component\OptionsResolver\Exception\MissingOptionsException'); + $this->expectException(MissingOptionsException::class); $this->createForm(); } public function testEnumOptionIsInvalid(): void { - $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException'); + $this->expectException(InvalidOptionsException::class); $this->createForm('state'); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 95f0b8a..2a4f28c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,9 @@ use PHPUnit\Framework\TestCase as PHPUnitTestCase; use Prophecy\PhpUnit\ProphecyTrait; +/** + * @author Yann Eugoné + */ abstract class TestCase extends PHPUnitTestCase { use ProphecyTrait; diff --git a/tests/Twig/Extension/EnumExtensionTest.php b/tests/Twig/Extension/EnumExtensionTest.php index 21fa66c..a46809e 100644 --- a/tests/Twig/Extension/EnumExtensionTest.php +++ b/tests/Twig/Extension/EnumExtensionTest.php @@ -25,13 +25,6 @@ protected function setUp(): void $this->registry = $this->prophesize(EnumRegistry::class); } - protected function tearDown(): void - { - unset( - $this->registry - ); - } - public function testEnumLabel(): void { $enum = $this->prophesize(EnumInterface::class); diff --git a/tests/Validator/Constraints/EnumValidatorTest.php b/tests/Validator/Constraints/EnumValidatorTest.php index 956da46..451a05c 100644 --- a/tests/Validator/Constraints/EnumValidatorTest.php +++ b/tests/Validator/Constraints/EnumValidatorTest.php @@ -4,8 +4,9 @@ use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; -use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Validator\Constraints\Choice; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; +use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; use Yokai\EnumBundle\EnumRegistry; use Yokai\EnumBundle\Tests\Fixtures\GenderEnum; @@ -34,19 +35,19 @@ protected function createValidator(): EnumValidator public function testAcceptOnlyEnum(): void { - $this->expectException('Symfony\Component\Validator\Exception\UnexpectedTypeException'); + $this->expectException(UnexpectedTypeException::class); $this->validator->validate(null, new Choice); } public function testEnumIsRequired(): void { - $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); + $this->expectException(ConstraintDefinitionException::class); $this->validator->validate('foo', new Enum); } public function testValidEnumIsRequired(): void { - $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); + $this->expectException(ConstraintDefinitionException::class); $this->validator->validate('foo', new Enum('state')); } @@ -70,15 +71,11 @@ public function testInvalidSingleEnum(): void $this->validator->validate('foo', $constraint); - $violationAssertion = $this->buildViolation('myMessage') + $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"foo"') - ->setCode(Choice::NO_SUCH_CHOICE_ERROR); - - if (version_compare(Kernel::VERSION, '4.3', '>=')) { - $violationAssertion->setParameter('{{ choices }}', '"customer", "prospect"'); - } - - $violationAssertion->assertRaised(); + ->setCode(Choice::NO_SUCH_CHOICE_ERROR) + ->setParameter('{{ choices }}', '"customer", "prospect"') + ->assertRaised(); } public function testValidMultipleEnum(): void @@ -96,15 +93,11 @@ public function testInvalidMultipleEnum(): void $this->validator->validate(['customer', 'foo'], $constraint); - $violationAssertion = $this->buildViolation('myMessage') + $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"foo"') ->setInvalidValue('foo') - ->setCode(Choice::NO_SUCH_CHOICE_ERROR); - - if (version_compare(Kernel::VERSION, '4.3', '>=')) { - $violationAssertion->setParameter('{{ choices }}', '"customer", "prospect"'); - } - - $violationAssertion->assertRaised(); + ->setCode(Choice::NO_SUCH_CHOICE_ERROR) + ->setParameter('{{ choices }}', '"customer", "prospect"') + ->assertRaised(); } } From dfb2e110db085b2b9eb6accfab33eb55c4d71ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Thu, 25 Jun 2020 22:41:48 +0200 Subject: [PATCH 12/15] Minor fixes --- src/ConstantExtractor.php | 9 +++++++-- src/ConstantListEnum.php | 3 +++ src/ConstantListTranslatedEnum.php | 3 +++ src/EnumRegistry.php | 2 +- src/Exception/CannotExtractConstantsException.php | 7 +++++-- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/ConstantExtractor.php b/src/ConstantExtractor.php index 44c9811..cda8924 100644 --- a/src/ConstantExtractor.php +++ b/src/ConstantExtractor.php @@ -8,7 +8,12 @@ use ReflectionException; use Yokai\EnumBundle\Exception\CannotExtractConstantsException; -final class ConstantExtractor +/** + * @internal + * + * @author Yann Eugoné + */ +class ConstantExtractor { public function extract(string $pattern): array { @@ -26,7 +31,7 @@ private function filter(array $constants, string $regexp, string $pattern): arra $matchingNames = preg_grep($regexp, array_keys($constants)); if (count($matchingNames) === 0) { - throw CannotExtractConstantsException::noConstantMathingPattern($pattern); + throw CannotExtractConstantsException::noConstantMatchingPattern($pattern); } return array_values(array_intersect_key($constants, array_flip($matchingNames))); diff --git a/src/ConstantListEnum.php b/src/ConstantListEnum.php index f59e276..f676d92 100644 --- a/src/ConstantListEnum.php +++ b/src/ConstantListEnum.php @@ -4,6 +4,9 @@ namespace Yokai\EnumBundle; +/** + * @author Yann Eugoné + */ class ConstantListEnum extends ConfigurableEnum { /** diff --git a/src/ConstantListTranslatedEnum.php b/src/ConstantListTranslatedEnum.php index 97204cc..aec3509 100644 --- a/src/ConstantListTranslatedEnum.php +++ b/src/ConstantListTranslatedEnum.php @@ -6,6 +6,9 @@ use Symfony\Contracts\Translation\TranslatorInterface; +/** + * @author Yann Eugoné + */ class ConstantListTranslatedEnum extends ConfigurableTranslatedEnum { /** diff --git a/src/EnumRegistry.php b/src/EnumRegistry.php index 545ac7d..762b224 100644 --- a/src/EnumRegistry.php +++ b/src/EnumRegistry.php @@ -51,7 +51,7 @@ public function get(string $name): EnumInterface * * @return bool */ - public function has($name): bool + public function has(string $name): bool { return isset($this->enums[$name]); } diff --git a/src/Exception/CannotExtractConstantsException.php b/src/Exception/CannotExtractConstantsException.php index 3cce515..5b5bc72 100644 --- a/src/Exception/CannotExtractConstantsException.php +++ b/src/Exception/CannotExtractConstantsException.php @@ -6,7 +6,10 @@ use InvalidArgumentException; -final class CannotExtractConstantsException extends InvalidArgumentException +/** + * @author Yann Eugoné + */ +class CannotExtractConstantsException extends InvalidArgumentException { public static function invalidPattern(string $pattern): self { @@ -25,7 +28,7 @@ public static function classHasNoPublicConstant(string $class): self return new self("Class $class has no public constant."); } - public static function noConstantMathingPattern(string $pattern): self + public static function noConstantMatchingPattern(string $pattern): self { return new self("Pattern $pattern matches no constant."); } From c450eaeea0340779c06e999744c8faec139c3163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Thu, 25 Jun 2020 23:24:50 +0200 Subject: [PATCH 13/15] Removed configuration --- src/DependencyInjection/Configuration.php | 38 ------------- src/DependencyInjection/EnumExtension.php | 14 +---- src/Resources/config/enum.xml | 2 + .../DependencyInjection/EnumExtensionTest.php | 54 +++---------------- 4 files changed, 11 insertions(+), 97 deletions(-) delete mode 100644 src/DependencyInjection/Configuration.php diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php deleted file mode 100644 index 7eee0ca..0000000 --- a/src/DependencyInjection/Configuration.php +++ /dev/null @@ -1,38 +0,0 @@ - - */ -class Configuration implements ConfigurationInterface -{ - /** - * @inheritdoc - */ - public function getConfigTreeBuilder(): TreeBuilder - { - $treeBuilder = new TreeBuilder('yokai_enum'); - $rootNode = $treeBuilder->getRootNode(); - - $rootNode - ->children() - ->booleanNode('enum_autoconfiguration') - ->info('If set to true, all services that implements EnumInterface, will obtain the "enum" tag automatically.') - ->defaultTrue() - ->end() - ->booleanNode('enum_registry_autoconfigurable') - ->info('If set to true, add an alias for the enum registry so your service can ask for it via autoconfiguration.') - ->defaultTrue() - ->end() - ->end() - ; - - return $treeBuilder; - } -} diff --git a/src/DependencyInjection/EnumExtension.php b/src/DependencyInjection/EnumExtension.php index 2faea46..081d7d6 100644 --- a/src/DependencyInjection/EnumExtension.php +++ b/src/DependencyInjection/EnumExtension.php @@ -12,7 +12,6 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\Validator\Validator\ValidatorInterface; use Yokai\EnumBundle\EnumInterface; -use Yokai\EnumBundle\EnumRegistry; /** * @author Yann Eugoné @@ -24,9 +23,6 @@ class EnumExtension extends Extension */ public function load(array $configs, ContainerBuilder $container): void { - $configuration = new Configuration(); - $config = $this->processConfiguration($configuration, $configs); - $xmlLoader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $xmlLoader->load('enum.xml'); @@ -47,13 +43,7 @@ public function load(array $configs, ContainerBuilder $container): void $xmlLoader->load('twig.xml'); } - if ($config['enum_autoconfiguration']) { - $container->registerForAutoconfiguration(EnumInterface::class) - ->addTag('enum'); - } - - if ($config['enum_registry_autoconfigurable']) { - $container->setAlias(EnumRegistry::class, 'enum.registry'); - } + $container->registerForAutoconfiguration(EnumInterface::class) + ->addTag('enum'); } } diff --git a/src/Resources/config/enum.xml b/src/Resources/config/enum.xml index 33237db..2a9775d 100644 --- a/src/Resources/config/enum.xml +++ b/src/Resources/config/enum.xml @@ -5,6 +5,8 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + diff --git a/tests/DependencyInjection/EnumExtensionTest.php b/tests/DependencyInjection/EnumExtensionTest.php index f158c08..92f7270 100644 --- a/tests/DependencyInjection/EnumExtensionTest.php +++ b/tests/DependencyInjection/EnumExtensionTest.php @@ -18,52 +18,6 @@ public function extension(): EnumExtension return new EnumExtension(); } - /** - * @test - */ - public function it_register_enum_for_autoconfiguration_by_default(): void - { - $container = new ContainerBuilder(); - $this->extension()->load([[]], $container); - - $autoconfigure = $container->getAutoconfiguredInstanceof(); - $this->assertArrayHasKey(EnumInterface::class, $autoconfigure); - $this->assertEquals(['enum' => [[]]], $autoconfigure[EnumInterface::class]->getTags()); - } - - /** - * @test - */ - public function it_do_not_register_enum_for_autoconfiguration_if_asked_to(): void - { - $container = new ContainerBuilder(); - $this->extension()->load([['enum_autoconfiguration' => false]], $container); - - $autoconfigure = $container->getAutoconfiguredInstanceof(); - $this->assertArrayNotHasKey(EnumInterface::class, $autoconfigure); - } - - /** - * @test - */ - public function it_register_enum_registry_alias_for_autowire_by_default(): void - { - $container = new ContainerBuilder(); - $this->extension()->load([[]], $container); - $this->assertTrue($container->hasAlias(EnumRegistry::class)); - } - - /** - * @test - */ - public function it_do_not_register_enum_registry_alias_for_autowire_if_asked_to(): void - { - $container = new ContainerBuilder(); - $this->extension()->load([['enum_registry_autoconfigurable' => false]], $container); - $this->assertFalse($container->hasAlias(EnumRegistry::class)); - } - - /** * @test */ @@ -71,15 +25,21 @@ public function it_register_services(): void { $container = new ContainerBuilder(); $this->extension()->load([[]], $container); + $services = [ 'yokai_enum.form_type.enum_type', 'yokai_enum.form_extension.enum_type_guesser', 'yokai_enum.validator_constraints.enum_validator', 'yokai_enum.twig_extension.enum_extension', ]; - foreach ($services as $service) { self::assertTrue($container->has($service), sprintf('Service "%s" is registered', $service)); } + + $autoconfigure = $container->getAutoconfiguredInstanceof(); + $this->assertArrayHasKey(EnumInterface::class, $autoconfigure); + $this->assertEquals(['enum' => [[]]], $autoconfigure[EnumInterface::class]->getTags()); + + $this->assertTrue($container->hasAlias(EnumRegistry::class)); } } From 4fc112ceee14371db690abfbd40e5e5f11c30ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Thu, 25 Jun 2020 23:24:59 +0200 Subject: [PATCH 14/15] Sort dependencies --- composer.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index c5cc9e5..b89a2f7 100644 --- a/composer.json +++ b/composer.json @@ -15,14 +15,14 @@ }, "require-dev": { "doctrine/annotations": "^1.3", - "phpunit/phpunit": "^9.0", - "symfony/form": "^4.4", - "symfony/framework-bundle": "^4.4", - "symfony/twig-bundle": "^4.4", - "symfony/validator": "^4.4", - "twig/twig": "^2.0", "phpspec/prophecy-phpunit": "^2.0", - "squizlabs/php_codesniffer": "^3.5" + "phpunit/phpunit": "^9.0", + "squizlabs/php_codesniffer": "^3.5", + "symfony/form": "^4.4|^5.0", + "symfony/framework-bundle": "^4.4|^5.0", + "symfony/twig-bundle": "^4.4|^5.0", + "symfony/validator": "^4.4|^5.0", + "twig/twig": "^2.0|^3.0" }, "suggest": { "symfony/framework-bundle": "Integrate with Symfony Framework", From 70d5411f23d07c60e948c42e81960ceeb42c0930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Thu, 25 Jun 2020 23:25:32 +0200 Subject: [PATCH 15/15] Droped scrutinizer and travis in favor of github actions --- .github/workflows/ci.yml | 39 +++++++++++++++++++++++++++++++++++++++ .scrutinizer.yml | 19 ------------------- .travis.yml | 32 -------------------------------- 3 files changed, 39 insertions(+), 51 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .scrutinizer.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..95457d6 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,39 @@ +name: Continuous Integration + +on: + pull_request: + push: + branches: + - "master" + +jobs: + tests: + name: "Tests" + runs-on: ubuntu-latest + + strategy: + matrix: + include: + - php-version: 7.1 + symfony-version: 4.4.* + - php-version: 7.4 + symfony-version: 4.4.* + - php-version: 7.2 + symfony-version: 5.1.* + - php-version: 7.4 + symfony-version: 5.1.* + + steps: + - name: "Checkout" + uses: actions/checkout@v2.0.0 + + - name: "Install dependencies with composer" + run: | + composer require --no-update --dev symfony/symfony:${{ matrix.symfony-version }} + composer update --no-interaction --no-progress --no-suggest + + - name: "Run checkstyle with squizlabs/php_codesniffer" + run: vendor/bin/phpcs + + - name: "Run tests with phpunit/phpunit" + run: vendor/bin/phpunit --coverage-text diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index e457268..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,19 +0,0 @@ -filter: - excluded_paths: [vendor/*, tests/*] - -before_commands: - - 'composer install --dev --prefer-source' - -tools: - external_code_coverage: true - php_mess_detector: true - php_code_sniffer: true - sensiolabs_security_checker: true - php_code_coverage: true - php_pdepend: true - php_loc: - enabled: true - excluded_dirs: [vendor, tests] - php_cpd: - enabled: true - excluded_dirs: [vendor, tests] diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 53bbdc5..0000000 --- a/.travis.yml +++ /dev/null @@ -1,32 +0,0 @@ -language: php - -php: - - 7.1 - - 7.2 - - 7.3 - -env: - - SYMFONY_VERSION="4.2.*" - - SYMFONY_VERSION="4.3.*" - -sudo: false - -cache: - directories: - - $HOME/.composer/cache - -before_install: - - composer selfupdate - - if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi; - -install: composer update --prefer-dist --no-interaction $COMPOSER_FLAGS - -script: - - vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover=coverage.clover - -after_script: - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover coverage.clover - -notifications: - email: eugone.yann@gmail.com