Fixes 'KeySet' rule when input is not array type

This commit is contained in:
Emmerson 2016-04-27 09:04:44 -03:00 committed by Henrique Moody
parent 294368294f
commit d9a4c78b16
2 changed files with 25 additions and 0 deletions

View file

@ -87,6 +87,10 @@ class KeySet extends AllOf
*/
private function hasValidStructure($input)
{
if (!is_array($input)) {
return false;
}
foreach ($this->getRules() as $keyRule) {
if (!array_key_exists($keyRule->reference, $input) && $keyRule->mandatory) {
return false;

View file

@ -172,4 +172,25 @@ class KeySetTest extends PHPUnit_Framework_TestCase
$keySet = new KeySet($key1, $key2);
$keySet->assert($input);
}
/**
* @expectedException Respect\Validation\Exceptions\KeySetException
* @expectedExceptionMessage Must have keys { "name" }
* @dataProvider providerForInvalidArguments
*/
public function testShouldThrowExceptionInCaseArgumentIsAnythingOtherThanArray($input)
{
$keySet = new KeySet(new Key('name'));
$keySet->assert($input);
}
public function providerForInvalidArguments()
{
return [
[''],
[null],
[0],
[new \stdClass()]
];
}
}