Skip to content

Commit

Permalink
Add tests for validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Jan 21, 2014
1 parent 3ae0810 commit 442f0c1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ abstract public function getControllerBehavioursOptions();

/**
* Allow to format error messages by different strategies
*
*
* @param array $errorMessages
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ public function testThrowExceptionOnFailedValidation()
$resource->expects($this->once())->method('getMetadata')->will($this->returnValue($metadata));
$metadata->expects($this->once())->method('getInputFilterName')->will($this->returnValue('inputFilter'));

$data = ['foo'];
$data = ['foo'];
$errorMessages = ['email' => ['invalid' => 'Email is invalid']];

$inputFilter = $this->getMock('Zend\InputFilter\InputFilterInterface');
$inputFilter->expects($this->once())->method('setData')->with($data);
$inputFilter->expects($this->once())->method('getMessages')->will($this->returnValue(['fail']));
$inputFilter->expects($this->once())->method('getMessages')->will($this->returnValue($errorMessages));
$inputFilter->expects($this->once())
->method('isValid')
->will($this->returnValue(false));
Expand All @@ -135,4 +136,35 @@ public function testThrowExceptionOnFailedValidation()

$this->dataValidation->validateData($resource, $data);
}

public function testCanPreserveKeys()
{
// We should not test protected methods but this is the only way I've found
$this->controllerBehavioursOptions->setPreserveErrorKeys(true);

$reflMethod = new \ReflectionMethod($this->dataValidation, 'formatErrorMessages');
$reflMethod->setAccessible(true);

$errorMessages = ['email' => ['invalid' => 'Email is invalid']];

$result = $reflMethod->invoke($this->dataValidation, $errorMessages);

$this->assertEquals($errorMessages, $result);
}

public function testCanRemoveKeys()
{
// We should not test protected methods but this is the only way I've found
$this->controllerBehavioursOptions->setPreserveErrorKeys(false);

$reflMethod = new \ReflectionMethod($this->dataValidation, 'formatErrorMessages');
$reflMethod->setAccessible(true);

$errorMessages = ['email' => ['invalid' => 'Email is invalid']];
$expectedMessages = ['email' => ['Email is invalid']];

$result = $reflMethod->invoke($this->dataValidation, $errorMessages);

$this->assertEquals($expectedMessages, $result);
}
}

0 comments on commit 442f0c1

Please sign in to comment.