From ad6a91e0abc1d78f31547b583bb229fa9963bd67 Mon Sep 17 00:00:00 2001 From: Maelan LE BORGNE Date: Thu, 2 Nov 2023 17:59:44 +0100 Subject: [PATCH] Add CI --- .github/workflows/ci.yaml | 62 +++++++++++++++++++ .gitignore | 1 + .php-cs-fixer.dist.php | 25 ++++++++ DependencyInjection/Configuration.php | 3 - .../IsometriksSpamExtension.php | 15 ++--- .../HoneypotValidationListener.php | 11 ++-- .../TimedSpamValidationListener.php | 21 +++---- .../Provider/SessionTimedSpamProvider.php | 13 ++-- .../Provider/TimedSpamProviderInterface.php | 15 +---- .../Spam/Type/FormTypeHoneypotExtension.php | 22 +++---- .../Spam/Type/FormTypeTimedSpamExtension.php | 14 ++--- composer.json | 2 + phpunit.xml.dist | 3 +- tests/HoneyPotTest.php | 18 +++--- tests/TimedSpamTest.php | 23 ++++--- tests/fixtures/TestKernel.php | 4 +- 16 files changed, 154 insertions(+), 98 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 .php-cs-fixer.dist.php diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..a5604ee --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,62 @@ +name: CI + +on: + pull_request: ~ + push: + branches: + - main + release: + types: + - created + schedule: + - cron: '43 21 * * *' + +jobs: + test: + name: "Build and Test - PHP ${{ matrix.php }} Symfony:${{ matrix.symfony-version }} ${{ matrix.deps }}" + runs-on: ubuntu-latest + env: + SYMFONY_REQUIRE: ${{ matrix.symfony-version }} + strategy: + matrix: + include: + - symfony-version: 5.4 + php: 7.4 + - symfony-version: 6.4 + php: 8.1 + - symfony-version: 7.0 + php: 8.2 + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + coverage: none + php-version: ${{ matrix.php }} + + - name: Install dependencies + run: composer install + + - name: Run tests + run: | + ./vendor/bin/simple-phpunit + + php-cs-fixer: + name: PHP CS Fixer + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 7.4 + tools: php-cs-fixer + + - name: Run PHP-CS-Fixer + run: + php-cs-fixer fix --dry-run --diff diff --git a/.gitignore b/.gitignore index 5a2d600..521de49 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /composer.lock /vendor /.phpunit.result.cache +/.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..8fcef59 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,25 @@ +in($dirs); + +return (new \PhpCsFixer\Config()) + ->setRules(array( + '@Symfony' => true, + '@Symfony:risky' => true, + 'phpdoc_to_comment' => false, + )) + ->setRiskyAllowed(true) + ->setFinder($finder); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 2aeafff..c686091 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -12,9 +12,6 @@ */ class Configuration implements ConfigurationInterface { - /** - * {@inheritDoc} - */ public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('isometriks_spam'); diff --git a/DependencyInjection/IsometriksSpamExtension.php b/DependencyInjection/IsometriksSpamExtension.php index 8e9a2a7..2d3aa73 100644 --- a/DependencyInjection/IsometriksSpamExtension.php +++ b/DependencyInjection/IsometriksSpamExtension.php @@ -2,10 +2,10 @@ namespace Isometriks\Bundle\SpamBundle\DependencyInjection; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\HttpKernel\DependencyInjection\Extension; /** * This is the class that loads and manages your bundle configuration. @@ -14,9 +14,6 @@ */ class IsometriksSpamExtension extends Extension { - /** - * {@inheritDoc} - */ public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); @@ -36,12 +33,12 @@ private function processTimedConfig(array $config, ContainerBuilder $container, $loader->load('timed.xml'); $definition = $container->getDefinition('isometriks_spam.form.extension.type.timed_spam'); - $definition->addArgument(array( + $definition->addArgument([ 'min' => $config['min'], 'max' => $config['max'], 'global' => $config['global'], 'message' => $config['message'], - )); + ]); } private function processHoneypotConfig(array $config, ContainerBuilder $container, XmlFileLoader $loader): void @@ -53,12 +50,12 @@ private function processHoneypotConfig(array $config, ContainerBuilder $containe $loader->load('honeypot.xml'); $definition = $container->getDefinition('isometriks_spam.form.extension.type.honeypot'); - $definition->addArgument(array( + $definition->addArgument([ 'field' => $config['field'], 'use_class' => $config['use_class'], 'hide_class' => $config['hide_class'], 'global' => $config['global'], 'message' => $config['message'], - )); + ]); } } diff --git a/Form/Extension/Spam/EventListener/HoneypotValidationListener.php b/Form/Extension/Spam/EventListener/HoneypotValidationListener.php index 564d69c..9f50329 100644 --- a/Form/Extension/Spam/EventListener/HoneypotValidationListener.php +++ b/Form/Extension/Spam/EventListener/HoneypotValidationListener.php @@ -20,8 +20,7 @@ public function __construct( string $translationDomain, string $fieldName, string $errorMessage - ) - { + ) { $this->translator = $translator; $this->translationDomain = $translationDomain; $this->fieldName = $fieldName; @@ -38,13 +37,13 @@ public function preSubmit(FormEvent $event): void $errorMessage = $this->errorMessage; if (null !== $this->translator) { - $errorMessage = $this->translator->trans($errorMessage, array(), $this->translationDomain); + $errorMessage = $this->translator->trans($errorMessage, [], $this->translationDomain); } $form->addError(new FormError($errorMessage)); } - if (is_array($data)) { + if (\is_array($data)) { unset($data[$this->fieldName]); } } @@ -54,8 +53,8 @@ public function preSubmit(FormEvent $event): void public static function getSubscribedEvents(): array { - return array( + return [ FormEvents::PRE_SUBMIT => 'preSubmit', - ); + ]; } } diff --git a/Form/Extension/Spam/EventListener/TimedSpamValidationListener.php b/Form/Extension/Spam/EventListener/TimedSpamValidationListener.php index 6fbab6b..47e0b7c 100644 --- a/Form/Extension/Spam/EventListener/TimedSpamValidationListener.php +++ b/Form/Extension/Spam/EventListener/TimedSpamValidationListener.php @@ -23,8 +23,7 @@ public function __construct( string $translationDomain, string $errorMessage, array $options - ) - { + ) { $this->timeProvider = $timeProvider; $this->translator = $translator; $this->translationDomain = $translationDomain; @@ -36,13 +35,13 @@ public function preSubmit(FormEvent $event): void { $form = $event->getForm(); - if ($form->isRoot() && - $form->getConfig()->getOption('compound') && - !$this->timeProvider->isFormTimeValid($form->getName(), $this->options)) { + if ($form->isRoot() + && $form->getConfig()->getOption('compound') + && !$this->timeProvider->isFormTimeValid($form->getName(), $this->options)) { $errorMessage = $this->errorMessage; if (null !== $this->translator) { - $errorMessage = $this->translator->trans($errorMessage, array(), $this->translationDomain); + $errorMessage = $this->translator->trans($errorMessage, [], $this->translationDomain); } $form->addError(new FormError($errorMessage)); @@ -58,9 +57,9 @@ public function postSubmit(FormEvent $event): void { $form = $event->getForm(); - if ($form->isRoot() && - $form->getConfig()->getOption('compound') && - !$form->isValid()) { + if ($form->isRoot() + && $form->getConfig()->getOption('compound') + && !$form->isValid()) { // If the form has errors, set the time again $this->timeProvider->generateFormTime($form->getName()); } @@ -68,9 +67,9 @@ public function postSubmit(FormEvent $event): void public static function getSubscribedEvents(): array { - return array( + return [ FormEvents::PRE_SUBMIT => 'preSubmit', FormEvents::POST_SUBMIT => 'postSubmit', - ); + ]; } } diff --git a/Form/Extension/Spam/Provider/SessionTimedSpamProvider.php b/Form/Extension/Spam/Provider/SessionTimedSpamProvider.php index d2ad6ce..f4d83bb 100644 --- a/Form/Extension/Spam/Provider/SessionTimedSpamProvider.php +++ b/Form/Extension/Spam/Provider/SessionTimedSpamProvider.php @@ -2,7 +2,6 @@ namespace Isometriks\Bundle\SpamBundle\Form\Extension\Spam\Provider; -use DateTime; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\SessionInterface; @@ -15,9 +14,9 @@ public function __construct(RequestStack $requestStack) $this->requestStack = $requestStack; } - public function generateFormTime(string $name): DateTime + public function generateFormTime(string $name): \DateTime { - $startTime = new DateTime(); + $startTime = new \DateTime(); $key = $this->getSessionKey($name); $this->getSession()->set($key, $startTime); @@ -33,16 +32,16 @@ public function isFormTimeValid(string $name, array $options): bool /* * No value stored, so this can't be valid or session expired. */ - if ($startTime === false) { + if (false === $startTime) { return false; } - $currentTime = new DateTime(); + $currentTime = new \DateTime(); /* * Check against a minimum time */ - if ($options['min'] !== null) { + if (null !== $options['min']) { $minTime = clone $startTime; $minTime->modify(sprintf('+%d seconds', $options['min'])); @@ -52,7 +51,7 @@ public function isFormTimeValid(string $name, array $options): bool /* * Check against a maximum time */ - if ($options['max'] !== null) { + if (null !== $options['max']) { $maxTime = clone $startTime; $maxTime->modify(sprintf('+%d seconds', $options['max'])); diff --git a/Form/Extension/Spam/Provider/TimedSpamProviderInterface.php b/Form/Extension/Spam/Provider/TimedSpamProviderInterface.php index 1c757c4..9fa08bc 100644 --- a/Form/Extension/Spam/Provider/TimedSpamProviderInterface.php +++ b/Form/Extension/Spam/Provider/TimedSpamProviderInterface.php @@ -2,23 +2,15 @@ namespace Isometriks\Bundle\SpamBundle\Form\Extension\Spam\Provider; -use DateTime; - interface TimedSpamProviderInterface { /** * Generate form time. - * - * @param string $name - * - * @return DateTime */ - public function generateFormTime(string $name): DateTime; + public function generateFormTime(string $name): \DateTime; /** * Check if form has time. - * - * @param string $name */ public function hasFormTime(string $name): bool; @@ -31,17 +23,12 @@ public function getFormTime(string $name); /** * Removes a form time name. - * - * @param string $name */ public function removeFormTime(string $name): void; /** * Check if form time is valid. * - * @param string $name - * @param array $options - * * @return bool $valid */ public function isFormTimeValid(string $name, array $options): bool; diff --git a/Form/Extension/Spam/Type/FormTypeHoneypotExtension.php b/Form/Extension/Spam/Type/FormTypeHoneypotExtension.php index 22dbda4..0512a13 100644 --- a/Form/Extension/Spam/Type/FormTypeHoneypotExtension.php +++ b/Form/Extension/Spam/Type/FormTypeHoneypotExtension.php @@ -22,8 +22,7 @@ public function __construct( ?TranslatorInterface $translator, string $translationDomain, array $defaults - ) - { + ) { $this->translator = $translator; $this->translationDomain = $translationDomain; $this->defaults = $defaults; @@ -52,20 +51,20 @@ public function finishView(FormView $view, FormInterface $form, array $options): throw new \RuntimeException(sprintf('Honeypot field "%s" is already in use.', $options['honeypot_field'])); } - $formOptions = array( + $formOptions = [ 'mapped' => false, 'label' => false, 'required' => false, - ); + ]; if ($options['honeypot_use_class']) { - $formOptions['attr'] = array( + $formOptions['attr'] = [ 'class' => $options['honeypot_hide_class'], - ); + ]; } else { - $formOptions['attr'] = array( + $formOptions['attr'] = [ 'style' => 'display:none', - ); + ]; } $factory = $form->getConfig()->getAttribute('honeypot_factory'); @@ -77,18 +76,15 @@ public function finishView(FormView $view, FormInterface $form, array $options): public function configureOptions(OptionsResolver $resolver): void { - $resolver->setDefaults(array( + $resolver->setDefaults([ 'honeypot' => $this->defaults['global'], 'honeypot_use_class' => $this->defaults['use_class'], 'honeypot_hide_class' => $this->defaults['hide_class'], 'honeypot_field' => $this->defaults['field'], 'honeypot_message' => $this->defaults['message'], - )); + ]); } - /** - * @inheritdoc - */ public static function getExtendedTypes(): iterable { return [FormType::class]; diff --git a/Form/Extension/Spam/Type/FormTypeTimedSpamExtension.php b/Form/Extension/Spam/Type/FormTypeTimedSpamExtension.php index ecab449..41d46c6 100644 --- a/Form/Extension/Spam/Type/FormTypeTimedSpamExtension.php +++ b/Form/Extension/Spam/Type/FormTypeTimedSpamExtension.php @@ -24,8 +24,7 @@ public function __construct( ?TranslatorInterface $translator, string $translationDomain, array $defaults - ) - { + ) { $this->timeProvider = $timeProvider; $this->translator = $translator; $this->translationDomain = $translationDomain; @@ -38,10 +37,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void return; } - $providerOptions = array( + $providerOptions = [ 'min' => $options['timed_spam_min'], 'max' => $options['timed_spam_max'], - ); + ]; $builder ->addEventSubscriber(new TimedSpamValidationListener( @@ -62,17 +61,14 @@ public function finishView(FormView $view, FormInterface $form, array $options): public function configureOptions(OptionsResolver $resolver): void { - $resolver->setDefaults(array( + $resolver->setDefaults([ 'timed_spam' => $this->defaults['global'], 'timed_spam_min' => $this->defaults['min'], 'timed_spam_max' => $this->defaults['max'], 'timed_spam_message' => $this->defaults['message'], - )); + ]); } - /** - * @inheritdoc - */ public static function getExtendedTypes(): iterable { return [FormType::class]; diff --git a/composer.json b/composer.json index 74d5133..c696cb9 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,8 @@ "symfony/framework-bundle": "^5.4 || ^6.0 || ^7.0" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.4", + "phpunit/phpunit": "^9.1.1 || ^9.5", "symfony/phpunit-bridge": "^5.4 || ^6.0 || ^7.0", "symfony/yaml": "^5.4 || ^6.0 || ^7.0" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d1b55ab..a157a39 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,10 +3,8 @@ @@ -16,6 +14,7 @@ + diff --git a/tests/HoneyPotTest.php b/tests/HoneyPotTest.php index 9570bd6..395cf41 100644 --- a/tests/HoneyPotTest.php +++ b/tests/HoneyPotTest.php @@ -20,7 +20,7 @@ public function testValidationShouldPass($data, $formOptions): void } $form = $formBuilder->getForm(); $form->submit($data); - $this->assertEquals(true, $form->isValid()); + $this->assertTrue($form->isValid()); $this->assertEmpty($form->getErrors()); } @@ -34,7 +34,7 @@ public function provideValidationShouldPass() ], [ 'honeypot' => false, - ] + ], ], 'no_honeypot_with_field' => [ [ @@ -44,7 +44,7 @@ public function provideValidationShouldPass() [ 'honeypot' => false, 'honeypot_field' => 'phone', - ] + ], ], 'honeypot_on_phone' => [ [ @@ -54,7 +54,7 @@ public function provideValidationShouldPass() [ 'honeypot' => true, 'honeypot_field' => 'phone', - ] + ], ], 'honeypot_on_email' => [ [ @@ -64,8 +64,8 @@ public function provideValidationShouldPass() [ 'honeypot' => true, 'honeypot_field' => 'email', - ] - ] + ], + ], ]; } @@ -98,7 +98,7 @@ public function provideValidationShouldFail() [ 'honeypot' => true, 'honeypot_field' => 'phone', - ] + ], ], 'honeypot_field_not_submitted' => [ [ @@ -118,7 +118,7 @@ public function provideValidationShouldFail() [ 'honeypot' => true, 'honeypot_field' => 'phone', - ] + ], ], 'honeypot_with_error_message' => [ [ @@ -130,7 +130,7 @@ public function provideValidationShouldFail() 'honeypot_field' => 'email', 'honeypot_message' => 'We detected a strange behaviour in your form submission. Please try again.', ], - 'We detected a strange behaviour in your form submission. Please try again.' + 'We detected a strange behaviour in your form submission. Please try again.', ], ]; } diff --git a/tests/TimedSpamTest.php b/tests/TimedSpamTest.php index 50b70e1..956886e 100644 --- a/tests/TimedSpamTest.php +++ b/tests/TimedSpamTest.php @@ -6,7 +6,6 @@ use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\SessionFactory; use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorageFactory; class TimedSpamTest extends KernelTestCase @@ -49,7 +48,7 @@ public function provideValidationShouldPass() ], [ 'timed_spam' => false, - ] + ], ], 'no_timed_spam_with_options' => [ [ @@ -61,7 +60,7 @@ public function provideValidationShouldPass() 'timed_spam_min' => 3, 'timed_spam_max' => 40, 'timed_spam_message' => 'Please wait 3 seconds before submitting', - ] + ], ], 'timed_spam_min_0' => [ [ @@ -71,7 +70,7 @@ public function provideValidationShouldPass() [ 'timed_spam' => true, 'timed_spam_min' => 0, - ] + ], ], 'timed_spam_min_1' => [ [ @@ -82,7 +81,7 @@ public function provideValidationShouldPass() 'timed_spam' => true, 'timed_spam_min' => 1, ], - 1 + 1, ], 'timed_spam_min_0_max_1' => [ [ @@ -93,8 +92,8 @@ public function provideValidationShouldPass() 'timed_spam' => true, 'timed_spam_min' => 0, 'timed_spam_max' => 1, - ] - ] + ], + ], ]; } @@ -129,7 +128,7 @@ public function provideValidationShouldFail(): array [ 'timed_spam' => true, 'timed_spam_min' => 1, - ] + ], ], 'timed_spam_max_0' => [ [ @@ -139,7 +138,7 @@ public function provideValidationShouldFail(): array [ 'timed_spam' => true, 'timed_spam_max' => 0, - ] + ], ], 'timed_spam_max_1' => [ [ @@ -150,7 +149,7 @@ public function provideValidationShouldFail(): array 'timed_spam' => true, 'timed_spam_max' => 1, ], - 1 + 1, ], // Maybe this should rather throw an error during option resolution ? 'timed_spam_min_2_max_1' => [ @@ -162,7 +161,7 @@ public function provideValidationShouldFail(): array 'timed_spam' => true, 'timed_spam_min' => 2, 'timed_spam_max' => 1, - ] + ], ], 'timed_spam_with_error_message' => [ [ @@ -175,7 +174,7 @@ public function provideValidationShouldFail(): array 'timed_spam_message' => 'Please wait 1 second before submitting', ], 0, - 'Please wait 1 second before submitting' + 'Please wait 1 second before submitting', ], ]; } diff --git a/tests/fixtures/TestKernel.php b/tests/fixtures/TestKernel.php index b005ee4..db4437c 100644 --- a/tests/fixtures/TestKernel.php +++ b/tests/fixtures/TestKernel.php @@ -23,7 +23,7 @@ public function registerBundles(): iterable { return [ new FrameworkBundle(), - new IsometriksSpamBundle() + new IsometriksSpamBundle(), ]; } @@ -42,12 +42,10 @@ protected function configureContainer(ContainerConfigurator $container, LoaderIn 'secret' => 'foo', 'test' => true, 'http_method_override' => true, - 'handle_all_throwables' => true, 'php_errors' => [ 'log' => true, ], ]); - } public function getCacheDir(): string