From 7ae5af2ca1a5f2ca7de95cf998021ec890f0f320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Fri, 8 Oct 2021 10:36:50 +0200 Subject: [PATCH 1/2] Updates the factory for different form view helpers to avoid code duplications MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Frank Brückner --- config/module.config.php | 8 ++-- .../Helper/Factory/FormCollectionFactory.php | 40 ----------------- .../Helper/Factory/FormElementFactory.php | 44 ------------------- .../Form/View/Helper/Factory/FormFactory.php | 40 ----------------- .../Factory/FormModuleOptionsFactory.php | 21 +++++++++ .../View/Helper/Factory/FormRowFactory.php | 40 ----------------- 6 files changed, 25 insertions(+), 168 deletions(-) delete mode 100644 src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php delete mode 100644 src/TwbsHelper/Form/View/Helper/Factory/FormElementFactory.php delete mode 100644 src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php create mode 100644 src/TwbsHelper/Form/View/Helper/Factory/FormModuleOptionsFactory.php delete mode 100644 src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php diff --git a/config/module.config.php b/config/module.config.php index 0cee0638e..a88bd093a 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -104,10 +104,10 @@ ], 'factories' => [ - \TwbsHelper\Form\View\Helper\Form::class => \TwbsHelper\Form\View\Helper\Factory\FormFactory::class, - \TwbsHelper\Form\View\Helper\FormElement::class => \TwbsHelper\Form\View\Helper\Factory\FormElementFactory::class, - \TwbsHelper\Form\View\Helper\FormRow::class => \TwbsHelper\Form\View\Helper\Factory\FormRowFactory::class, - \TwbsHelper\Form\View\Helper\FormCollection::class => \TwbsHelper\Form\View\Helper\Factory\FormCollectionFactory::class, + \TwbsHelper\Form\View\Helper\Form::class => \TwbsHelper\Form\View\Helper\Factory\FormModuleOptionsFactory::class, + \TwbsHelper\Form\View\Helper\FormElement::class => \TwbsHelper\Form\View\Helper\Factory\FormModuleOptionsFactory::class, + \TwbsHelper\Form\View\Helper\FormRow::class => \TwbsHelper\Form\View\Helper\Factory\FormModuleOptionsFactory::class, + \TwbsHelper\Form\View\Helper\FormCollection::class => \TwbsHelper\Form\View\Helper\Factory\FormModuleOptionsFactory::class, ], 'aliases' => [ diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php deleted file mode 100644 index f034044d1..000000000 --- a/src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php +++ /dev/null @@ -1,40 +0,0 @@ -= 2.2) -> proxy to __invoke - * - * @param \Laminas\ServiceManager\ServiceLocatorInterface $oServiceLocator - * @param mixed $sCanonicalName - * @param mixed $sRequestedName - * @return \TwbsHelper\Form\View\Helper\FormCollection - */ - public function createService( - \Laminas\ServiceManager\ServiceLocatorInterface $oServiceLocator, - $sCanonicalName = null, - $sRequestedName = null - ): \TwbsHelper\Form\View\Helper\FormCollection { - return $this($oServiceLocator, $sRequestedName); - } - - /** - * Compatibility with Laminas and ZF3 - * - * @param \Interop\Container\ContainerInterface $oContainer - * @param mixed $sRequestedName - * @param array $aOptions - * @return \TwbsHelper\Form\View\Helper\FormCollection - */ - public function __invoke( - \Interop\Container\ContainerInterface $oContainer, - $sRequestedName, - array $aOptions = null - ): \TwbsHelper\Form\View\Helper\FormCollection { - $oOptions = $oContainer->get(\TwbsHelper\Options\ModuleOptions::class); - - return new \TwbsHelper\Form\View\Helper\FormCollection($oOptions); - } -} diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormElementFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormElementFactory.php deleted file mode 100644 index f3a7b500c..000000000 --- a/src/TwbsHelper/Form/View/Helper/Factory/FormElementFactory.php +++ /dev/null @@ -1,44 +0,0 @@ -= 2.2) -> proxy to __invoke - * - * @param \Laminas\ServiceManager\ServiceLocatorInterface $oServiceLocator - * @param mixed $sCanonicalName - * @param mixed $sRequestedName - * @return \TwbsHelper\Form\View\Helper\FormElement - */ - public function createService( - \Laminas\ServiceManager\ServiceLocatorInterface $oServiceLocator, - $sCanonicalName = null, - $sRequestedName = null - ): \TwbsHelper\Form\View\Helper\FormElement { - return $this($oServiceLocator, $sRequestedName); - } - - /** - * Compatibility with Laminas and ZF3 - * - * @param \Interop\Container\ContainerInterface $oContainer - * @param mixed $sRequestedName - * @param array $aOptions - * @return \TwbsHelper\Form\View\Helper\FormElement - */ - public function __invoke( - \Interop\Container\ContainerInterface $oContainer, - $sRequestedName, - array $aOptions = null - ): \TwbsHelper\Form\View\Helper\FormElement { - $oOptions = $oContainer->get(\TwbsHelper\Options\ModuleOptions::class); - - return new \TwbsHelper\Form\View\Helper\FormElement($oOptions); - } -} diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php deleted file mode 100644 index a713c6c22..000000000 --- a/src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php +++ /dev/null @@ -1,40 +0,0 @@ -= 2.2) -> proxy to __invoke - * - * @param \Laminas\ServiceManager\ServiceLocatorInterface $oServiceLocator - * @param mixed $sCanonicalName - * @param mixed $sRequestedName - * @return \TwbsHelper\Form\View\Helper\Form - */ - public function createService( - \Laminas\ServiceManager\ServiceLocatorInterface $oServiceLocator, - $sCanonicalName = null, - $sRequestedName = null - ): \TwbsHelper\Form\View\Helper\Form { - return $this($oServiceLocator, $sRequestedName); - } - - /** - * Compatibility with Laminas and ZF3 - * - * @param \Interop\Container\ContainerInterface $oContainer - * @param mixed $sRequestedName - * @param array $aOptions - * @return \TwbsHelper\Form\View\Helper\Form - */ - public function __invoke( - \Interop\Container\ContainerInterface $oContainer, - $sRequestedName, - array $aOptions = null - ): \TwbsHelper\Form\View\Helper\Form { - $oOptions = $oContainer->get(\TwbsHelper\Options\ModuleOptions::class); - - return new \TwbsHelper\Form\View\Helper\Form($oOptions); - } -} diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormModuleOptionsFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormModuleOptionsFactory.php new file mode 100644 index 000000000..f1f6bd190 --- /dev/null +++ b/src/TwbsHelper/Form/View/Helper/Factory/FormModuleOptionsFactory.php @@ -0,0 +1,21 @@ +get(ModuleOptions::class); + + return new $requestedName($moduleOptions); + } +} diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php deleted file mode 100644 index 85134ef84..000000000 --- a/src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php +++ /dev/null @@ -1,40 +0,0 @@ -= 2.2) -> proxy to __invoke - * - * @param \Laminas\ServiceManager\ServiceLocatorInterface $oServiceLocator - * @param mixed $sCanonicalName - * @param mixed $sRequestedName - * @return \TwbsHelper\Form\View\Helper\FormRow - */ - public function createService( - \Laminas\ServiceManager\ServiceLocatorInterface $oServiceLocator, - $sCanonicalName = null, - $sRequestedName = null - ): \TwbsHelper\Form\View\Helper\FormRow { - return $this($oServiceLocator, $sRequestedName); - } - - /** - * Compatibility with Laminas and ZF3 - * - * @param \Interop\Container\ContainerInterface $oContainer - * @param mixed $sRequestedName - * @param array $aOptions - * @return \TwbsHelper\Form\View\Helper\FormRow - */ - public function __invoke( - \Interop\Container\ContainerInterface $oContainer, - $sRequestedName, - array $aOptions = null - ): \TwbsHelper\Form\View\Helper\FormRow { - $oOptions = $oContainer->get(\TwbsHelper\Options\ModuleOptions::class); - - return new \TwbsHelper\Form\View\Helper\FormRow($oOptions); - } -} From a9b9dcb5b51017f1df74821f683a571cea038514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Fri, 8 Oct 2021 11:06:28 +0200 Subject: [PATCH 2/2] Updates the factory for module options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Frank Brückner --- .../Options/Factory/ModuleOptionsFactory.php | 54 +++---------------- .../Helper/Factory/FormElementFactoryTest.php | 49 +++++++++++++---- .../Factory/ModuleOptionsFactoryTest.php | 25 --------- 3 files changed, 46 insertions(+), 82 deletions(-) delete mode 100644 tests/TestSuite/TwbsHelper/Options/Factory/ModuleOptionsFactoryTest.php diff --git a/src/TwbsHelper/Options/Factory/ModuleOptionsFactory.php b/src/TwbsHelper/Options/Factory/ModuleOptionsFactory.php index 345da8143..2d99f5ebe 100644 --- a/src/TwbsHelper/Options/Factory/ModuleOptionsFactory.php +++ b/src/TwbsHelper/Options/Factory/ModuleOptionsFactory.php @@ -2,56 +2,16 @@ namespace TwbsHelper\Options\Factory; -use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\FactoryInterface; -use Laminas\ServiceManager\ServiceLocatorInterface; +use Psr\Container\ContainerInterface; +use TwbsHelper\Options\ModuleOptions; -/** - * ModuleOptionsFactory - * - * @uses FactoryInterface - */ -class ModuleOptionsFactory implements FactoryInterface +class ModuleOptionsFactory { - - - /** - * createService - * - * @param ServiceLocatorInterface $oServiceLocator - * @access public - * @return \TwbsHelper\Options\ModuleOptions - */ - public function createService(ServiceLocatorInterface $oServiceLocator) + public function __invoke(ContainerInterface $container): ModuleOptions { - return $this->createServiceWithConfig($oServiceLocator->get('config')); - } + /** @var array $config */ + $config = $container->get('config'); - - /** - * __invoke - * - * @param ContainerInterface $oContainer - * @param string $sRequestedName - * @param array $aOptions - * @access public - * @return \TwbsHelper\Options\ModuleOptions - */ - public function __invoke(ContainerInterface $oContainer, $sRequestedName, array $aOptions = null) - { - return $this->createServiceWithConfig($oContainer->get('config')); - } - - - /** - * createServiceWithConfig - * - * @param array $aConfig - * @access protected - * @return \TwbsHelper\Options\ModuleOptions - */ - protected function createServiceWithConfig(array $aConfig) - { - return new \TwbsHelper\Options\ModuleOptions($aConfig['twbshelper']); + return new ModuleOptions($config['twbshelper']); } } diff --git a/tests/TestSuite/TwbsHelper/Form/View/Helper/Factory/FormElementFactoryTest.php b/tests/TestSuite/TwbsHelper/Form/View/Helper/Factory/FormElementFactoryTest.php index bbf95fcfb..59d235337 100644 --- a/tests/TestSuite/TwbsHelper/Form/View/Helper/Factory/FormElementFactoryTest.php +++ b/tests/TestSuite/TwbsHelper/Form/View/Helper/Factory/FormElementFactoryTest.php @@ -2,26 +2,55 @@ namespace TestSuite\TwbsHelper\Form\View\Helper\Factory; -class FormElementFactoryTest extends \PHPUnit\Framework\TestCase +use PHPUnit\Framework\TestCase; +use TestSuite\Bootstrap; +use TwbsHelper\Form\View\Helper\Factory\FormModuleOptionsFactory; +use TwbsHelper\Form\View\Helper\Form; +use TwbsHelper\Form\View\Helper\FormCollection; +use TwbsHelper\Form\View\Helper\FormElement; +use TwbsHelper\Form\View\Helper\FormRow; + +class FormElementFactoryTest extends TestCase { /** - * @var \TwbsHelper\Form\View\Helper\Factory\FormElementFactory + * @var FormModuleOptionsFactory */ - protected $formElementFactory; + protected $formModuleOptionsFactory; - /** - * @see \PHPUnit\Framework\TestCase::setUp() - */ public function setUp(): void { - $this->formElementFactory = new \TwbsHelper\Form\View\Helper\Factory\FormElementFactory(); + $this->formElementFactory = new FormModuleOptionsFactory(); } - public function testRenderElement() + /** + * @dataProvider formElementProvider + */ + public function testRenderElement(string $formElement) { $this->assertInstanceOf( - \TwbsHelper\Form\View\Helper\FormElement::class, - $this->formElementFactory->createService(\TestSuite\Bootstrap::getServiceManager()) + $formElement, + ($this->formElementFactory)( + Bootstrap::getServiceManager(), + $formElement + ) ); } + + public function formElementProvider(): array + { + return [ + Form::class => [ + Form::class, + ], + FormElement::class => [ + FormElement::class, + ], + FormRow::class => [ + FormRow::class, + ], + FormCollection::class => [ + FormCollection::class, + ], + ]; + } } diff --git a/tests/TestSuite/TwbsHelper/Options/Factory/ModuleOptionsFactoryTest.php b/tests/TestSuite/TwbsHelper/Options/Factory/ModuleOptionsFactoryTest.php deleted file mode 100644 index a83603aa6..000000000 --- a/tests/TestSuite/TwbsHelper/Options/Factory/ModuleOptionsFactoryTest.php +++ /dev/null @@ -1,25 +0,0 @@ -moduleOptionsFactory = new \TwbsHelper\Options\Factory\ModuleOptionsFactory(); - } - - public function testCreateService() - { - $this->assertInstanceOf( - '\TwbsHelper\Options\ModuleOptions', - $this->moduleOptionsFactory->createService(\TestSuite\Bootstrap::getServiceManager()) - ); - } -}