This repository was archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add input_filter_specs feature from zfcampus/zf-content-validation and associated tests #7247
Closed
improved-broccoli
wants to merge
3
commits into
zendframework:master
from
improved-broccoli:import-input_filter_specs-from-zfcampus/zf-content-validation
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
library/Zend/InputFilter/InputFilterAbstractServiceFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| <?php | ||
| /** | ||
| * Zend Framework (http://framework.zend.com/) | ||
| * | ||
| * @link http://github.com/zendframework/zf2 for the canonical source repository | ||
| * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
| * @license http://framework.zend.com/license/new-bsd New BSD License | ||
| */ | ||
|
|
||
| namespace Zend\InputFilter; | ||
|
|
||
| use Zend\Filter\FilterPluginManager; | ||
| use Zend\ServiceManager\AbstractFactoryInterface; | ||
| use Zend\ServiceManager\ServiceLocatorInterface; | ||
| use Zend\Validator\ValidatorPluginManager; | ||
|
|
||
| class InputFilterAbstractServiceFactory implements AbstractFactoryInterface | ||
| { | ||
| /** | ||
| * @var Factory | ||
| */ | ||
| protected $factory; | ||
|
|
||
| /** | ||
| * @param ServiceLocatorInterface $inputFilters | ||
| * @param string $cName | ||
| * @param string $rName | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName) | ||
| { | ||
| $services = $inputFilters->getServiceLocator(); | ||
| if (! $services instanceof ServiceLocatorInterface | ||
| || ! $services->has('Config') | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| $config = $services->get('Config'); | ||
| if (!isset($config['input_filter_specs'][$rName]) | ||
| || !is_array($config['input_filter_specs'][$rName]) | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @param ServiceLocatorInterface $inputFilters | ||
| * @param string $cName | ||
| * @param string $rName | ||
| * | ||
| * @return \Zend\InputFilter\InputFilterInterface | ||
| */ | ||
| public function createServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName) | ||
| { | ||
| $services = $inputFilters->getServiceLocator(); | ||
| $allConfig = $services->get('Config'); | ||
| $config = $allConfig['input_filter_specs'][$rName]; | ||
|
|
||
| $factory = $this->getInputFilterFactory($services); | ||
|
|
||
| return $factory->createInputFilter($config); | ||
| } | ||
|
|
||
| /** | ||
| * @param ServiceLocatorInterface $services | ||
| * | ||
| * @return Factory | ||
| */ | ||
| protected function getInputFilterFactory(ServiceLocatorInterface $services) | ||
| { | ||
| if ($this->factory instanceof Factory) { | ||
| return $this->factory; | ||
| } | ||
|
|
||
| $this->factory = new Factory(); | ||
| $this->factory | ||
| ->getDefaultFilterChain() | ||
| ->setPluginManager($this->getFilterPluginManager($services)); | ||
| $this->factory | ||
| ->getDefaultValidatorChain() | ||
| ->setPluginManager($this->getValidatorPluginManager($services)); | ||
|
|
||
| return $this->factory; | ||
| } | ||
|
|
||
| /** | ||
| * @param ServiceLocatorInterface $services | ||
| * | ||
| * @return \Zend\ServiceManager\AbstractPluginManager | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's returning a |
||
| */ | ||
| protected function getFilterPluginManager(ServiceLocatorInterface $services) | ||
| { | ||
| if ($services->has('FilterManager')) { | ||
| return $services->get('FilterManager'); | ||
| } | ||
|
|
||
| return new FilterPluginManager(); | ||
| } | ||
|
|
||
| /** | ||
| * @param ServiceLocatorInterface $services | ||
| * | ||
| * @return \Zend\ServiceManager\AbstractPluginManager | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above comment, only |
||
| */ | ||
| protected function getValidatorPluginManager(ServiceLocatorInterface $services) | ||
| { | ||
| if ($services->has('ValidatorManager')) { | ||
| return $services->get('ValidatorManager'); | ||
| } | ||
|
|
||
| return new ValidatorPluginManager(); | ||
| } | ||
| } | ||
156 changes: 156 additions & 0 deletions
156
tests/ZendTest/InputFilter/InputFilterAbstractServiceFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| <?php | ||
| /** | ||
| * Zend Framework (http://framework.zend.com/) | ||
| * | ||
| * @link http://github.com/zendframework/zf2 for the canonical source repository | ||
| * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
| * @license http://framework.zend.com/license/new-bsd New BSD License | ||
| */ | ||
|
|
||
|
||
| namespace ZendTest\InputFilter; | ||
|
|
||
| use PHPUnit_Framework_TestCase as TestCase; | ||
| use Zend\Filter\FilterPluginManager; | ||
| use Zend\InputFilter\InputFilterPluginManager; | ||
| use Zend\ServiceManager\ServiceManager; | ||
| use Zend\Validator\ValidatorPluginManager; | ||
| use Zend\InputFilter\InputFilterAbstractServiceFactory; | ||
|
|
||
| class InputFilterAbstractServiceFactoryTest extends TestCase | ||
| { | ||
| public function setUp() | ||
| { | ||
| $this->services = new ServiceManager(); | ||
| $this->filters = new InputFilterPluginManager(); | ||
| $this->filters->setServiceLocator($this->services); | ||
| $this->services->setService('InputFilterManager', $this->filters); | ||
|
|
||
| $this->factory = new InputFilterAbstractServiceFactory(); | ||
| } | ||
|
|
||
| public function testCannotCreateServiceIfNoConfigServicePresent() | ||
| { | ||
| $this->assertFalse($this->factory->canCreateServiceWithName($this->filters, 'filter', 'filter')); | ||
| } | ||
|
|
||
| public function testCannotCreateServiceIfConfigServiceDoesNotHaveInputFiltersConfiguration() | ||
| { | ||
| $this->services->setService('Config', array()); | ||
| $this->assertFalse($this->factory->canCreateServiceWithName($this->filters, 'filter', 'filter')); | ||
| } | ||
|
|
||
| public function testCannotCreateServiceIfConfigInputFiltersDoesNotContainMatchingServiceName() | ||
| { | ||
| $this->services->setService('Config', array( | ||
| 'input_filter_specs' => array(), | ||
| )); | ||
| $this->assertFalse($this->factory->canCreateServiceWithName($this->filters, 'filter', 'filter')); | ||
| } | ||
|
|
||
| public function testCanCreateServiceIfConfigInputFiltersContainsMatchingServiceName() | ||
| { | ||
| $this->services->setService('Config', array( | ||
| 'input_filter_specs' => array( | ||
| 'filter' => array(), | ||
| ), | ||
| )); | ||
| $this->assertTrue($this->factory->canCreateServiceWithName($this->filters, 'filter', 'filter')); | ||
| } | ||
|
|
||
| public function testCreatesInputFilterInstance() | ||
| { | ||
| $this->services->setService('Config', array( | ||
| 'input_filter_specs' => array( | ||
| 'filter' => array(), | ||
| ), | ||
| )); | ||
| $filter = $this->factory->createServiceWithName($this->filters, 'filter', 'filter'); | ||
| $this->assertInstanceOf('Zend\InputFilter\InputFilterInterface', $filter); | ||
| } | ||
|
|
||
| /** | ||
| * @depends testCreatesInputFilterInstance | ||
| */ | ||
| public function testUsesConfiguredValidationAndFilterManagerServicesWhenCreatingInputFilter() | ||
| { | ||
| $filters = new FilterPluginManager(); | ||
| $filter = function ($value) { | ||
| }; | ||
| $filters->setService('foo', $filter); | ||
|
|
||
| $validators = new ValidatorPluginManager(); | ||
| $validator = $this->getMock('Zend\Validator\ValidatorInterface'); | ||
| $validators->setService('foo', $validator); | ||
|
|
||
| $this->services->setService('FilterManager', $filters); | ||
| $this->services->setService('ValidatorManager', $validators); | ||
| $this->services->setService('Config', array( | ||
| 'input_filter_specs' => array( | ||
| 'filter' => array( | ||
| 'input' => array( | ||
| 'name' => 'input', | ||
| 'required' => true, | ||
| 'filters' => array( | ||
| array( 'name' => 'foo' ), | ||
| ), | ||
| 'validators' => array( | ||
| array( 'name' => 'foo' ), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| )); | ||
|
|
||
| $inputFilter = $this->factory->createServiceWithName($this->filters, 'filter', 'filter'); | ||
| $this->assertTrue($inputFilter->has('input')); | ||
|
|
||
| $input = $inputFilter->get('input'); | ||
|
|
||
| $filterChain = $input->getFilterChain(); | ||
| $this->assertSame($filters, $filterChain->getPluginManager()); | ||
| $this->assertEquals(1, count($filterChain)); | ||
| $this->assertSame($filter, $filterChain->plugin('foo')); | ||
| $this->assertEquals(1, count($filterChain)); | ||
|
|
||
| $validatorChain = $input->getvalidatorChain(); | ||
| $this->assertSame($validators, $validatorChain->getPluginManager()); | ||
| $this->assertEquals(1, count($validatorChain)); | ||
| $this->assertSame($validator, $validatorChain->plugin('foo')); | ||
| $this->assertEquals(1, count($validatorChain)); | ||
| } | ||
|
|
||
| public function testRetrieveInputFilterFromInputFilterPluginManager() | ||
| { | ||
| $filters = new FilterPluginManager(); | ||
| $filter = function ($value) { | ||
| }; | ||
| $filters->setService('foo', $filter); | ||
|
|
||
| $validators = new ValidatorPluginManager(); | ||
| $validator = $this->getMock('Zend\Validator\ValidatorInterface'); | ||
| $validators->setService('foo', $validator); | ||
|
|
||
| $this->services->setService('FilterManager', $filters); | ||
| $this->services->setService('ValidatorManager', $validators); | ||
| $this->services->setService('Config', array( | ||
| 'input_filter_specs' => array( | ||
| 'foobar' => array( | ||
| 'input' => array( | ||
| 'name' => 'input', | ||
| 'required' => true, | ||
| 'filters' => array( | ||
| array( 'name' => 'foo' ), | ||
| ), | ||
| 'validators' => array( | ||
| array( 'name' => 'foo' ), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| )); | ||
| $this->services->get('InputFilterManager')->addAbstractFactory('Zend\InputFilter\InputFilterAbstractServiceFactory'); | ||
|
|
||
| $inputFilter = $this->services->get('InputFilterManager')->get('foobar'); | ||
| $this->assertInstanceOf('Zend\InputFilter\InputFilterInterface', $inputFilter); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change with :
like other files.