Permalink
Browse files

MVC service factories for Filters and Validators fixes and unit tests

  • Loading branch information...
1 parent bb80dd3 commit a6f8bc66fff24b7785d1e22932f6922dffc99252 @cgmartin cgmartin committed Sep 8, 2012
@@ -10,12 +10,8 @@
namespace Zend\Mvc\Service;
-use Zend\Mvc\Exception;
-use Zend\Mvc\Router\RouteMatch;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
-use Zend\View\Helper as ViewHelper;
-use Zend\View\Helper\HelperInterface as ViewHelperInterface;
/**
* @category Zend
@@ -24,17 +20,17 @@
*/
class FilterManagerFactory extends AbstractPluginManagerFactory
{
- const PLUGIN_MANAGER_CLASS = 'Zend\Validator\FilterPluginManager';
+ 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;
+ /**
+ * Create and return the filter plugin manager
+ *
+ * @param ServiceLocatorInterface $serviceLocator
+ * @return FilterPluginManager
+ */
+ public function createService(ServiceLocatorInterface $serviceLocator)
+ {
+ $plugins = parent::createService($serviceLocator);
+ return $plugins;
}
}
@@ -10,12 +10,8 @@
namespace Zend\Mvc\Service;
-use Zend\Mvc\Exception;
-use Zend\Mvc\Router\RouteMatch;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
-use Zend\View\Helper as ViewHelper;
-use Zend\View\Helper\HelperInterface as ViewHelperInterface;
/**
* @category Zend
@@ -26,15 +22,15 @@ 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;
+ /**
+ * Create and return the validator plugin manager
+ *
+ * @param ServiceLocatorInterface $serviceLocator
+ * @return ValidatorPluginManager
+ */
+ public function createService(ServiceLocatorInterface $serviceLocator)
+ {
+ $plugins = parent::createService($serviceLocator);
+ return $plugins;
}
}
@@ -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');
+ }
+}
@@ -45,6 +45,7 @@ public function setUp()
public function tearDown()
{
+ AbstractValidator::setDefaultTranslator(null);
AbstractValidator::setMessageLength(-1);
}
@@ -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 a6f8bc6

Please sign in to comment.