Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Add MVC service factories for Filters and Validators #2315

Merged
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
27 changes: 27 additions & 0 deletions library/Zend/ModuleManager/Feature/FilterProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_ModuleManager
*/

namespace Zend\ModuleManager\Feature;

/**
* @category Zend
* @package Zend_ModuleManager
* @subpackage Feature
*/
interface FilterProviderInterface
{
/**
* Expected to return \Zend\ServiceManager\Config object or array to
* seed such an object.
*
* @return array|\Zend\ServiceManager\Config
*/
public function getFilterConfig();
}
27 changes: 27 additions & 0 deletions library/Zend/ModuleManager/Feature/ValidatorProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_ModuleManager
*/

namespace Zend\ModuleManager\Feature;

/**
* @category Zend
* @package Zend_ModuleManager
* @subpackage Feature
*/
interface ValidatorProviderInterface
{
/**
* Expected to return \Zend\ServiceManager\Config object or array to
* seed such an object.
*
* @return array|\Zend\ServiceManager\Config
*/
public function getValidatorConfig();
}
36 changes: 36 additions & 0 deletions library/Zend/Mvc/Service/FilterManagerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace Zend\Mvc\Service;

use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* @category Zend
* @package Zend_Mvc
* @subpackage Service
*/
class FilterManagerFactory extends AbstractPluginManagerFactory
{
const PLUGIN_MANAGER_CLASS = 'Zend\Filter\FilterPluginManager';

/**
* Create and return the filter plugin manager
*
* @param ServiceLocatorInterface $serviceLocator
* @return FilterPluginManager
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$plugins = parent::createService($serviceLocator);
return $plugins;
}
}
12 changes: 12 additions & 0 deletions library/Zend/Mvc/Service/ModuleManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ public function createService(ServiceLocatorInterface $serviceLocator)
'Zend\ModuleManager\Feature\ViewHelperProviderInterface',
'getViewHelperConfig'
);
$serviceListener->addServiceManager(
'ValidatorManager',
'validators',
'Zend\ModuleManager\Feature\ValidatorProviderInterface',
'getValidatorConfig'
);
$serviceListener->addServiceManager(
'FilterManager',
'filters',
'Zend\ModuleManager\Feature\FilterProviderInterface',
'getFilterConfig'
);

$events = $serviceLocator->get('EventManager');
$events->attach($defaultListeners);
Expand Down
2 changes: 2 additions & 0 deletions library/Zend/Mvc/Service/ServiceListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ class ServiceListenerFactory implements FactoryInterface
'ConsoleAdapter' => 'Zend\Mvc\Service\ConsoleAdapterFactory',
'ConsoleRouter' => 'Zend\Mvc\Service\RouterFactory',
'DependencyInjector' => 'Zend\Mvc\Service\DiFactory',
'FilterManager' => 'Zend\Mvc\Service\FilterManagerFactory',
'HttpRouter' => 'Zend\Mvc\Service\RouterFactory',
'Request' => 'Zend\Mvc\Service\RequestFactory',
'Response' => 'Zend\Mvc\Service\ResponseFactory',
'Router' => 'Zend\Mvc\Service\RouterFactory',
'ValidatorManager' => 'Zend\Mvc\Service\ValidatorManagerFactory',
'ViewHelperManager' => 'Zend\Mvc\Service\ViewHelperManagerFactory',
'ViewFeedRenderer' => 'Zend\Mvc\Service\ViewFeedRendererFactory',
'ViewFeedStrategy' => 'Zend\Mvc\Service\ViewFeedStrategyFactory',
Expand Down
36 changes: 36 additions & 0 deletions library/Zend/Mvc/Service/ValidatorManagerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace Zend\Mvc\Service;

use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* @category Zend
* @package Zend_Mvc
* @subpackage Service
*/
class ValidatorManagerFactory extends AbstractPluginManagerFactory
{
const PLUGIN_MANAGER_CLASS = 'Zend\Validator\ValidatorPluginManager';

/**
* Create and return the validator plugin manager
*
* @param ServiceLocatorInterface $serviceLocator
* @return ValidatorPluginManager
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$plugins = parent::createService($serviceLocator);
return $plugins;
}
}
46 changes: 46 additions & 0 deletions tests/ZendTest/Filter/FilterPluginManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Filter
*/

namespace ZendTest\Filter;

use Zend\Filter\FilterPluginManager;

/**
* @category Zend
* @package Zend_Filter
* @subpackage UnitTests
* @group Zend_Filter
*/
class FilterPluginManagerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->filters = new FilterPluginManager();
}

public function testFilterSuccessfullyRetrieved()
{
$filter = $this->filters->get('int');
$this->assertInstanceOf('Zend\Filter\Int', $filter);
}

public function testRegisteringInvalidFilterRaisesException()
{
$this->setExpectedException('Zend\Filter\Exception\RuntimeException');
$this->filters->setService('test', $this);
}

public function testLoadingInvalidFilterRaisesException()
{
$this->filters->setInvokableClass('test', get_class($this));
$this->setExpectedException('Zend\Filter\Exception\RuntimeException');
$this->filters->get('test');
}
}
1 change: 1 addition & 0 deletions tests/ZendTest/Validator/ValidatorChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function setUp()

public function tearDown()
{
AbstractValidator::setDefaultTranslator(null);
AbstractValidator::setMessageLength(-1);
}

Expand Down
76 changes: 76 additions & 0 deletions tests/ZendTest/Validator/ValidatorPluginManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Validator
*/

namespace ZendTest\Validator;

use Zend\Validator\ValidatorPluginManager;

/**
* @category Zend
* @package Zend_Validator
* @subpackage UnitTests
* @group Zend_Validator
*/
class ValidatorPluginManagerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->validators = new ValidatorPluginManager();
}

public function testAllowsInjectingTranslator()
{
$translator = $this->getMock("Zend\I18n\Translator\Translator");

$slContents = array(array('translator', $translator));
$serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$serviceLocator->expects($this->once())
->method('get')
->will($this->returnValueMap($slContents));
$serviceLocator->expects($this->once())
->method('has')
->with($this->equalTo('translator'))
->will($this->returnValue(true));

$this->validators->setServiceLocator($serviceLocator);
$this->assertSame($serviceLocator, $this->validators->getServiceLocator());

$validator = $this->validators->get('notempty');
$this->assertSame($translator, $validator->getTranslator());
}

public function testNoTranslatorInjectedWhenTranslatorIsNotPresent()
{
$serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$serviceLocator->expects($this->once())
->method('has')
->with($this->equalTo('translator'))
->will($this->returnValue(false));

$this->validators->setServiceLocator($serviceLocator);
$this->assertSame($serviceLocator, $this->validators->getServiceLocator());

$validator = $this->validators->get('notempty');
$this->assertNull($validator->getTranslator());
}

public function testRegisteringInvalidValidatorRaisesException()
{
$this->setExpectedException('Zend\Validator\Exception\RuntimeException');
$this->validators->setService('test', $this);
}

public function testLoadingInvalidValidatorRaisesException()
{
$this->validators->setInvokableClass('test', get_class($this));
$this->setExpectedException('Zend\Validator\Exception\RuntimeException');
$this->validators->get('test');
}
}