1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\Tests\EmailValidator\Validation; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\Exception\CommaInDomain; |
6
|
|
|
use Egulias\EmailValidator\Exception\NoDomainPart; |
7
|
|
|
use Egulias\EmailValidator\Validation\MultipleErrors; |
8
|
|
|
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd; |
9
|
|
|
use Egulias\EmailValidator\Warning\AddressLiteral; |
10
|
|
|
use Egulias\EmailValidator\Warning\DomainLiteral; |
11
|
|
|
|
12
|
|
|
class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
public function testUsesAndLogicalOperation() |
15
|
|
|
{ |
16
|
|
|
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); |
17
|
|
|
$validationTrue = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
18
|
|
|
$validationTrue->expects($this->any())->method("isValid")->willReturn(true); |
19
|
|
|
$validationTrue->expects($this->any())->method("getWarnings")->willReturn([]); |
20
|
|
|
$validationFalse = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
21
|
|
|
$validationFalse->expects($this->any())->method("isValid")->willReturn(false); |
22
|
|
|
$validationFalse->expects($this->any())->method("getWarnings")->willReturn([]); |
23
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validationTrue, $validationFalse]); |
24
|
|
|
$this->assertFalse($multipleValidation->isValid("[email protected]", $lexer)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @expectedException \Egulias\EmailValidator\Validation\Exception\EmptyValidationList |
29
|
|
|
*/ |
30
|
|
|
public function testEmptyListIsNotAllowed() |
31
|
|
|
{ |
32
|
|
|
new MultipleValidationWithAnd([]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public function testValidationIsFine() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); |
38
|
|
|
|
39
|
|
|
$validation = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
40
|
|
|
$validation->expects($this->any())->method("isValid")->willReturn(true); |
41
|
|
|
$validation->expects($this->once())->method("getWarnings")->willReturn([]); |
42
|
|
|
|
43
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation]); |
44
|
|
|
$this->assertTrue($multipleValidation->isValid("[email protected]", $lexer)); |
45
|
|
|
$this->assertNull($multipleValidation->getError()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testAccumulatesWarnings() |
49
|
|
|
{ |
50
|
|
|
$warnings1 = [ |
51
|
|
|
AddressLiteral::CODE => new AddressLiteral() |
52
|
|
|
]; |
53
|
|
|
$warnings2 = [ |
54
|
|
|
DomainLiteral::CODE => new DomainLiteral() |
55
|
|
|
]; |
56
|
|
|
$expectedResult = array_merge($warnings1, $warnings2); |
57
|
|
|
|
58
|
|
|
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); |
59
|
|
|
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
60
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(true); |
61
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn($warnings1); |
62
|
|
|
|
63
|
|
|
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
64
|
|
|
$validation2->expects($this->any())->method("isValid")->willReturn(false); |
65
|
|
|
$validation2->expects($this->once())->method("getWarnings")->willReturn($warnings2); |
66
|
|
|
|
67
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); |
68
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
69
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getWarnings()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGathersAllTheErrors() |
73
|
|
|
{ |
74
|
|
|
$error1 = new CommaInDomain(); |
75
|
|
|
$error2 = new NoDomainPart(); |
76
|
|
|
|
77
|
|
|
$expectedResult = new MultipleErrors([$error1, $error2]); |
78
|
|
|
|
79
|
|
|
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); |
80
|
|
|
|
81
|
|
|
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
82
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(true); |
83
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); |
84
|
|
|
$validation1->expects($this->once())->method("getError")->willReturn($error1); |
85
|
|
|
|
86
|
|
|
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
87
|
|
|
$validation2->expects($this->any())->method("isValid")->willReturn(false); |
88
|
|
|
$validation2->expects($this->once())->method("getWarnings")->willReturn([]); |
89
|
|
|
$validation2->expects($this->once())->method("getError")->willReturn($error2); |
90
|
|
|
|
91
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); |
92
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
93
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getError()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testBreakOutOfLoopWhenError() |
97
|
|
|
{ |
98
|
|
|
$error = new CommaInDomain(); |
99
|
|
|
|
100
|
|
|
$expectedResult = new MultipleErrors([$error]); |
101
|
|
|
|
102
|
|
|
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); |
103
|
|
|
|
104
|
|
|
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
105
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(false); |
106
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); |
107
|
|
|
$validation1->expects($this->once())->method("getError")->willReturn($error); |
108
|
|
|
|
109
|
|
|
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
110
|
|
|
$validation2->expects($this->never())->method("isValid"); |
111
|
|
|
$validation2->expects($this->never())->method("getWarnings"); |
112
|
|
|
$validation2->expects($this->never())->method("getError"); |
113
|
|
|
|
114
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2], MultipleValidationWithAnd::STOP_ON_ERROR); |
115
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
116
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getError()); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.