diff --git a/CHANGELOG.md b/CHANGELOG.md index b38df108..808fd3d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,13 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed +- [#151](https://github.com/zendframework/zend-inputfilter/pull/151) fixes an + issue in `Factory::createInput()` introduced in + [#2](https://github.com/zendframework/zend-inputfilter/pull/2) whereby an + input pulled from the input filter manager would be injected with the default + filter and validator chains, overwriting any chains that were set during + instantiation and/or `init()`. They are now never overwritten. + - [#149](https://github.com/zendframework/zend-inputfilter/pull/149) fixes an issue with how error messages for collection input field items were reported; previously, as soon as one item in the collection failed, the same validation diff --git a/src/Factory.php b/src/Factory.php index ca524c06..0a0d38ac 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -189,10 +189,10 @@ public function createInput($inputSpecification) )); } - if ($this->defaultFilterChain) { + if (! $managerInstance && $this->defaultFilterChain) { $input->setFilterChain(clone $this->defaultFilterChain); } - if ($this->defaultValidatorChain) { + if (! $managerInstance && $this->defaultValidatorChain) { $input->setValidatorChain(clone $this->defaultValidatorChain); } diff --git a/test/FactoryTest.php b/test/FactoryTest.php index a4bec0da..17de82ae 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -12,6 +12,7 @@ use Interop\Container\ContainerInterface; use PHPUnit_Framework_MockObject_MockObject as MockObject; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; use Zend\Filter; use Zend\InputFilter\CollectionInputFilter; use Zend\InputFilter\Exception\InvalidArgumentException; @@ -996,6 +997,27 @@ public function testClearDefaultValidatorChain() $this->assertNull($factory->getDefaultValidatorChain()); } + /** + * @see https://github.com/zendframework/zend-inputfilter/issues/8 + */ + public function testWhenCreateInputPullsInputFromThePluginManagerItMustNotOverwriteFilterAndValidatorChains() + { + $input = $this->prophesize(InputInterface::class); + $input->setFilterChain(Argument::any())->shouldNotBeCalled(); + $input->setValidatorChain(Argument::any())->shouldNotBeCalled(); + + $pluginManager = $this->prophesize(InputFilterPluginManager::class); + $pluginManager->populateFactoryPluginManagers(Argument::type(Factory::class))->shouldBeCalled(); + $pluginManager->has('Some\Test\Input')->willReturn(true); + $pluginManager->get('Some\Test\Input')->will([$input, 'reveal']); + + $spec = ['type' => 'Some\Test\Input']; + + $factory = new Factory($pluginManager->reveal()); + + $this->assertSame($input->reveal(), $factory->createInput($spec)); + } + /** * @return Factory */