diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..2e00f42 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,56 @@ +name: Build Symftony/form-handler +run-name: 🧑‍💻${{ github.actor }} Run build. +on: [ push ] +jobs: + Build: + name: PHP ${{ matrix.php-versions }} // composer ${{ matrix.composer-command }} + runs-on: ubuntu-latest + strategy: + matrix: + php-versions: [ '8.1', '8.2', '8.3' ] + composer-command: + - 'install' + - 'update --prefer-lowest' + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup PHP ${{ matrix.php-versions }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + coverage: xdebug + + - name: "Composer -> Get cache directory" + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: "Composer -> Cache dependencies" + uses: actions/cache@v4 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer- + + - name: "Composer -> Install dependencies" + run: composer ${{ matrix.composer-command }} --no-progress --prefer-dist --optimize-autoloader + + - name: "Composer -> Show" + run: composer show + + - name: "Composer -> check platform requirements" + run: composer check-platform-reqs + +# - name: "Rector -> Run" +# run: rector --dry-run + + - name: "PHP cs fixer -> check" + run: vendor/bin/php-cs-fixer check src + + - name: "PHPStan -> Run" + run: vendor/bin/phpstan analyse src + + - name: "PHPUnit -> Run" + run: vendor/bin/phpunit --coverage-text + env: + XDEBUG_MODE: coverage diff --git a/.gitignore b/.gitignore index 50b321e..4091b78 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ vendor composer.lock + +.phpunit.cache .phpunit.result.cache + +.php-cs-fixer.cache diff --git a/composer.json b/composer.json index cc9e48c..afa3e27 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "symfony/form": "^6.1" }, "require-dev": { - "phpunit/phpunit": "^9.1", + "phpunit/phpunit": "^10", "symfony/dependency-injection": "^6.1", "symfony/config": "^6.1", "symfony/http-kernel": "^6.1", @@ -23,7 +23,10 @@ "phpspec/prophecy-phpunit": "^2.1", "symfony/translation": "^6.1", "symfony/contracts": "^3.5@dev", - "symfony/validator": "^6.1" + "symfony/validator": "^6.1", + "phpstan/phpstan": "^1.11", + "friendsofphp/php-cs-fixer": "dev-master", + "rector/rector": "dev-main" }, "suggest": { "symfony/dependency-injection": "^6.1", diff --git a/examples/CustomFormHandler.php b/examples/CustomFormHandler.php index 4663456..eb0ca92 100644 --- a/examples/CustomFormHandler.php +++ b/examples/CustomFormHandler.php @@ -1,5 +1,7 @@ createForm(ChoiceType::class, 'my-value', null, [ 'method' => 'GET', @@ -30,7 +29,7 @@ public function formChoice() ]); } - public function createFromRequest($request = null, $notSubmitted = true, $invalid = true) + public function createFromRequest(?mixed $request = null, $notSubmitted = true, $invalid = true) { $form = $this->createForm(ChoiceType::class, 'my-value', null, [ 'method' => 'GET', @@ -49,4 +48,4 @@ public function createFromRequest($request = null, $notSubmitted = true, $invali return $this->handleRequest($form, $request); } -} \ No newline at end of file +} diff --git a/examples/custom-form-api.php b/examples/custom-form-api.php index 6929448..049412c 100644 --- a/examples/custom-form-api.php +++ b/examples/custom-form-api.php @@ -1,9 +1,11 @@ addTypeExtension(new TransformationFailedTypeExtension()) ->addExtension(new ValidatorExtension(Validation::createValidator())) ->getFormFactory(); - // Initialize form handler $formHandler = new CustomFormHandler(); $formHandler->setFormFactory($formFactory); - // Create the form // Handle request $exception = null; try { $result = $formHandler->createFromRequest(); -} catch (FormException $e) { - $exception = $e; +} catch (FormException $formException) { + $exception = $formException; } ?> @@ -41,7 +40,9 @@ Form handler throw NotSubmitted/Invalid Exception example - +

Form handler will return default data when exception append

@@ -67,13 +68,22 @@

/!\ When you use the handler to do some API you dont need the form in your controller. You just need your data or Exception /!\

- $formHandler->handleRequest($form) : - - getMessage() ?> + $formHandler->handleRequest($form) : + + getMessage() ?> + +?>
+addTypeExtension(new TransformationFailedTypeExtension()) ->addExtension(new ValidatorExtension(Validation::createValidator())) ->getFormFactory(); - // Initialize form handler $formHandler = new CustomFormHandler(); $formHandler->setFormFactory($formFactory); - // Create the form $form = $formHandler->formChoice(); // Handle request $exception = null; try { $result = $formHandler->handleRequest($form); -} catch (FormException $e) { - $exception = $e; +} catch (FormException $formException) { + $exception = $formException; } ?> @@ -42,7 +41,9 @@ Form handler throw NotSubmitted/Invalid Exception example - +

Form handler will return default data when exception append

@@ -67,23 +68,41 @@
- $form->isSubmitted() : isSubmitted()) ?> - $form->isValid() : isSubmitted() &&var_export($form->isValid()) ?> + $form->isSubmitted() : isSubmitted()) ?> +?> + $form->isValid() : isSubmitted() &&var_export($form->isValid()) ?> +?> $form->getTransformationFailure() : - getTransformationFailure()): ?> + getTransformationFailure()): ?> getTransformationFailure()->getMessage(); ?> null + +?> - $form->getData() : getData()); ?> - $formHandler->handleRequest($form) : - + $form->getData() : getData()); +?> + $formHandler->handleRequest($form) : + getMessage() ?> + +?>
+addTypeExtension(new TransformationFailedTypeExtension()) ->addExtension(new ValidatorExtension(Validation::createValidator())) ->getFormFactory(); - // Initialize form handler $formHandler = new FormHandler(); $formHandler->setFormFactory($formFactory); - // Create the form $form = $formHandler->createForm(ChoiceType::class, 'my-value', null, [ 'method' => 'GET', @@ -46,13 +44,12 @@ 'handler_not_submitted' => 'not submitted return data', 'handler_transformation_failed' => 'transformation fail return data', ]); - // Handle request $exception = null; try { $result = $formHandler->handleRequest($form); -} catch (FormException $e) { - $exception = $e; +} catch (FormException $formException) { + $exception = $formException; } ?> @@ -60,7 +57,9 @@ Form handler throw NotSubmitted/Invalid Exception example - +

Form handler will return default data when exception append

@@ -85,23 +84,41 @@
- $form->isSubmitted() : isSubmitted()) ?> - $form->isValid() : isSubmitted() && var_export($form->isValid()) ?> + $form->isSubmitted() : isSubmitted()) ?> +?> + $form->isValid() : isSubmitted() && var_export($form->isValid()) ?> +?> $form->getTransformationFailure() : - getTransformationFailure()): ?> + getTransformationFailure() instanceof \Symfony\Component\Form\Exception\TransformationFailedException): ?> getTransformationFailure()->getMessage(); ?> null + +?> - $form->getData() : getData()); ?> - $formHandler->handleRequest($form) : - + $form->getData() : getData()); +?> + $formHandler->handleRequest($form) : + getMessage() ?> + +?>
+addTypeExtension(new InvalidTypeExtension()) ->addExtension(new ValidatorExtension(Validation::createValidator())) ->getFormFactory(); - // Initialize form handler $formHandler = new FormHandler(); $formHandler->setFormFactory($formFactory); - // Create the form $form = $formHandler->createForm(ChoiceType::class, 'my-value', null, [ 'method' => 'GET', @@ -44,13 +42,12 @@ 'handler_invalid' => true, 'handler_not_submitted' => true, ]); - // Handle request $exception = null; try { $result = $formHandler->handleRequest($form); -} catch (FormException $e) { - $exception = $e; +} catch (FormException $formException) { + $exception = $formException; } ?> @@ -58,7 +55,9 @@ Form handler throw NotSubmitted/Invalid Exception example - +

Form handler will throw NotSubmitted/Invalid exception

@@ -83,23 +82,41 @@
- $form->isSubmitted() : isSubmitted()) ?> - $form->isValid() : isSubmitted() && var_export($form->isValid()) ?> + $form->isSubmitted() : isSubmitted()) ?> +?> + $form->isValid() : isSubmitted() && var_export($form->isValid()) ?> +?> $form->getTransformationFailure() : - getTransformationFailure()): ?> + getTransformationFailure() instanceof \Symfony\Component\Form\Exception\TransformationFailedException): ?> getTransformationFailure()->getMessage(); ?> null + +?> - $form->getData() : getData()); ?> - $formHandler->handleRequest($form) : - + $form->getData() : getData()); +?> + $formHandler->handleRequest($form) : + getMessage() ?> + +?>
+addTypeExtension(new NotSubmittedTypeExtension()) ->addTypeExtension(new TransformationFailedTypeExtension()) ->getFormFactory(); - // Initialize form handler $formHandler = new FormHandler(); $formHandler->setFormFactory($formFactory); - // Create the form $form = $formHandler->createForm(ChoiceType::class, 'my-value', null, [ 'method' => 'GET', @@ -32,13 +30,12 @@ 'handler_not_submitted' => true, 'handler_transformation_failed' => true, ]); - // Handle request $exception = null; try { $result = $formHandler->handleRequest($form); -} catch (FormException $e) { - $exception = $e; +} catch (FormException $formException) { + $exception = $formException; } ?> @@ -46,7 +43,9 @@ Form handler will throw NotSubmitted/TransformationFailed exception - +

Form handler will throw NotSubmitted/TransformationFailed exception

@@ -67,23 +66,41 @@
- $form->isSubmitted() : isSubmitted()) ?> - $form->isValid() : isSubmitted() && var_export($form->isValid()) ?> + $form->isSubmitted() : isSubmitted()) ?> +?> + $form->isValid() : isSubmitted() && var_export($form->isValid()) ?> +?> $form->getTransformationFailure() : - getTransformationFailure()): ?> + getTransformationFailure() instanceof \Symfony\Component\Form\Exception\TransformationFailedException): ?> getTransformationFailure()->getMessage(); ?> null + +?> - $form->getData() : getData()); ?> - $formHandler->handleRequest($form) : - + $form->getData() : getData()); +?> + $formHandler->handleRequest($form) : + throw getMessage() ?> + +?>
+Form handler Basic - +

Form handler Basic

diff --git a/examples/menu.php b/examples/menu.php index 8e52c06..3f0194b 100644 --- a/examples/menu.php +++ b/examples/menu.php @@ -1,3 +1,8 @@ +
Basic @@ -85,3 +90,4 @@ } + - - - - src - - - src/Exception - src/*Bundle/Resources - - + @@ -18,4 +15,18 @@ tests + + + src + + + src/Exception + src/*Bundle/Resources + + + + + + + diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..4f9fbf3 --- /dev/null +++ b/rector.php @@ -0,0 +1,34 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->withPreparedSets( + deadCode: true, + codeQuality: true, + codingStyle: true, + typeDeclarations: true, + privatization: true, + naming: true, + instanceOf: true, + earlyReturn: true, + strictBooleans: true, + carbon: true, + rectorPreset: true, + phpunitCodeQuality: true, + doctrineCodeQuality: true, + symfonyCodeQuality: true, + symfonyConfigs: true, + ) + // uncomment to reach your current PHP version + ->withPhpSets() + ->withRules([ + AddVoidReturnTypeWhereNoReturnRector::class, + ]); diff --git a/src/Exception/FormException.php b/src/Exception/FormException.php index 0d90148..becc9fc 100644 --- a/src/Exception/FormException.php +++ b/src/Exception/FormException.php @@ -1,4 +1,5 @@ setDefaults([ + $optionsResolver->setDefaults([ 'handler_invalid' => false, ]); } diff --git a/src/Form/Extension/NotSubmitted/Type/NotSubmittedTypeExtension.php b/src/Form/Extension/NotSubmitted/Type/NotSubmittedTypeExtension.php index 059ae13..998ab23 100644 --- a/src/Form/Extension/NotSubmitted/Type/NotSubmittedTypeExtension.php +++ b/src/Form/Extension/NotSubmitted/Type/NotSubmittedTypeExtension.php @@ -1,4 +1,5 @@ setDefaults([ + $optionsResolver->setDefaults([ 'handler_not_submitted' => false, ]); } diff --git a/src/Form/Extension/TransformationFailed/Type/TransformationFailedTypeExtension.php b/src/Form/Extension/TransformationFailed/Type/TransformationFailedTypeExtension.php index 621f26a..bfa5225 100644 --- a/src/Form/Extension/TransformationFailed/Type/TransformationFailedTypeExtension.php +++ b/src/Form/Extension/TransformationFailed/Type/TransformationFailedTypeExtension.php @@ -1,4 +1,5 @@ setDefaults([ + $optionsResolver->setDefaults([ 'handler_transformation_failed' => false, ]); } diff --git a/src/FormBundle/DependencyInjection/Compiler/FormHandlerCompilerPass.php b/src/FormBundle/DependencyInjection/Compiler/FormHandlerCompilerPass.php index d34daf2..d15e5f5 100644 --- a/src/FormBundle/DependencyInjection/Compiler/FormHandlerCompilerPass.php +++ b/src/FormBundle/DependencyInjection/Compiler/FormHandlerCompilerPass.php @@ -1,26 +1,28 @@ findTaggedServiceIds('form.handler'); + $taggedServices = $containerBuilder->findTaggedServiceIds('form.handler'); - if (!$container->hasDefinition('form.factory') && 0 < count($taggedServices)) { + if (!$containerBuilder->hasDefinition('form.factory') && [] !== $taggedServices) { throw new \LogicException('Form factory expected for "form.handler" tagged services.'); } - $formFactory = $container->getDefinition('form.factory'); - foreach ($taggedServices as $id => $tags) { - $container->getDefinition($id)->addMethodCall('setFormFactory', [$formFactory]); + $definition = $containerBuilder->getDefinition('form.factory'); + foreach (array_keys($taggedServices) as $id) { + $containerBuilder->getDefinition($id)->addMethodCall('setFormFactory', [$definition]); } } } diff --git a/src/FormBundle/DependencyInjection/FormHandlerExtension.php b/src/FormBundle/DependencyInjection/FormHandlerExtension.php index a6258d5..f018f3e 100644 --- a/src/FormBundle/DependencyInjection/FormHandlerExtension.php +++ b/src/FormBundle/DependencyInjection/FormHandlerExtension.php @@ -1,4 +1,5 @@ load('services.yml'); + $yamlFileLoader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../Resources/config')); + $yamlFileLoader->load('services.yml'); } } diff --git a/src/FormBundle/FormHandlerBundle.php b/src/FormBundle/FormHandlerBundle.php index 3a51526..c1e783a 100644 --- a/src/FormBundle/FormHandlerBundle.php +++ b/src/FormBundle/FormHandlerBundle.php @@ -1,5 +1,7 @@ addCompilerPass(new FormHandlerCompilerPass()); + $containerBuilder->addCompilerPass(new FormHandlerCompilerPass()); } } diff --git a/src/FormBundle/Serializer/Normalizer/FormInvalidNormalizer.php b/src/FormBundle/Serializer/Normalizer/FormInvalidNormalizer.php index 6ee830b..2d11dad 100644 --- a/src/FormBundle/Serializer/Normalizer/FormInvalidNormalizer.php +++ b/src/FormBundle/Serializer/Normalizer/FormInvalidNormalizer.php @@ -1,4 +1,7 @@ isSubmitted() && !$data->isValid(); } - public function normalize($object, $format = null, array $context = []): float|int|bool|\ArrayObject|array|string|null + public function normalize(mixed $object, ?string $format = null, array $context = []): float|int|bool|\ArrayObject|array|string|null { return [ 'code' => $context['status_code'] ?? null, @@ -47,43 +49,42 @@ public function normalize($object, $format = null, array $context = []): float|i /** * This code has been taken from JMSSerializer. - * - * @param FormInterface $data - * - * @return array */ private function convertFormToArray(FormInterface $data): array { - $form = $errors = []; + $form = []; + $errors = []; foreach ($data->getErrors() as $error) { + // @phpstan-ignore-next-line $errors[] = $this->getErrorMessage($error); } - if ($errors) { + + if ($errors !== []) { $form['errors'] = $errors; } + $children = []; foreach ($data->all() as $child) { if ($child instanceof FormInterface) { $children[$child->getName()] = $this->convertFormToArray($child); } } - if ($children) { + + if ($children !== []) { $form['children'] = $children; } + return $form; } - /** - * @param FormError $error - * - * @return string - */ - private function getErrorMessage(FormError $error) + private function getErrorMessage(FormError $formError): string { - if (null !== $error->getMessagePluralization()) { - return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), 'validators'); + if (null !== $formError->getMessagePluralization()) { + // @phpstan-ignore-next-line + return $this->translator->transChoice($formError->getMessageTemplate(), $formError->getMessagePluralization(), $formError->getMessageParameters(), 'validators'); } - return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators'); + + return $this->translator->trans($formError->getMessageTemplate(), $formError->getMessageParameters(), 'validators'); } } diff --git a/src/FormHandler.php b/src/FormHandler.php index 1a773a9..d6ad8a6 100644 --- a/src/FormHandler.php +++ b/src/FormHandler.php @@ -1,4 +1,5 @@ formFactory = $formFactory; } - /** - * @param string $type - * @param null $name - * @param null $data - * @param array $options - * - * @return FormInterface - */ - public function createForm(string $type, $name = null, $data = null, array $options = []): FormInterface + public function createForm(string $type, string $name = null, mixed $data = null, array $options = []): FormInterface { if (null !== $name) { return $this->formFactory->createNamed($name, $type, $data, $options); @@ -35,15 +31,7 @@ public function createForm(string $type, $name = null, $data = null, array $opti return $this->formFactory->create($type, $data, $options); } - /** - * @param $request - * @param FormInterface $form - * - * @return mixed - * - * @throws mixed - */ - public function handleRequest(FormInterface $form, $request = null): mixed + public function handleRequest(FormInterface $form, mixed $request = null): mixed { $form->handleRequest($request); $formConfig = $form->getConfig(); @@ -60,13 +48,6 @@ public function handleRequest(FormInterface $form, $request = null): mixed return $this->postSubmit($form); } - /** - * @param FormInterface $form - * @param array $data - * @param bool $clearMissing - * - * @return mixed|null - */ public function handleData(FormInterface $form, array $data = [], bool $clearMissing = true): mixed { $formConfig = $form->getConfig(); @@ -85,7 +66,7 @@ public function handleData(FormInterface $form, array $data = [], bool $clearMis return $this->postSubmit($form); } - private function postSubmit(FormInterface $form,) + private function postSubmit(FormInterface $form): mixed { $formConfig = $form->getConfig(); if ($form->isSubmitted()) { @@ -99,7 +80,7 @@ private function postSubmit(FormInterface $form,) } $transformationFailure = $form->getTransformationFailure(); - if ($transformationFailure && $formConfig->hasOption('handler_transformation_failed')) { + if ($transformationFailure instanceof \Symfony\Component\Form\Exception\TransformationFailedException && $formConfig->hasOption('handler_transformation_failed')) { $failed = $formConfig->getOption('handler_transformation_failed'); if (true === $failed) { throw new TransformationFailedFormException($form, 'Transformation form failed.', 0, $transformationFailure); diff --git a/tests/Form/Extension/Invalid/Type/InvalidTypeExtensionTest.php b/tests/Form/Extension/Invalid/Type/InvalidTypeExtensionTest.php index db7fb70..28e016b 100644 --- a/tests/Form/Extension/Invalid/Type/InvalidTypeExtensionTest.php +++ b/tests/Form/Extension/Invalid/Type/InvalidTypeExtensionTest.php @@ -10,7 +10,7 @@ use Symftony\FormHandler\Form\Extension\Invalid\Type\InvalidTypeExtension; use Symfony\Component\OptionsResolver\OptionsResolver; -class InvalidTypeExtensionTest extends TestCase +final class InvalidTypeExtensionTest extends TestCase { use ProphecyTrait; @@ -18,14 +18,14 @@ class InvalidTypeExtensionTest extends TestCase private InvalidTypeExtension $invalidTypeExtension; - public function setUp(): void + protected function setUp(): void { $this->optionsResolverMock = $this->prophesize(OptionsResolver::class); $this->invalidTypeExtension = new InvalidTypeExtension(); } - public function testConfigureOptions() + public function testConfigureOptions(): void { $this->optionsResolverMock ->setDefaults([ @@ -37,8 +37,8 @@ public function testConfigureOptions() $this->invalidTypeExtension->configureOptions($this->optionsResolverMock->reveal()); } - public function testGetExtendedType() + public function testGetExtendedType(): void { - $this->assertEquals([FormType::class], $this->invalidTypeExtension->getExtendedTypes()); + $this->assertSame([FormType::class], $this->invalidTypeExtension->getExtendedTypes()); } } diff --git a/tests/Form/Extension/NotSubmitted/Type/NotSubmittedTypeExtensionTest.php b/tests/Form/Extension/NotSubmitted/Type/NotSubmittedTypeExtensionTest.php index 5cd8d20..86f8732 100644 --- a/tests/Form/Extension/NotSubmitted/Type/NotSubmittedTypeExtensionTest.php +++ b/tests/Form/Extension/NotSubmitted/Type/NotSubmittedTypeExtensionTest.php @@ -10,7 +10,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use Symftony\FormHandler\Form\Extension\NotSubmitted\Type\NotSubmittedTypeExtension; -class NotSubmittedTypeExtensionTest extends TestCase +final class NotSubmittedTypeExtensionTest extends TestCase { use ProphecyTrait; @@ -18,14 +18,14 @@ class NotSubmittedTypeExtensionTest extends TestCase private NotSubmittedTypeExtension $notSubmittedTypeExtension; - public function setUp(): void + protected function setUp(): void { $this->optionsResolverMock = $this->prophesize(OptionsResolver::class); $this->notSubmittedTypeExtension = new NotSubmittedTypeExtension(); } - public function testConfigureOptions() + public function testConfigureOptions(): void { $this->optionsResolverMock ->setDefaults([ @@ -37,8 +37,8 @@ public function testConfigureOptions() $this->notSubmittedTypeExtension->configureOptions($this->optionsResolverMock->reveal()); } - public function testGetExtendedType() + public function testGetExtendedType(): void { - $this->assertEquals([FormType::class], $this->notSubmittedTypeExtension->getExtendedTypes()); + $this->assertSame([FormType::class], $this->notSubmittedTypeExtension->getExtendedTypes()); } } diff --git a/tests/Form/Extension/TransformationFailed/Type/TransformationFailedTypeExtensionTest.php b/tests/Form/Extension/TransformationFailed/Type/TransformationFailedTypeExtensionTest.php index b43e77f..5d80836 100644 --- a/tests/Form/Extension/TransformationFailed/Type/TransformationFailedTypeExtensionTest.php +++ b/tests/Form/Extension/TransformationFailed/Type/TransformationFailedTypeExtensionTest.php @@ -10,22 +10,22 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use Symftony\FormHandler\Form\Extension\TransformationFailed\Type\TransformationFailedTypeExtension; -class TransformationFailedTypeExtensionTest extends TestCase +final class TransformationFailedTypeExtensionTest extends TestCase { use ProphecyTrait; private OptionsResolver|ObjectProphecy $optionsResolverMock; - private TransformationFailedTypeExtension $notSubmittedTypeExtension; + private TransformationFailedTypeExtension $transformationFailedTypeExtension; - public function setUp(): void + protected function setUp(): void { $this->optionsResolverMock = $this->prophesize(OptionsResolver::class); - $this->notSubmittedTypeExtension = new TransformationFailedTypeExtension(); + $this->transformationFailedTypeExtension = new TransformationFailedTypeExtension(); } - public function testConfigureOptions() + public function testConfigureOptions(): void { $this->optionsResolverMock ->setDefaults([ @@ -34,11 +34,11 @@ public function testConfigureOptions() ->willReturn($this->optionsResolverMock->reveal()) ->shouldBeCalled(); - $this->notSubmittedTypeExtension->configureOptions($this->optionsResolverMock->reveal()); + $this->transformationFailedTypeExtension->configureOptions($this->optionsResolverMock->reveal()); } - public function testGetExtendedType() + public function testGetExtendedType(): void { - $this->assertEquals([FormType::class], $this->notSubmittedTypeExtension->getExtendedTypes()); + $this->assertSame([FormType::class], $this->transformationFailedTypeExtension->getExtendedTypes()); } } diff --git a/tests/FormBundle/DependencyInjection/Compiler/FormHandlerCompilerPassTest.php b/tests/FormBundle/DependencyInjection/Compiler/FormHandlerCompilerPassTest.php index ccaab08..62980e0 100644 --- a/tests/FormBundle/DependencyInjection/Compiler/FormHandlerCompilerPassTest.php +++ b/tests/FormBundle/DependencyInjection/Compiler/FormHandlerCompilerPassTest.php @@ -9,7 +9,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; -class FormHandlerCompilerPassTest extends TestCase +final class FormHandlerCompilerPassTest extends TestCase { use ProphecyTrait; @@ -17,26 +17,26 @@ class FormHandlerCompilerPassTest extends TestCase private FormHandlerCompilerPass $formHandlerCompilerPass; - public function setUp(): void + protected function setUp(): void { $this->containerBuilder = new ContainerBuilder(); $this->formHandlerCompilerPass = new FormHandlerCompilerPass(); } - public function testProcessThrowLogicException() + public function testProcessThrowLogicException(): void { $this->expectException(\LogicException::class); $this->expectExceptionMessage('Form factory expected for "form.handler" tagged services.'); $this->containerBuilder->addDefinitions([ - 'my_custom_form_handler' => (new Definition())->addTag('form.handler') + 'my_custom_form_handler' => (new Definition())->addTag('form.handler'), ]); $this->formHandlerCompilerPass->process($this->containerBuilder); } - public function testProcess() + public function testProcess(): void { $formFactoryDefinition = new Definition(); @@ -51,7 +51,7 @@ public function testProcess() $this->formHandlerCompilerPass->process($this->containerBuilder); $this->assertSame([ - ['setFormFactory', [$formFactoryDefinition]] + ['setFormFactory', [$formFactoryDefinition]], ], $def->getMethodCalls()); } } diff --git a/tests/FormBundle/FormHandlerBundleTest.php b/tests/FormBundle/FormHandlerBundleTest.php index 38a86fb..e164e2d 100644 --- a/tests/FormBundle/FormHandlerBundleTest.php +++ b/tests/FormBundle/FormHandlerBundleTest.php @@ -8,26 +8,26 @@ use Symftony\FormHandler\FormBundle\FormHandlerBundle; use Symfony\Component\DependencyInjection\ContainerBuilder; -class FormHandlerBundleTest extends TestCase +final class FormHandlerBundleTest extends TestCase { private ContainerBuilder $containerBuilder; private FormHandlerBundle $formHandlerBundle; - public function setUp(): void + protected function setUp(): void { $this->containerBuilder = new ContainerBuilder(); $this->formHandlerBundle = new FormHandlerBundle(); } - public function testBuild() + public function testBuild(): void { $this->formHandlerBundle->build($this->containerBuilder); $containFormHandlerCompilerPass = false; - foreach ($this->containerBuilder->getCompilerPassConfig()->getPasses() as $passe) { - if ($passe instanceof FormHandlerCompilerPass) { + foreach ($this->containerBuilder->getCompilerPassConfig()->getPasses() as $compilerPass) { + if ($compilerPass instanceof FormHandlerCompilerPass) { $containFormHandlerCompilerPass = true; break; } diff --git a/tests/FormHandlerTest.php b/tests/FormHandlerTest.php index 0c35bf7..f5561a7 100644 --- a/tests/FormHandlerTest.php +++ b/tests/FormHandlerTest.php @@ -15,7 +15,7 @@ use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; -class FormHandlerTest extends TestCase +final class FormHandlerTest extends TestCase { use ProphecyTrait; @@ -25,9 +25,9 @@ class FormHandlerTest extends TestCase private FormConfigInterface|ObjectProphecy $formConfigMock; - private FormHandler|ObjectProphecy $formHandler; + private FormHandler $formHandler; - public function setUp(): void + protected function setUp(): void { $this->formFactoryMock = $this->prophesize(FormFactoryInterface::class); $this->formConfigMock = $this->prophesize(FormConfigInterface::class); @@ -36,7 +36,7 @@ public function setUp(): void $this->formHandler = new FormHandler(); } - public function testCreateNamedForm() + public function testCreateNamedForm(): void { $this->formFactoryMock ->createNamed('my_fake_name', 'my_fake_type', 'my_fake_data', ['my_fake_options']) @@ -46,7 +46,7 @@ public function testCreateNamedForm() $this->formHandler->createForm('my_fake_type', 'my_fake_name', 'my_fake_data', ['my_fake_options']); } - public function testCreateForm() + public function testCreateForm(): void { $this->formFactoryMock ->create('my_fake_type', 'my_fake_data', ['my_fake_options']) @@ -56,7 +56,7 @@ public function testCreateForm() $this->formHandler->createForm('my_fake_type', null, 'my_fake_data', ['my_fake_options']); } - public function testHandleRequestThrowNotSubmittedFormException() + public function testHandleRequestThrowNotSubmittedFormException(): void { $this->expectException(NotSubmittedFormException::class); $this->expectExceptionMessage('Not submitted form.'); @@ -89,7 +89,7 @@ public function testHandleRequestThrowNotSubmittedFormException() $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request'); } - public function testHandleRequestNotSubmitted() + public function testHandleRequestNotSubmitted(): void { $this->formMock ->handleRequest('my_fake_request') @@ -116,10 +116,10 @@ public function testHandleRequestNotSubmitted() ->willReturn('my_fake_handler_not_submitted_data') ->shouldBeCalled(); - $this->assertEquals('my_fake_handler_not_submitted_data', $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request')); + $this->assertSame('my_fake_handler_not_submitted_data', $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request')); } - public function testHandleRequestThrowInvalidFormException() + public function testHandleRequestThrowInvalidFormException(): void { $this->expectException(InvalidFormException::class); $this->expectExceptionMessage('Invalid form'); @@ -157,7 +157,7 @@ public function testHandleRequestThrowInvalidFormException() $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request'); } - public function testHandleRequestInvalid() + public function testHandleRequestInvalid(): void { $this->formMock ->handleRequest('my_fake_request') @@ -189,10 +189,10 @@ public function testHandleRequestInvalid() ->willReturn('my_fake_handler_invalid_data') ->shouldBeCalled(); - $this->assertEquals('my_fake_handler_invalid_data', $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request')); + $this->assertSame('my_fake_handler_invalid_data', $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request')); } - public function testHandleRequestThrowTransformationFailedFormException() + public function testHandleRequestThrowTransformationFailedFormException(): void { $this->expectException(TransformationFailedFormException::class); $this->expectExceptionMessage('Transformation form failed.'); @@ -235,7 +235,7 @@ public function testHandleRequestThrowTransformationFailedFormException() $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request'); } - public function testHandleRequestTransformationFailed() + public function testHandleRequestTransformationFailed(): void { $this->formMock ->handleRequest('my_fake_request') @@ -273,10 +273,10 @@ public function testHandleRequestTransformationFailed() ->willReturn('my_fake_handler_transformation_failed_data') ->shouldBeCalled(); - $this->assertEquals('my_fake_handler_transformation_failed_data', $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request')); + $this->assertSame('my_fake_handler_transformation_failed_data', $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request')); } - public function testHandleRequest() + public function testHandleRequest(): void { $this->formMock ->handleRequest('my_fake_request') @@ -308,10 +308,10 @@ public function testHandleRequest() ->willReturn('my_fake_form_data') ->shouldBeCalled(); - $this->assertEquals('my_fake_form_data', $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request')); + $this->assertSame('my_fake_form_data', $this->formHandler->handleRequest($this->formMock->reveal(), 'my_fake_request')); } - public function testHandleDataThrowNotSubmittedFormException() + public function testHandleDataThrowNotSubmittedFormException(): void { $this->expectException(NotSubmittedFormException::class); $this->expectExceptionMessage('Not submitted form.'); @@ -339,7 +339,7 @@ public function testHandleDataThrowNotSubmittedFormException() $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_request']); } - public function testHandleDataNotSubmitted() + public function testHandleDataNotSubmitted(): void { $this->formMock->getConfig() ->willReturn($this->formConfigMock) @@ -361,10 +361,10 @@ public function testHandleDataNotSubmitted() ->willReturn('my_fake_handler_not_submitted_data') ->shouldBeCalled(); - $this->assertEquals('my_fake_handler_not_submitted_data', $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_request'])); + $this->assertSame('my_fake_handler_not_submitted_data', $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_request'])); } - public function testHandleDataThrowInvalidFormException() + public function testHandleDataThrowInvalidFormException(): void { $this->expectException(InvalidFormException::class); $this->expectExceptionMessage('Invalid form.'); @@ -408,7 +408,7 @@ public function testHandleDataThrowInvalidFormException() $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_key' => 'my_fake_request_value'], true); } - public function testHandleDataInvalid() + public function testHandleDataInvalid(): void { $this->formMock->getConfig() ->willReturn($this->formConfigMock) @@ -446,10 +446,10 @@ public function testHandleDataInvalid() ->willReturn('my_fake_handler_invalid_data') ->shouldBeCalled(); - $this->assertEquals('my_fake_handler_invalid_data', $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_key' => 'my_fake_request_value'], true)); + $this->assertSame('my_fake_handler_invalid_data', $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_key' => 'my_fake_request_value'], true)); } - public function testHandleDataThrowTransformationFailedFormException() + public function testHandleDataThrowTransformationFailedFormException(): void { $this->expectException(TransformationFailedFormException::class); $this->expectExceptionMessage('Transformation form failed.'); @@ -497,7 +497,7 @@ public function testHandleDataThrowTransformationFailedFormException() $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_key' => 'my_fake_request_value'], true); } - public function testHandleDataTransformationFailed() + public function testHandleDataTransformationFailed(): void { $this->formMock->getConfig() ->willReturn($this->formConfigMock) @@ -539,10 +539,10 @@ public function testHandleDataTransformationFailed() ->willReturn('my_fake_handler_transformation_failed_data') ->shouldBeCalled(); - $this->assertEquals('my_fake_handler_transformation_failed_data', $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_key' => 'my_fake_request_value'], true)); + $this->assertSame('my_fake_handler_transformation_failed_data', $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_key' => 'my_fake_request_value'], true)); } - public function testHandleData() + public function testHandleData(): void { $this->formMock->getConfig() ->willReturn($this->formConfigMock) @@ -580,6 +580,6 @@ public function testHandleData() ->willReturn('my_fake_form_data') ->shouldBeCalled(); - $this->assertEquals('my_fake_form_data', $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_key' => 'my_fake_request_value'], true)); + $this->assertSame('my_fake_form_data', $this->formHandler->handleData($this->formMock->reveal(), ['my_fake_key' => 'my_fake_request_value'], true)); } }