Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrates and simplifies factories #227

Merged
merged 2 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong variable name

$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.