Skip to content

Commit

Permalink
MultipleValidatorWithAnd now can break out of loop when error occurs (#…
Browse files Browse the repository at this point in the history
…122)

* MultipleValidatorWithAnd now can break out of loop when error occurs

* improved logic

* updated doc

* bugfix
  • Loading branch information
issei-m authored and egulias committed Jul 12, 2016
1 parent b366d54 commit 19811e0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
56 changes: 54 additions & 2 deletions EmailValidator/Validation/MultipleValidationWithAnd.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,60 @@
namespace Egulias\EmailValidator\Validation;

use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\InvalidEmail;
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;

class MultipleValidationWithAnd implements EmailValidation
{
/**
* If one of validations gets failure skips all succeeding validation.
* This means MultipleErrors will only contain a single error which first found.
*/
const STOP_ON_ERROR = 0;

/**
* All of validations will be invoked even if one of them got failure.
* So MultipleErrors will contain all causes.
*/
const ALLOW_ALL_ERRORS = 1;

/**
* @var EmailValidation[]
*/
private $validations = [];

/**
* @var array
*/
private $warnings = [];

/**
* @var MultipleErrors
*/
private $error;

public function __construct(array $validations)

/**
* @var bool
*/
private $mode;

/**
* @param EmailValidation[] $validations The validations.
* @param int $mode The validation mode (one of the constants).
*/
public function __construct(array $validations, $mode = self::ALLOW_ALL_ERRORS)
{
if (count($validations) == 0) {
throw new EmptyValidationList();
}

$this->validations = $validations;
$this->mode = $mode;
}

/**
* {@inheritdoc}
*/
public function isValid($email, EmailLexer $emailLexer)
{
$result = true;
Expand All @@ -29,17 +66,32 @@ public function isValid($email, EmailLexer $emailLexer)
$result = $result && $validation->isValid($email, $emailLexer);
$this->warnings = array_merge($this->warnings, $validation->getWarnings());
$errors[] = $validation->getError();

if ($this->shouldStop($result)) {
break;
}
}
$this->error = new MultipleErrors($errors);

return $result;
}

private function shouldStop($result)
{
return !$result && $this->mode === self::STOP_ON_ERROR;
}

/**
* {@inheritdoc}
*/
public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getWarnings()
{
return $this->warnings;
Expand Down
25 changes: 24 additions & 1 deletion Tests/EmailValidator/Validation/MultipleValidationWitAndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testUsesAndLogicalOperation()
}

/**
* @expectedException Egulias\EmailValidator\Validation\Exception\EmptyValidationList
* @expectedException \Egulias\EmailValidator\Validation\Exception\EmptyValidationList
*/
public function testEmptyListIsNotAllowed()
{
Expand Down Expand Up @@ -79,4 +79,27 @@ public function testGathersAllTheErrors()
$multipleValidation->isValid("[email protected]", $lexer);
$this->assertEquals($expectedResult, $multipleValidation->getError());
}

public function testBreakOutOfLoopWhenError()
{
$error = new CommaInDomain();

$expectedResult = new MultipleErrors([$error]);

$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer");

$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
$validation1->expects($this->any())->method("isValid")->willReturn(false);
$validation1->expects($this->once())->method("getWarnings")->willReturn([]);
$validation1->expects($this->once())->method("getError")->willReturn($error);

$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
$validation2->expects($this->never())->method("isValid");
$validation2->expects($this->never())->method("getWarnings");
$validation2->expects($this->never())->method("getError");

$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2], MultipleValidationWithAnd::STOP_ON_ERROR);
$multipleValidation->isValid("[email protected]", $lexer);
$this->assertEquals($expectedResult, $multipleValidation->getError());
}
}

0 comments on commit 19811e0

Please sign in to comment.