Browse files

Merge branch 'feature/validator-plugin-manager' of https://github.com…

…/cgmartin/zf2 into feature/filter-validator-manager-factories
  • Loading branch information...
2 parents 871401e + b57d117 commit 526a33c658ee6e6d35a9adbed611fb324b68aedb @weierophinney weierophinney committed Sep 13, 2012
View
27 library/Zend/ModuleManager/Feature/FilterProviderInterface.php
@@ -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();
+}
View
27 library/Zend/ModuleManager/Feature/ValidatorProviderInterface.php
@@ -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();
+}
View
36 library/Zend/Mvc/Service/FilterManagerFactory.php
@@ -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;
+ }
+}
View
12 library/Zend/Mvc/Service/ModuleManagerFactory.php
@@ -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);
View
2 library/Zend/Mvc/Service/ServiceListenerFactory.php
@@ -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',
View
36 library/Zend/Mvc/Service/ValidatorManagerFactory.php
@@ -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;
+ }
+}
View
46 tests/ZendTest/Filter/FilterPluginManagerTest.php
@@ -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');
+ }
+}
View
1 tests/ZendTest/Validator/ValidatorChainTest.php
@@ -45,6 +45,7 @@ public function setUp()
public function tearDown()
{
+ AbstractValidator::setDefaultTranslator(null);
AbstractValidator::setMessageLength(-1);
}
View
76 tests/ZendTest/Validator/ValidatorPluginManagerTest.php
@@ -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');
+ }
+}

0 comments on commit 526a33c

Please sign in to comment.