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

Commit

Permalink
[zendframework/zendframework#2146] Added unit tests to ensure chain i…
Browse files Browse the repository at this point in the history
…nstances are used

- Also optimized filter/validator chain checks
  • Loading branch information
weierophinney committed Aug 16, 2012
1 parent d19b2a6 commit 2503264
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,31 @@ public function createInput($inputSpecification)
}
break;
case 'filters':
if (!is_array($value) && !$value instanceof Traversable && !$value instanceof FilterChain) {
if ($value instanceof FilterChain) {
$input->setFilterChain($value);
break;
}
if (!is_array($value) && !$value instanceof Traversable) {
throw new Exception\RuntimeException(sprintf(
'%s expects the value associated with "filters" to be an array/Traversable of filters or filter specifications, or a FilterChain; received "%s"',
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value))
));
}
if ($value instanceof FilterChain) {
$input->setFilterChain($value);
break;
}
$this->populateFilters($input->getFilterChain(), $value);
break;
case 'validators':
if (!is_array($value) && !$value instanceof Traversable && !$value instanceof ValidatorChain) {
if ($value instanceof ValidatorChain) {
$input->setValidatorChain($value);
break;
}
if (!is_array($value) && !$value instanceof Traversable) {
throw new Exception\RuntimeException(sprintf(
'%s expects the value associated with "validators" to be an array/Traversable of validators or validator specifications, or a ValidatorChain; received "%s"',
__METHOD__,
(is_object($value) ? get_class($value) : gettype($value))
));
}
if ($value instanceof ValidatorChain) {
$input->setValidatorChain($value);
break;
}
$this->populateValidators($input->getValidatorChain(), $value);
break;
default:
Expand Down
26 changes: 26 additions & 0 deletions test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,30 @@ public function testFactoryWillCreateInputFilterMatchingInputNameWhenNotSpecifie
$this->assertTrue($inputFilter->has('foo'));
$this->assertInstanceOf('Zend\InputFilter\Input', $inputFilter->get('foo'));
}

public function testFactoryAllowsPassingValidatorChainsInInputSpec()
{
$factory = new Factory();
$chain = new Validator\ValidatorChain();
$input = $factory->createInput(array(
'name' => 'foo',
'validators' => $chain,
));
$this->assertInstanceOf('Zend\InputFilter\InputInterface', $input);
$test = $input->getValidatorChain();
$this->assertSame($chain, $test);
}

public function testFactoryAllowsPassingFilterChainsInInputSpec()
{
$factory = new Factory();
$chain = new Filter\FilterChain();
$input = $factory->createInput(array(
'name' => 'foo',
'filters' => $chain,
));
$this->assertInstanceOf('Zend\InputFilter\InputInterface', $input);
$test = $input->getFilterChain();
$this->assertSame($chain, $test);
}
}

0 comments on commit 2503264

Please sign in to comment.