Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RFCValidation strategy, multiple validation (#96)
closes #62
- Loading branch information
Showing
7 changed files
with
209 additions
and
31 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/Egulias/EmailValidator/Validation/Exception/EmptyValidationList.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Egulias\EmailValidator\Validation\Exception; | ||
|
||
use Exception; | ||
|
||
class EmptyValidationList extends \InvalidArgumentException | ||
{ | ||
public function __construct($code = 0, Exception $previous = null) | ||
{ | ||
parent::__construct("Empty validation list is not allowed", $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Egulias\EmailValidator\Validation; | ||
|
||
use Egulias\EmailValidator\Exception\InvalidEmail; | ||
|
||
class MultipleErrors extends InvalidEmail | ||
{ | ||
const CODE = 999; | ||
const REASON = "Accumulated errors for multiple validations"; | ||
/** | ||
* @var array | ||
*/ | ||
private $errors = []; | ||
|
||
public function __construct(array $errors) | ||
{ | ||
$this->errors = $errors; | ||
parent::__construct(); | ||
} | ||
|
||
public function getErrors() | ||
{ | ||
return $this->errors; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Egulias/EmailValidator/Validation/MultipleValidationWithAnd.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Egulias\EmailValidator\Validation; | ||
|
||
use Egulias\EmailValidator\EmailLexer; | ||
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList; | ||
|
||
class MultipleValidationWithAnd implements EmailValidation | ||
{ | ||
private $validations = []; | ||
private $warnings = []; | ||
private $error; | ||
|
||
public function __construct(array $validations) | ||
{ | ||
if (count($validations) == 0) { | ||
throw new EmptyValidationList(); | ||
} | ||
|
||
$this->validations = $validations; | ||
} | ||
|
||
public function isValid($email, EmailLexer $emailLexer) | ||
{ | ||
$result = true; | ||
$errors = []; | ||
foreach ($this->validations as $validation) { | ||
$result = $result && $validation->isValid($email, $emailLexer); | ||
$this->warnings = array_merge($this->warnings, $validation->getWarnings()); | ||
$errors[] = $validation->getError(); | ||
} | ||
$this->error = new MultipleErrors($errors); | ||
|
||
return $result; | ||
} | ||
|
||
public function getError() | ||
{ | ||
return $this->error; | ||
} | ||
|
||
public function getWarnings() | ||
{ | ||
return $this->warnings; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
tests/egulias/Tests/EmailValidator/Validation/MultipleValidationWitAndTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Egulias\Tests\EmailValidator\Validation; | ||
|
||
use Egulias\EmailValidator\Exception\CommaInDomain; | ||
use Egulias\EmailValidator\Exception\NoDomainPart; | ||
use Egulias\EmailValidator\Validation\MultipleErrors; | ||
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd; | ||
use Egulias\EmailValidator\Warning\AddressLiteral; | ||
use Egulias\EmailValidator\Warning\DomainLiteral; | ||
|
||
class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testUsesAndLogicalOperation() | ||
{ | ||
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); | ||
$validationTrue = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); | ||
$validationTrue->expects($this->any())->method("isValid")->willReturn(true); | ||
$validationTrue->expects($this->any())->method("getWarnings")->willReturn([]); | ||
$validationFalse = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); | ||
$validationFalse->expects($this->any())->method("isValid")->willReturn(false); | ||
$validationFalse->expects($this->any())->method("getWarnings")->willReturn([]); | ||
$multipleValidation = new MultipleValidationWithAnd([$validationTrue, $validationFalse]); | ||
$this->assertFalse($multipleValidation->isValid("exmpale@example.com", $lexer)); | ||
} | ||
|
||
/** | ||
* @expectedException Egulias\EmailValidator\Validation\Exception\EmptyValidationList | ||
*/ | ||
public function testEmptyListIsNotAllowed() | ||
{ | ||
new MultipleValidationWithAnd([]); | ||
} | ||
|
||
public function testAccumulatesWarnings() | ||
{ | ||
$warnings1 = [ | ||
AddressLiteral::CODE => new AddressLiteral() | ||
]; | ||
$warnings2 = [ | ||
DomainLiteral::CODE => new DomainLiteral() | ||
]; | ||
$expectedResult = array_merge($warnings1, $warnings2); | ||
|
||
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); | ||
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); | ||
$validation1->expects($this->any())->method("isValid")->willReturn(true); | ||
$validation1->expects($this->once())->method("getWarnings")->willReturn($warnings1); | ||
|
||
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); | ||
$validation2->expects($this->any())->method("isValid")->willReturn(false); | ||
$validation2->expects($this->once())->method("getWarnings")->willReturn($warnings2); | ||
|
||
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); | ||
$multipleValidation->isValid("example@example.com", $lexer); | ||
$this->assertEquals($expectedResult, $multipleValidation->getWarnings()); | ||
} | ||
|
||
public function testGathersAllTheErrors() | ||
{ | ||
$error1 = new CommaInDomain(); | ||
$error2 = new NoDomainPart(); | ||
|
||
$expectedResult = new MultipleErrors([$error1, $error2]); | ||
|
||
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); | ||
|
||
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); | ||
$validation1->expects($this->any())->method("isValid")->willReturn(true); | ||
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); | ||
$validation1->expects($this->once())->method("getError")->willReturn($error1); | ||
|
||
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); | ||
$validation2->expects($this->any())->method("isValid")->willReturn(false); | ||
$validation2->expects($this->once())->method("getWarnings")->willReturn([]); | ||
$validation2->expects($this->once())->method("getError")->willReturn($error2); | ||
|
||
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); | ||
$multipleValidation->isValid("example@example.com", $lexer); | ||
$this->assertEquals($expectedResult, $multipleValidation->getError()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters