Last active
August 29, 2015 14:01
-
-
Save bakura10/e35887640ef00942476c to your computer and use it in GitHub Desktop.
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 | |
class MyInputFilter extends InputFilter | |
{ | |
public function __construct() | |
{ | |
$this->add(['name' => 'example', 'required' => false]); | |
$this->add(['name' => 'foo', 'required' => false]); | |
$this->add(['name' => 'bar', 'required' => false]); | |
} | |
} | |
$inputFilter = new MyInputFilter(); | |
$inputFilter->setData(['foo' => 'myValue', 'unknown' => 'lolzor']); | |
if ($inputFilter->isValid()) { | |
$values = $inputFilter->getValues(); | |
// Do you expect (1) ['example' => null, 'foo' => 'myValue', 'bar' => null] | |
// OR (2) : ['example' => null, 'foo' => 'myValue', 'bar' => null, 'unknown' => 'lolzor'] | |
// OR (3) : ['foo' => 'myValue'] | |
// OR (4) : ['foo' => 'myValue', 'unknown' => 'lolzor'] | |
} | |
// Currently, ZF2 does (1), I personally expect it to do (3), with getUnknown returning unknown fields |
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 | |
public function dataProvider() | |
{ | |
return [ | |
// Validate none | |
[ | |
'validation_group' => InputCollection::VALIDATE_NONE, | |
'data' => [], | |
'result_raw_data' => [], | |
'result_filtered_data' => [], | |
'result_unknown_data' => [], | |
'is_valid' => true | |
], | |
// Validate all | |
[ | |
'validation_group' => InputCollection::VALIDATE_ALL, | |
'data' => ['email' => '[email protected]', 'first_name' => ' Michaël '], | |
'result_raw_data' => ['email' => '[email protected]', 'first_name' => ' Michaël '], | |
'result_filtered_data' => ['email' => '[email protected]', 'first_name' => 'Michaël'], | |
'result_unknown_data' => [], | |
'is_valid' => true | |
], | |
// Validate all with first_name not given | |
[ | |
'validation_group' => InputCollection::VALIDATE_ALL, | |
'data' => ['email' => '[email protected]'], | |
'result_raw_data' => ['email' => '[email protected]'], | |
'result_filtered_data' => ['email' => '[email protected]'], | |
'result_unknown_data' => [], | |
'is_valid' => true | |
], | |
// Validate all with unknown value | |
[ | |
'validation_group' => InputCollection::VALIDATE_ALL, | |
'data' => ['email' => '[email protected]', 'unknown' => 'value'], | |
'result_raw_data' => ['email' => '[email protected]'], | |
'result_filtered_data' => ['email' => '[email protected]'], | |
'result_unknown_data' => ['unknown' => 'value'], | |
'is_valid' => true | |
], | |
// Assert fails if required input is not present | |
[ | |
'validation_group' => InputCollection::VALIDATE_ALL, | |
'data' => ['first_name' => 'Michaël'], | |
'result_raw_data' => ['first_name' => 'Michaël'], | |
'result_filtered_data' => ['first_name' => 'Michaël'], | |
'result_unknown_data' => [], | |
'is_valid' => false | |
], | |
// Validate only one field (with validation group) without unknown fields | |
[ | |
'validation_group' => ['email'], | |
'data' => ['email' => '[email protected]', 'first_name' => 'Michaël'], | |
'result_raw_data' => ['email' => '[email protected]'], | |
'result_filtered_data' => ['email' => '[email protected]'], | |
'result_unknown_data' => [], | |
'is_valid' => true | |
], | |
// Validate only one field with a nested input collection | |
[ | |
'validation_group' => InputCollection::VALIDATE_ALL, | |
'data' => ['email' => '[email protected]', 'address' => ['city' => 'a']], | |
'result_raw_data' => ['email' => '[email protected]', 'address' => ['city' => 'a']], | |
'result_filtered_data' => ['email' => '[email protected]'], | |
'result_unknown_data' => [], | |
'is_valid' => false | |
], | |
// Validate two fields with a nested input collection | |
[ | |
'validation_group' => InputCollection::VALIDATE_ALL, | |
'data' => ['email' => '[email protected]', 'address' => ['city' => ' abc ']], | |
'result_raw_data' => ['email' => '[email protected]', 'address' => ['city' => ' abc ']], | |
'result_filtered_data' => ['email' => '[email protected]', 'address' => ['city' => 'abc']], | |
'result_unknown_data' => [], | |
'is_valid' => true | |
], | |
// Assert can have unknown field in nested inputs | |
[ | |
'validation_group' => InputCollection::VALIDATE_ALL, | |
'data' => ['email' => '[email protected]', 'address' => ['unknown' => 'abc']], | |
'result_raw_data' => ['email' => '[email protected]'], | |
'result_filtered_data' => ['email' => '[email protected]'], | |
'result_unknown_data' => ['address' => ['unknown' => 'abc']], | |
'is_valid' => true | |
], | |
// Assert that validation group applies to nested inputs | |
[ | |
'validation_group' => ['address' => ['city']], | |
'data' => ['email' => '[email protected]', 'address' => ['city' => ' abc ']], | |
'result_raw_data' => ['address' => ['city' => ' abc ']], | |
'result_filtered_data' => ['address' => ['city' => 'abc']], | |
'result_unknown_data' => [], | |
'is_valid' => true | |
] | |
]; | |
} | |
/** | |
* @dataProvider dataProvider | |
*/ | |
public function testBehaviour( | |
$validationGroup, | |
array $data, | |
array $resultRawData, | |
array $resultFilteredData, | |
array $resultUnknownData, | |
$isValid | |
) { | |
$inputCollection = new InputCollection(); | |
$inputCollection->setName('user'); | |
// We add one input that is required, one that is optional, and a nested input collection | |
$input1 = new Input(); | |
$input1->setName('email'); | |
$input1->setRequired(true); | |
$input2 = new Input(); | |
$input2->setName('first_name'); | |
$input2->getFilterChain()->attachByName(StringTrim::class); | |
$addressInputCollection = new InputCollection(); | |
$addressInputCollection->setName('address'); | |
$input3 = new Input(); | |
$input3->setName('city'); | |
$input3->getFilterChain()->attachByName(StringTrim::class); | |
$input3->getValidatorChain()->attachByName(StringLength::class, ['min' => 2]); | |
$addressInputCollection->addInput($input3); | |
$inputCollection->addInput($input1); | |
$inputCollection->addInput($input2); | |
$inputCollection->addInput($addressInputCollection); | |
$inputCollection->setValidationGroup($validationGroup); | |
$result = $inputCollection->runAgainst($data); | |
$this->assertEquals($isValid, $result->isValid()); | |
$this->assertEquals($resultRawData, $result->getRawData()); | |
$this->assertEquals($resultFilteredData, $result->getData()); | |
$this->assertEquals($resultUnknownData, $result->getUnknownData()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍