From 8d5912e31e9ff093e17aee9d251ef62ca892ddbc Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Sat, 12 Aug 2023 16:30:02 +0300 Subject: [PATCH 1/6] unit tests, psalm & phpcs --- .github/workflows/cs-tests.yml | 46 ++++ .github/workflows/static-analysis.yml | 46 ++++ .github/workflows/unit-tests.yml | 46 ++++ .gitignore | 2 + composer.json | 29 ++- phpcs.xml | 20 ++ phpunit.xml | 14 ++ psalm.xml | 17 ++ src/ConfigProvider.php | 31 +-- src/Factory/FormAbstractServiceFactory.php | 167 +++++++------ src/Factory/FormElementManagerFactory.php | 19 +- src/FormElementManager.php | 15 +- test/ConfigProviderTest.php | 46 ++++ .../FormAbstractServiceFactoryTest.php | 234 ++++++++++++++++++ .../Factory/FormElementManagerFactoryTest.php | 35 +++ 15 files changed, 638 insertions(+), 129 deletions(-) create mode 100644 .github/workflows/cs-tests.yml create mode 100644 .github/workflows/static-analysis.yml create mode 100644 .github/workflows/unit-tests.yml create mode 100644 phpcs.xml create mode 100644 phpunit.xml create mode 100644 psalm.xml create mode 100644 test/ConfigProviderTest.php create mode 100644 test/Factory/FormAbstractServiceFactoryTest.php create mode 100644 test/Factory/FormElementManagerFactoryTest.php diff --git a/.github/workflows/cs-tests.yml b/.github/workflows/cs-tests.yml new file mode 100644 index 0000000..3da9965 --- /dev/null +++ b/.github/workflows/cs-tests.yml @@ -0,0 +1,46 @@ +on: + - push + +name: Run phpcs checks + +jobs: + mutation: + name: PHP ${{ matrix.php }}-${{ matrix.os }} + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - "8.1" + - "8.2" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + tools: composer:v2, cs2pr + coverage: none + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + - name: Install dependencies with composer + run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run phpcs checks + run: vendor/bin/phpcs diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..377bcfb --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,46 @@ +on: + - push + +name: Run static analysis + +jobs: + mutation: + name: PHP ${{ matrix.php }}-${{ matrix.os }} + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - "8.1" + - "8.2" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + tools: composer:v2, cs2pr + coverage: none + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + - name: Install dependencies with composer + run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run static analysis + run: vendor/bin/psalm --no-cache --output-format=github --show-info=false --threads=4 diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..593bed9 --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,46 @@ +on: + - push + +name: Run PHPUnit tests + +jobs: + mutation: + name: PHP ${{ matrix.php }}-${{ matrix.os }} + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - "8.1" + - "8.2" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + tools: composer:v2, cs2pr + coverage: none + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + - name: Install dependencies with composer + run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run PHPUnit tests + run: vendor/bin/phpunit --colors=always diff --git a/.gitignore b/.gitignore index f3e6777..a76bc71 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ com_crashlytics_export_strings.xml crashlytics.properties crashlytics-build.properties fabric.properties +.phpcs-cache +.phpunit.result.cache \ No newline at end of file diff --git a/composer.json b/composer.json index a100d06..a1861d8 100644 --- a/composer.json +++ b/composer.json @@ -18,15 +18,15 @@ } ], "require": { - "php": "~7.4.0 || ~8.0.0 || ~8.1.0", + "php": "~8.1 || ~8.2", "laminas/laminas-servicemanager": "^3.10", - "laminas/laminas-form": "^3.1.1", - "laminas/laminas-log": "2.15.2" + "laminas/laminas-form": "^3.0" }, "require-dev": { - "phpunit/phpunit": "^9.1", "squizlabs/php_codesniffer": "^3.5", - "doctrine/annotations": "^1.10" + "phpunit/phpunit": "^10.2", + "laminas/laminas-coding-standard": "^2.5", + "vimeo/psalm": "^5.13" }, "autoload": { "psr-4": { @@ -35,7 +35,24 @@ }, "autoload-dev": { "psr-4": { - "DotTest\\Form\\": "tests" + "DotTest\\Form\\": "test/" } + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, + "scripts": { + "check": [ + "@cs-check", + "@test" + ], + "cs-check": "phpcs", + "cs-fix": "phpcbf", + "test": "phpunit --colors=always", + "test-coverage": "phpunit --colors=always --coverage-clover clover.xml", + "static-analysis": "psalm --shepherd --stats" } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..1efe663 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + src + test + + + + diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..75be41e --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,14 @@ + + + + + ./test + + + + + + ./src + + + diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..2015b2a --- /dev/null +++ b/psalm.xml @@ -0,0 +1,17 @@ + + + + + + + + + diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 99d7daf..3b3df90 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -1,23 +1,13 @@ $this->getDependenciesConfig(), 'view_helpers' => $this->getViewHelpersConfig(), - - 'dot_form' => [ + 'dot_form' => [ 'forms' => [], - ], ]; } @@ -39,21 +27,18 @@ public function getDependenciesConfig(): array 'abstract_factories' => [ FormAbstractServiceFactory::class, ], - 'aliases' => [ - 'Laminas\Form\Annotation\FormAnnotationBuilder' => 'FormAnnotationBuilder', - AnnotationBuilder::class => 'FormAnnotationBuilder', + 'aliases' => [ FormElementManager::class => 'FormElementManager', ], - 'factories' => [ - 'FormElementManager' => FormElementManagerFactory::class, - 'FormAnnotationBuilder' => AnnotationBuilderFactory::class, - ] + 'factories' => [ + FormElementManager::class => FormElementManagerFactory::class, + ], ]; } public function getViewHelpersConfig(): array { - $laminasFormConfigProvider = new \Laminas\Form\ConfigProvider(); + $laminasFormConfigProvider = new LaminasConfigProvider(); return $laminasFormConfigProvider->getViewHelperConfig(); } } diff --git a/src/Factory/FormAbstractServiceFactory.php b/src/Factory/FormAbstractServiceFactory.php index 80cb9db..03d123f 100644 --- a/src/Factory/FormAbstractServiceFactory.php +++ b/src/Factory/FormAbstractServiceFactory.php @@ -1,94 +1,101 @@ getConfig($container); + if (empty($config)) { + return false; + } + + return isset($config[$requestedName]) && is_array($config[$requestedName]) && ! empty($config[$requestedName]); } /** - * @param ContainerInterface $container * @param string $requestedName - * @param array|null $options - * @return \Laminas\Form\ElementInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): FormInterface { - $parts = explode('.', $requestedName); - - //merge configs if extends another form - $config = $this->getConfig($container); - $specificConfig = $config[$parts[1]]; + $config = $this->getConfig($container); + $config = $config[$requestedName]; + $factory = $this->getFormFactory($container); - do { - $extendsConfigKey = isset($specificConfig['extends']) && is_string($specificConfig['extends']) - ? trim($specificConfig['extends']) - : null; + $this->marshalInputFilter($config, $container, $factory); + return $factory->createForm($config); + } - unset($specificConfig['extends']); + protected function getConfig(ContainerInterface $container): array + { + if ($this->config !== null) { + return $this->config; + } - if (!is_null($extendsConfigKey) - && array_key_exists($extendsConfigKey, $config) - && is_array($config[$extendsConfigKey]) - ) { - $specificConfig = ArrayUtils::merge($config[$extendsConfigKey], $specificConfig); - } - } while ($extendsConfigKey != null); + if (! $container->has('config')) { + $this->config = []; + return $this->config; + } - $this->config[$parts[1]] = $specificConfig; + $config = $container->get('config'); + if (! isset($config[$this->configKey]) || ! is_array($config[$this->configKey])) { + $this->config = []; + return $this->config; + } - return parent::__invoke($container, $parts[1], $options); - } + $this->config = $config[$this->configKey]; - /** - * @param ContainerInterface $container - * @return array - */ - protected function getConfig(ContainerInterface $container): array - { - parent::getConfig($container); - if (!empty($this->config)) { + if (! empty($this->config)) { if (isset($this->config[$this->subConfigKey]) && is_array($this->config[$this->subConfigKey])) { $this->config = $this->config[$this->subConfigKey]; } @@ -97,28 +104,26 @@ protected function getConfig(ContainerInterface $container): array return $this->config; } - protected function getFormFactory(ContainerInterface $container): \Laminas\Form\Factory + protected function getFormFactory(ContainerInterface $container): FormFactory { - $formFactory = parent::getFormFactory($container); - if ($container->has('InputFilterManager')) { - $formFactory->setInputFilterFactory(new Factory($container->get('InputFilterManager'))); + if ($this->factory instanceof Factory) { + return $this->factory; + } + + $elements = null; + if ($container->has(FormElementManager::class)) { + $elements = $container->get(FormElementManager::class); } - return $formFactory; + $this->factory = new Factory($elements); + return $this->factory; } /** - * Marshal the input filter into the configuration - * - * If an input filter is specified: - * - if the InputFilterManager is present, checks if it's there; if so, - * retrieves it and resets the specification to the instance. - * - otherwise, pulls the input filter factory from the form factory, and - * attaches the FilterManager and ValidatorManager to it. - * - * @param array $config + * @throws NotFoundExceptionInterface + * @throws ContainerExceptionInterface */ - protected function marshalInputFilter(array &$config, ContainerInterface $container, \Laminas\Form\Factory $formFactory): void + protected function marshalInputFilter(array &$config, ContainerInterface $container, FormFactory $formFactory): void { if (! isset($config['input_filter'])) { return; @@ -128,11 +133,8 @@ protected function marshalInputFilter(array &$config, ContainerInterface $contai return; } - if ( - is_string($config['input_filter']) - && $container->has('InputFilterManager') - ) { - $inputFilters = $container->get('InputFilterManager'); + if (is_string($config['input_filter']) && $container->has(InputFilterPluginManager::class)) { + $inputFilters = $container->get(InputFilterPluginManager::class); if ($inputFilters->has($config['input_filter'])) { $config['input_filter'] = $inputFilters->get($config['input_filter']); return; @@ -140,7 +142,14 @@ protected function marshalInputFilter(array &$config, ContainerInterface $contai } $inputFilterFactory = $formFactory->getInputFilterFactory(); - $inputFilterFactory->getDefaultFilterChain()->setPluginManager($container->get('FilterManager')); - $inputFilterFactory->getDefaultValidatorChain()->setPluginManager($container->get('ValidatorManager')); + $filterChain = $inputFilterFactory->getDefaultFilterChain(); + $filterChain?->setPluginManager( + $container->get(FilterPluginManager::class) + ); + + $validatorChain = $inputFilterFactory->getDefaultValidatorChain(); + $validatorChain?->setPluginManager( + $container->get(ValidatorPluginManager::class) + ); } } diff --git a/src/Factory/FormElementManagerFactory.php b/src/Factory/FormElementManagerFactory.php index eac5cd2..d9687c3 100644 --- a/src/Factory/FormElementManagerFactory.php +++ b/src/Factory/FormElementManagerFactory.php @@ -1,24 +1,21 @@ get('config')['dot_form']['form_manager']); } diff --git a/src/FormElementManager.php b/src/FormElementManager.php index 2ff850e..0ee7e86 100644 --- a/src/FormElementManager.php +++ b/src/FormElementManager.php @@ -1,19 +1,14 @@ config = (new ConfigProvider())(); + } + + public function testHasDependencies(): void + { + $this->assertArrayHasKey('dependencies', $this->config); + } + + public function testDependenciesHasAbstractFactories(): void + { + $this->assertArrayHasKey('abstract_factories', $this->config['dependencies']); + $this->assertContains( + FormAbstractServiceFactory::class, + $this->config['dependencies']['abstract_factories'] + ); + } + + public function testDependenciesHasAliases(): void + { + $this->assertArrayHasKey('aliases', $this->config['dependencies']); + $this->assertArrayHasKey(FormElementManager::class, $this->config['dependencies']['aliases']); + } + + public function testDependenciesHasFactories(): void + { + $this->assertArrayHasKey('factories', $this->config['dependencies']); + $this->assertArrayHasKey(FormElementManager::class, $this->config['dependencies']['factories']); + } +} diff --git a/test/Factory/FormAbstractServiceFactoryTest.php b/test/Factory/FormAbstractServiceFactoryTest.php new file mode 100644 index 0000000..d8b3bb5 --- /dev/null +++ b/test/Factory/FormAbstractServiceFactoryTest.php @@ -0,0 +1,234 @@ +subject = new FormAbstractServiceFactory(); + $this->container = $this->createMock(ContainerInterface::class); + } + + public function testClassImplementsAbstractFactoryInterface(): void + { + $this->assertInstanceOf(AbstractFactoryInterface::class, $this->subject); + } + + public function testCanCreateReturnsFalseInvalidFormName(): void + { + $requestedName = 'invalidName'; + + $canCreate = $this->subject->canCreate($this->container, $requestedName); + $this->assertFalse($canCreate); + } + + public function testCanCreateReturnsFalseMissingPrefix(): void + { + $requestedName = 'dot.form'; + + $canCreate = $this->subject->canCreate($this->container, $requestedName); + $this->assertFalse($canCreate); + } + + public function testCanCreateReturnsFalseConfigProvided(): void + { + $requestedName = 'config'; + + $canCreate = $this->subject->canCreate($this->container, $requestedName); + $this->assertFalse($canCreate); + } + + /** + * @throws Exception + */ + public function testCanCreateReturnsFalseEmptyConfig(): void + { + $requestedName = 'dot-form.form'; + $container = $this->createMock(ContainerInterface::class); + + $container->method('get')->willReturn([]); + + $canCreate = $this->subject->canCreate($container, $requestedName); + $this->assertFalse($canCreate); + } + + /** + * @throws Exception + */ + public function testCanCreateReturnsFalseInvalidConfig(): void + { + $requestedName = 'dot-form.form'; + $container = $this->createMock(ContainerInterface::class); + + $container->expects($this->once())->method('has')->willReturn(true); + $container->method('get')->willReturn(['dot_form' => []]); + + $canCreate = $this->subject->canCreate($container, $requestedName); + $this->assertFalse($canCreate); + } + + /** + * @throws Exception + */ + public function testCanCreateReturnsTrue(): void + { + $requestedName = 'dot-form.form'; + $container = $this->createMock(ContainerInterface::class); + + $container->method('has')->willReturn(true); + $container->method('get')->willReturn(['dot_form' => ['dot-form.form' => ['form_name']]]); + + $canCreate = $this->subject->canCreate($container, $requestedName); + $this->assertTrue($canCreate); + } + + /** + * @throws ReflectionException + */ + public function testGetExistingConfig(): void + { + $config = ['form.dot_forms']; + $reflectionClass = new ReflectionClass(FormAbstractServiceFactory::class); + $reflectionClass->getProperty('config')->setValue($this->subject, $config); + + $result = $reflectionClass + ->getMethod('getConfig') + ->invoke($this->subject, $this->container); + + $this->assertSame($config, $result); + } + + /** + * @throws ReflectionException + */ + public function testConfigNotFound(): void + { + $this->container->expects($this->once())->method('has')->willReturn(false); + + $config = $this->callMethod($this->subject, 'getConfig', $this->container); + + $this->assertSame([], $config); + } + + /** + * @throws ReflectionException + */ + public function testGetInvalidConfig(): void + { + $this->container->expects($this->once())->method('has')->willReturn(true); + $this->container->expects($this->once())->method('get')->willReturn(['form']); + + $config = $this->callMethod($this->subject, 'getConfig', $this->container); + + $this->assertSame([], $config); + } + + /** + * @throws ReflectionException + */ + public function testGetValidConfig(): void + { + $config = ['dot_form' => ['form_name']]; + $this->container->expects($this->once())->method('has')->willReturn(true); + $this->container->expects($this->once())->method('get')->willReturn($config); + + $result = $this->callMethod($this->subject, 'getConfig', $this->container); + + $this->assertSame($config['dot_form'], $result); + } + + /** + * @throws ReflectionException + */ + public function testGetConfigSubKey(): void + { + $config = ['dot_form' => ['forms' => ['form_name']]]; + $this->container->expects($this->once())->method('has')->willReturn(true); + $this->container->expects($this->once())->method('get')->willReturn($config); + + $result = $this->callMethod($this->subject, 'getConfig', $this->container); + + $this->assertSame($config['dot_form']['forms'], $result); + } + + /** + * @throws Exception + */ + public function testGetExistingFormFactory(): void + { + $formFactory = $this->createMock(Factory::class); + $reflectionClass = new ReflectionClass(FormAbstractServiceFactory::class); + $reflectionClass->getProperty('factory')->setValue($this->subject, $formFactory); + + $result = $reflectionClass + ->getMethod('getFormFactory') + ->invoke($this->subject, $this->container); + + $this->assertInstanceOf(Factory::class, $result); + } + + /** + * @throws ReflectionException + */ + public function testGetFormFactoryFormElementManagerNotFound(): void + { + $this->container->expects($this->once())->method('has')->willReturn(false); + + $result = $this->callMethod($this->subject, 'getFormFactory', $this->container); + $reflectionClass = new ReflectionClass($result); + + $this->assertInstanceOf(Factory::class, $result); + $this->assertNull($reflectionClass->getProperty('formElementManager')->getValue($result)); + } + + /** + * @throws Exception + */ + public function testGetFormFactoryExistingFormElementManager(): void + { + $formElementManager = $this->createMock(FormElementManager::class); + $this->container->expects($this->once())->method('has')->willReturn(true); + $this->container->expects($this->once())->method('get')->willReturn($formElementManager); + + $result = $this->callMethod($this->subject, 'getFormFactory', $this->container); + $reflectionClass = new ReflectionClass($result); + + $this->assertInstanceOf(Factory::class, $result); + $this->assertInstanceOf( + FormElementManager::class, + $reflectionClass->getProperty('formElementManager')->getValue($result) + ); + } + + /** + * @throws ReflectionException + */ + private function callMethod(object $object, string $method, mixed ...$args): mixed + { + $reflectionClass = new ReflectionClass($object::class); + + return $reflectionClass->getMethod($method)->invoke($object, ...$args); + } +} diff --git a/test/Factory/FormElementManagerFactoryTest.php b/test/Factory/FormElementManagerFactoryTest.php new file mode 100644 index 0000000..e5482d5 --- /dev/null +++ b/test/Factory/FormElementManagerFactoryTest.php @@ -0,0 +1,35 @@ +createMock(ContainerInterface::class); + $container->expects($this->once())->method('get')->willReturn([ + 'dot_form' => [ + 'form_manager' => [], + ], + ]); + + $formElementManagerFactory = (new FormElementManagerFactory())($container); + + $this->assertInstanceOf(FormElementManager::class, $formElementManagerFactory); + } +} From 5945ac588cc3f7d296208a69013fc290d0325cd8 Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Mon, 14 Aug 2023 12:11:17 +0300 Subject: [PATCH 2/6] requsted changes --- .gitignore | 2 +- LICENSE.md | 2 +- README.md | 5 ++++- composer.json | 2 +- src/ConfigProvider.php | 1 + src/Factory/FormAbstractServiceFactory.php | 22 ++++++++++++++----- .../FormAbstractServiceFactoryTest.php | 20 +++++++++++++++++ 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index a76bc71..2f9a784 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,4 @@ crashlytics.properties crashlytics-build.properties fabric.properties .phpcs-cache -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache diff --git a/LICENSE.md b/LICENSE.md index 24e7185..6757d85 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Apidemia +Copyright (c) 2023 Apidemia Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index a782176..03e63e3 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ DotKernel form component extending and customizing [laminas-form](https://github.com/laminas/laminas-form) ![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-form) -![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-form/4.0.1) +![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-form/4.2.0) @@ -13,3 +13,6 @@ DotKernel form component extending and customizing [laminas-form](https://github [![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-form)](https://github.com/dotkernel/dot-form/stargazers) [![GitHub license](https://img.shields.io/github/license/dotkernel/dot-form)](https://github.com/dotkernel/dot-form/blob/4.0/LICENSE.md) +[![Build Static](https://github.com/dotkernel/dot-form/actions/workflows/static-analysis.yml/badge.svg?branch=4.0)](https://github.com/dotkernel/dot-form/actions/workflows/static-analysis.yml) + +[![SymfonyInsight](https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502/big.svg)](https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502) \ No newline at end of file diff --git a/composer.json b/composer.json index a1861d8..39f0c6d 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ }, "autoload": { "psr-4": { - "Dot\\Form\\": "src" + "Dot\\Form\\": "src/" } }, "autoload-dev": { diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 3b3df90..341a5dc 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -29,6 +29,7 @@ public function getDependenciesConfig(): array ], 'aliases' => [ FormElementManager::class => 'FormElementManager', + 'FormElementManager' => FormElementManagerFactory::class, ], 'factories' => [ FormElementManager::class => FormElementManagerFactory::class, diff --git a/src/Factory/FormAbstractServiceFactory.php b/src/Factory/FormAbstractServiceFactory.php index 03d123f..6d787bc 100644 --- a/src/Factory/FormAbstractServiceFactory.php +++ b/src/Factory/FormAbstractServiceFactory.php @@ -36,20 +36,22 @@ class FormAbstractServiceFactory implements AbstractFactoryInterface /** * @param string $requestedName + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function canCreate(ContainerInterface $container, $requestedName): bool { - $parts = explode('.', $requestedName); - if (count($parts) !== 2) { + // avoid infinite loops when looking up config + if ($requestedName === 'config') { return false; } - if ($parts[0] !== static::PREFIX) { + $parts = explode('.', $requestedName); + if (count($parts) !== 2) { return false; } - // avoid infinite loops when looking up config - if ($requestedName === 'config') { + if ($parts[0] !== static::PREFIX) { return false; } @@ -58,7 +60,7 @@ public function canCreate(ContainerInterface $container, $requestedName): bool return false; } - return isset($config[$requestedName]) && is_array($config[$requestedName]) && ! empty($config[$requestedName]); + return ! empty($config[$requestedName]) && is_array($config[$requestedName]); } /** @@ -76,6 +78,10 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ return $factory->createForm($config); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ protected function getConfig(ContainerInterface $container): array { if ($this->config !== null) { @@ -104,6 +110,10 @@ protected function getConfig(ContainerInterface $container): array return $this->config; } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ protected function getFormFactory(ContainerInterface $container): FormFactory { if ($this->factory instanceof Factory) { diff --git a/test/Factory/FormAbstractServiceFactoryTest.php b/test/Factory/FormAbstractServiceFactoryTest.php index d8b3bb5..b554816 100644 --- a/test/Factory/FormAbstractServiceFactoryTest.php +++ b/test/Factory/FormAbstractServiceFactoryTest.php @@ -12,7 +12,9 @@ use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use ReflectionClass; use ReflectionException; @@ -36,6 +38,10 @@ public function testClassImplementsAbstractFactoryInterface(): void $this->assertInstanceOf(AbstractFactoryInterface::class, $this->subject); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testCanCreateReturnsFalseInvalidFormName(): void { $requestedName = 'invalidName'; @@ -44,6 +50,10 @@ public function testCanCreateReturnsFalseInvalidFormName(): void $this->assertFalse($canCreate); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testCanCreateReturnsFalseMissingPrefix(): void { $requestedName = 'dot.form'; @@ -52,6 +62,10 @@ public function testCanCreateReturnsFalseMissingPrefix(): void $this->assertFalse($canCreate); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function testCanCreateReturnsFalseConfigProvided(): void { $requestedName = 'config'; @@ -61,6 +75,8 @@ public function testCanCreateReturnsFalseConfigProvided(): void } /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface * @throws Exception */ public function testCanCreateReturnsFalseEmptyConfig(): void @@ -75,6 +91,8 @@ public function testCanCreateReturnsFalseEmptyConfig(): void } /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface * @throws Exception */ public function testCanCreateReturnsFalseInvalidConfig(): void @@ -90,6 +108,8 @@ public function testCanCreateReturnsFalseInvalidConfig(): void } /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface * @throws Exception */ public function testCanCreateReturnsTrue(): void From 8cceb36d792d3284ab653d4138fc0fcce6a485e4 Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Mon, 14 Aug 2023 12:19:50 +0300 Subject: [PATCH 3/6] symfony insights updates --- CHANGELOG.md | 18 ++++++++++++++++++ OSSMETADATA | 2 +- README.md | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 774151f..8701d82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,21 @@ +## 4.2.0 - 2023-08-14 + +### Changed +* Added unit tests & removed laminas/laminas-log dependency + +### Added +* Unit Tests + +### Deprecated +* Nothing + +### Removed +* laminas/laminas-log dependency + +### Fixed +* Decoupled packages from laminas/laminas-log + + ## 4.0.1 - 2022-05-31 ### Changed diff --git a/OSSMETADATA b/OSSMETADATA index b96d4a4..58d43b8 100644 --- a/OSSMETADATA +++ b/OSSMETADATA @@ -1 +1 @@ -osslifecycle=active +osslifecycle=maintained diff --git a/README.md b/README.md index 03e63e3..43d470b 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,4 @@ DotKernel form component extending and customizing [laminas-form](https://github [![Build Static](https://github.com/dotkernel/dot-form/actions/workflows/static-analysis.yml/badge.svg?branch=4.0)](https://github.com/dotkernel/dot-form/actions/workflows/static-analysis.yml) -[![SymfonyInsight](https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502/big.svg)](https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502) \ No newline at end of file +[![SymfonyInsight](https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502/big.svg)](https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502) From f0272a8ba7ed28bae50f620098022a7731b61e61 Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Mon, 14 Aug 2023 18:38:54 +0300 Subject: [PATCH 4/6] requested changes --- README.md | 5 +---- composer.json | 1 - src/ConfigProvider.php | 3 +-- test/Factory/FormAbstractServiceFactoryTest.php | 2 ++ 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 43d470b..34e893b 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,6 @@ DotKernel form component extending and customizing [laminas-form](https://github ![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-form) ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-form/4.2.0) - - - [![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-form)](https://github.com/dotkernel/dot-form/issues) [![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-form)](https://github.com/dotkernel/dot-form/network) [![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-form)](https://github.com/dotkernel/dot-form/stargazers) @@ -15,4 +12,4 @@ DotKernel form component extending and customizing [laminas-form](https://github [![Build Static](https://github.com/dotkernel/dot-form/actions/workflows/static-analysis.yml/badge.svg?branch=4.0)](https://github.com/dotkernel/dot-form/actions/workflows/static-analysis.yml) -[![SymfonyInsight](https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502/big.svg)](https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502) +[![SymfonyInsight](https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502/big.svg)](https://insight.symfony.com/projects/370a5200-2e49-47da-9988-8e1de8f49502) diff --git a/composer.json b/composer.json index 39f0c6d..e4d2e33 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,6 @@ "laminas/laminas-form": "^3.0" }, "require-dev": { - "squizlabs/php_codesniffer": "^3.5", "phpunit/phpunit": "^10.2", "laminas/laminas-coding-standard": "^2.5", "vimeo/psalm": "^5.13" diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 341a5dc..f0a7509 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -29,10 +29,9 @@ public function getDependenciesConfig(): array ], 'aliases' => [ FormElementManager::class => 'FormElementManager', - 'FormElementManager' => FormElementManagerFactory::class, ], 'factories' => [ - FormElementManager::class => FormElementManagerFactory::class, + 'FormElementManager' => FormElementManagerFactory::class, ], ]; } diff --git a/test/Factory/FormAbstractServiceFactoryTest.php b/test/Factory/FormAbstractServiceFactoryTest.php index b554816..927a0ff 100644 --- a/test/Factory/FormAbstractServiceFactoryTest.php +++ b/test/Factory/FormAbstractServiceFactoryTest.php @@ -195,6 +195,7 @@ public function testGetConfigSubKey(): void /** * @throws Exception + * @throws ReflectionException */ public function testGetExistingFormFactory(): void { @@ -225,6 +226,7 @@ public function testGetFormFactoryFormElementManagerNotFound(): void /** * @throws Exception + * @throws ReflectionException */ public function testGetFormFactoryExistingFormElementManager(): void { From d896fccc3129d811300c486539699a82fa29a0e1 Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Mon, 14 Aug 2023 18:41:20 +0300 Subject: [PATCH 5/6] fix failing unit test --- test/ConfigProviderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index 8a8697f..09a889d 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -41,6 +41,6 @@ public function testDependenciesHasAliases(): void public function testDependenciesHasFactories(): void { $this->assertArrayHasKey('factories', $this->config['dependencies']); - $this->assertArrayHasKey(FormElementManager::class, $this->config['dependencies']['factories']); + $this->assertArrayHasKey('FormElementManager', $this->config['dependencies']['factories']); } } From a51e3d7852ad310049ae96169acb4b8cf4362c55 Mon Sep 17 00:00:00 2001 From: MarioRadu Date: Mon, 14 Aug 2023 18:43:53 +0300 Subject: [PATCH 6/6] cs fix --- src/ConfigProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index f0a7509..c3e6c25 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -31,7 +31,7 @@ public function getDependenciesConfig(): array FormElementManager::class => 'FormElementManager', ], 'factories' => [ - 'FormElementManager' => FormElementManagerFactory::class, + 'FormElementManager' => FormElementManagerFactory::class, ], ]; }