Created
June 16, 2015 13:47
-
-
Save mirfilip/941ede7a1c8a2b4b0fa8 to your computer and use it in GitHub Desktop.
Null or a valid string
This file contains 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
<?php | |
require_once __DIR__ . '/../vendor/autoload.php'; | |
/** | |
* Class InputFilterTest | |
*/ | |
class InputFilterTest extends \PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @dataProvider provider | |
*/ | |
public function testInput(array $data, $expected) | |
{ | |
echo '=========' . "\n"; | |
$inputFilter = $this->createInput($data); | |
$result = $inputFilter->isValid(); | |
$this->assertSame($expected, $result); | |
echo ($result ? 'valid' : 'not valid'); | |
echo "\n" . '========='; | |
} | |
public function provider() | |
{ | |
return [ | |
[['optionalField' => 'value'], true], | |
[['optionalField' => ''], false], | |
[['optionalField' => []], false], | |
[['optionalField' => new \StdClass()], false], | |
"This fails :(" => [['optionalField' => null], true], | |
[[], false], | |
[['optionalField' => 1], false], | |
[['optionalField' => -1], false], | |
[['optionalField' => 0], false], | |
]; | |
} | |
/** | |
* @param array $data | |
* @return \Zend\InputFilter\InputFilter | |
*/ | |
private function createInput(array $data) | |
{ | |
$notEmptyType = \Zend\Validator\NotEmpty::ALL & ~\Zend\Validator\NotEmpty::NULL & ~\Zend\Validator\NotEmpty::STRING; | |
$input = new \Zend\InputFilter\Input('optionalField'); | |
$input | |
// ->setAllowEmpty(true) | |
// ->setRequired(false) | |
->setContinueIfEmpty(true) | |
->getValidatorChain() | |
->attach(new \Zend\Validator\NotEmpty($notEmptyType), true) | |
->attach(new \Zend\Validator\StringLength([ | |
'min' => 2 | |
]), true) | |
->attach(new \Zend\Validator\Callback(function ($value) { | |
echo 'validator called' . "\n"; | |
var_export($value); | |
echo "\n"; | |
return true; | |
}), true); | |
$inputFilter = new \Zend\InputFilter\InputFilter(); | |
$inputFilter->add($input); | |
$inputFilter->setData($data); | |
return $inputFilter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment