Skip to content

Commit

Permalink
Merge pull request #227 from froschdesign/hotfix/migrate-factories
Browse files Browse the repository at this point in the history
Migrates and simplifies factories
  • Loading branch information
neilime authored Oct 8, 2021
2 parents ee0922c + a9b9dcb commit 43c80a4
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 250 deletions.
8 changes: 4 additions & 4 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
40 changes: 0 additions & 40 deletions src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php

This file was deleted.

44 changes: 0 additions & 44 deletions src/TwbsHelper/Form/View/Helper/Factory/FormElementFactory.php

This file was deleted.

40 changes: 0 additions & 40 deletions src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace TwbsHelper\Form\View\Helper\Factory;

use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use TwbsHelper\Options\ModuleOptions;

class FormModuleOptionsFactory implements FactoryInterface
{
public function __invoke(
ContainerInterface $container,
$requestedName,
?array $options = null
) {
/** @var ModuleOptions $options */
$moduleOptions = $container->get(ModuleOptions::class);

return new $requestedName($moduleOptions);
}
}
40 changes: 0 additions & 40 deletions src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php

This file was deleted.

54 changes: 7 additions & 47 deletions src/TwbsHelper/Options/Factory/ModuleOptionsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
}
}

This file was deleted.

0 comments on commit 43c80a4

Please sign in to comment.