mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW: Made ValidationResult functions chain-able.
Added unit tests for CombiningResults
This commit is contained in:
parent
c84d9ee00c
commit
62694b0a8b
@ -32,6 +32,7 @@ class ValidationResult extends Object {
|
|||||||
* Record an error against this validation result,
|
* Record an error against this validation result,
|
||||||
* @param $message The validation error message
|
* @param $message The validation error message
|
||||||
* @param $code An optional error code string, that can be accessed with {@link $this->codeList()}.
|
* @param $code An optional error code string, that can be accessed with {@link $this->codeList()}.
|
||||||
|
* @return ValidationResult this
|
||||||
*/
|
*/
|
||||||
public function error($message, $code = null) {
|
public function error($message, $code = null) {
|
||||||
$this->isValid = false;
|
$this->isValid = false;
|
||||||
@ -47,10 +48,13 @@ class ValidationResult extends Object {
|
|||||||
} else {
|
} else {
|
||||||
$this->errorList[] = $message;
|
$this->errorList[] = $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the result is valid.
|
* Returns true if the result is valid.
|
||||||
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function valid() {
|
public function valid() {
|
||||||
return $this->isValid;
|
return $this->isValid;
|
||||||
@ -58,6 +62,7 @@ class ValidationResult extends Object {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an array of errors
|
* Get an array of errors
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function messageList() {
|
public function messageList() {
|
||||||
return $this->errorList;
|
return $this->errorList;
|
||||||
@ -65,6 +70,7 @@ class ValidationResult extends Object {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an array of error codes
|
* Get an array of error codes
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function codeList() {
|
public function codeList() {
|
||||||
$codeList = array();
|
$codeList = array();
|
||||||
@ -74,6 +80,7 @@ class ValidationResult extends Object {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the error message as a string.
|
* Get the error message as a string.
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function message() {
|
public function message() {
|
||||||
return implode("; ", $this->errorList);
|
return implode("; ", $this->errorList);
|
||||||
@ -81,6 +88,7 @@ class ValidationResult extends Object {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a starred list of all messages
|
* Get a starred list of all messages
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function starredList() {
|
public function starredList() {
|
||||||
return " * " . implode("\n * ", $this->errorList);
|
return " * " . implode("\n * ", $this->errorList);
|
||||||
@ -90,10 +98,15 @@ class ValidationResult extends Object {
|
|||||||
* Combine this Validation Result with the ValidationResult given in other.
|
* Combine this Validation Result with the ValidationResult given in other.
|
||||||
* It will be valid if both this and the other result are valid.
|
* It will be valid if both this and the other result are valid.
|
||||||
* This object will be modified to contain the new validation information.
|
* 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) {
|
public function combineAnd(ValidationResult $other) {
|
||||||
$this->isValid = $this->isValid && $other->valid();
|
$this->isValid = $this->isValid && $other->valid();
|
||||||
$this->errorList = array_merge($this->errorList, $other->messageList());
|
$this->errorList = array_merge($this->errorList, $other->messageList());
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ class ValidationExceptionTest extends SapphireTest
|
|||||||
*/
|
*/
|
||||||
public function testCreateFromComplexValidationResult() {
|
public function testCreateFromComplexValidationResult() {
|
||||||
$result = new ValidationResult();
|
$result = new ValidationResult();
|
||||||
$result->error('Invalid type');
|
$result->error('Invalid type')
|
||||||
$result->error('Out of kiwis');
|
->error('Out of kiwis');
|
||||||
$exception = new ValidationException($result);
|
$exception = new ValidationException($result);
|
||||||
|
|
||||||
$this->assertEquals(0, $exception->getCode());
|
$this->assertEquals(0, $exception->getCode());
|
||||||
@ -71,8 +71,8 @@ class ValidationExceptionTest extends SapphireTest
|
|||||||
*/
|
*/
|
||||||
public function testCreateWithComplexValidationResultAndMessage() {
|
public function testCreateWithComplexValidationResultAndMessage() {
|
||||||
$result = new ValidationResult();
|
$result = new ValidationResult();
|
||||||
$result->error('A spork is not a knife');
|
$result->error('A spork is not a knife')
|
||||||
$result->error('A knife is not a back scratcher');
|
->error('A knife is not a back scratcher');
|
||||||
$exception = new ValidationException($result, 'An error has occurred', E_USER_WARNING);
|
$exception = new ValidationException($result, 'An error has occurred', E_USER_WARNING);
|
||||||
|
|
||||||
$this->assertEquals(E_USER_WARNING, $exception->getCode());
|
$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',
|
$this->assertEquals('A spork is not a knife; A knife is not a back scratcher',
|
||||||
$exception->getResult()->message());
|
$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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user