NEW: Made ValidationResult functions chain-able.

Added unit tests for CombiningResults
This commit is contained in:
Jeremy Shipman 2014-01-09 11:03:36 +13:00
parent c84d9ee00c
commit 62694b0a8b
2 changed files with 41 additions and 4 deletions

View File

@ -32,6 +32,7 @@ class ValidationResult extends Object {
* Record an error against this validation result,
* @param $message The validation error message
* @param $code An optional error code string, that can be accessed with {@link $this->codeList()}.
* @return ValidationResult this
*/
public function error($message, $code = null) {
$this->isValid = false;
@ -47,10 +48,13 @@ class ValidationResult extends Object {
} else {
$this->errorList[] = $message;
}
return $this;
}
/**
* Returns true if the result is valid.
* @return boolean
*/
public function valid() {
return $this->isValid;
@ -58,6 +62,7 @@ class ValidationResult extends Object {
/**
* Get an array of errors
* @return array
*/
public function messageList() {
return $this->errorList;
@ -65,6 +70,7 @@ class ValidationResult extends Object {
/**
* Get an array of error codes
* @return array
*/
public function codeList() {
$codeList = array();
@ -74,6 +80,7 @@ class ValidationResult extends Object {
/**
* Get the error message as a string.
* @return string
*/
public function message() {
return implode("; ", $this->errorList);
@ -81,6 +88,7 @@ class ValidationResult extends Object {
/**
* Get a starred list of all messages
* @return string
*/
public function starredList() {
return " * " . implode("\n * ", $this->errorList);
@ -90,10 +98,15 @@ class ValidationResult extends Object {
* Combine this Validation Result with the ValidationResult given in other.
* It will be valid if both this and the other result are valid.
* This object will be modified to contain the new validation information.
*
* @param ValidationResult the validation result object to combine
* @return ValidationResult this
*/
public function combineAnd(ValidationResult $other) {
$this->isValid = $this->isValid && $other->valid();
$this->errorList = array_merge($this->errorList, $other->messageList());
return $this;
}

View File

@ -27,8 +27,8 @@ class ValidationExceptionTest extends SapphireTest
*/
public function testCreateFromComplexValidationResult() {
$result = new ValidationResult();
$result->error('Invalid type');
$result->error('Out of kiwis');
$result->error('Invalid type')
->error('Out of kiwis');
$exception = new ValidationException($result);
$this->assertEquals(0, $exception->getCode());
@ -71,8 +71,8 @@ class ValidationExceptionTest extends SapphireTest
*/
public function testCreateWithComplexValidationResultAndMessage() {
$result = new ValidationResult();
$result->error('A spork is not a knife');
$result->error('A knife is not a back scratcher');
$result->error('A spork is not a knife')
->error('A knife is not a back scratcher');
$exception = new ValidationException($result, 'An error has occurred', E_USER_WARNING);
$this->assertEquals(E_USER_WARNING, $exception->getCode());
@ -81,4 +81,28 @@ class ValidationExceptionTest extends SapphireTest
$this->assertEquals('A spork is not a knife; A knife is not a back scratcher',
$exception->getResult()->message());
}
/**
* Test combining validation results together
*/
public function testCombineResults(){
$result = new ValidationResult();
$anotherresult = new ValidationResult();
$yetanotherresult = new ValidationResult();
$anotherresult->error("Eat with your mouth closed", "EATING101");
$yetanotherresult->error("You didn't wash your hands", "BECLEAN");
$this->assertTrue($result->valid());
$this->assertFalse($anotherresult->valid());
$this->assertFalse($yetanotherresult->valid());
$result->combineAnd($anotherresult)
->combineAnd($yetanotherresult);
$this->assertFalse($result->valid());
$this->assertEquals(array(
"EATING101" => "Eat with your mouth closed",
"BECLEAN" => "You didn't wash your hands"
),$result->messageList());
}
}