1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\Tests\EmailValidator\Validation; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailValidator; |
6
|
|
|
use Egulias\EmailValidator\Exception\CommaInDomain; |
7
|
|
|
use Egulias\EmailValidator\Exception\NoDomainPart; |
8
|
|
|
use Egulias\EmailValidator\Validation\MultipleErrors; |
9
|
|
|
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd; |
10
|
|
|
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation; |
11
|
|
|
use Egulias\EmailValidator\Validation\RFCValidation; |
12
|
|
|
use Egulias\EmailValidator\Warning\AddressLiteral; |
13
|
|
|
use Egulias\EmailValidator\Warning\DomainLiteral; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
class MultipleValidationWithAndTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
public function testUsesAndLogicalOperation() |
19
|
|
|
{ |
20
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
21
|
|
|
$validationTrue = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
22
|
|
|
$validationTrue->expects($this->any())->method("isValid")->willReturn(true); |
23
|
|
|
$validationTrue->expects($this->any())->method("getWarnings")->willReturn([]); |
24
|
|
|
$validationFalse = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
25
|
|
|
$validationFalse->expects($this->any())->method("isValid")->willReturn(false); |
26
|
|
|
$validationFalse->expects($this->any())->method("getWarnings")->willReturn([]); |
27
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validationTrue, $validationFalse]); |
|
|
|
|
28
|
|
|
$this->assertFalse($multipleValidation->isValid("[email protected]", $lexer)); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @expectedException \Egulias\EmailValidator\Validation\Exception\EmptyValidationList |
33
|
|
|
*/ |
34
|
|
|
public function testEmptyListIsNotAllowed() |
35
|
|
|
{ |
36
|
|
|
new MultipleValidationWithAnd([]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function testValidationIsValid() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
42
|
|
|
|
43
|
|
|
$validation = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
44
|
|
|
$validation->expects($this->any())->method("isValid")->willReturn(true); |
45
|
|
|
$validation->expects($this->once())->method("getWarnings")->willReturn([]); |
46
|
|
|
|
47
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation]); |
|
|
|
|
48
|
|
|
$this->assertTrue($multipleValidation->isValid("[email protected]", $lexer)); |
|
|
|
|
49
|
|
|
$this->assertNull($multipleValidation->getError()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testAccumulatesWarnings() |
53
|
|
|
{ |
54
|
|
|
$warnings1 = [ |
55
|
|
|
AddressLiteral::CODE => new AddressLiteral() |
56
|
|
|
]; |
57
|
|
|
$warnings2 = [ |
58
|
|
|
DomainLiteral::CODE => new DomainLiteral() |
59
|
|
|
]; |
60
|
|
|
$expectedResult = array_merge($warnings1, $warnings2); |
61
|
|
|
|
62
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
63
|
|
|
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
64
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(true); |
65
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn($warnings1); |
66
|
|
|
|
67
|
|
|
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
68
|
|
|
|
69
|
|
|
$validation2->expects($this->any())->method("isValid")->willReturn(false); |
70
|
|
|
$validation2->expects($this->once())->method("getWarnings")->willReturn($warnings2); |
71
|
|
|
|
72
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); |
|
|
|
|
73
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
|
|
|
74
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getWarnings()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testGathersAllTheErrors() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$error1 = new CommaInDomain(); |
80
|
|
|
$error2 = new NoDomainPart(); |
81
|
|
|
|
82
|
|
|
$expectedResult = new MultipleErrors([$error1, $error2]); |
83
|
|
|
|
84
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
85
|
|
|
|
86
|
|
|
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
87
|
|
|
$validation1->expects($this->once())->method("isValid")->willReturn(false); |
88
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); |
89
|
|
|
$validation1->expects($this->once())->method("getError")->willReturn($error1); |
90
|
|
|
|
91
|
|
|
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
92
|
|
|
$validation2->expects($this->once())->method("isValid")->willReturn(false); |
93
|
|
|
$validation2->expects($this->once())->method("getWarnings")->willReturn([]); |
94
|
|
|
$validation2->expects($this->once())->method("getError")->willReturn($error2); |
95
|
|
|
|
96
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); |
|
|
|
|
97
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
|
|
|
98
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getError()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
public function testStopsAfterFirstError() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$error1 = new CommaInDomain(); |
104
|
|
|
$error2 = new NoDomainPart(); |
105
|
|
|
|
106
|
|
|
$expectedResult = new MultipleErrors([$error1]); |
107
|
|
|
|
108
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
109
|
|
|
|
110
|
|
|
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
111
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(false); |
112
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); |
113
|
|
|
$validation1->expects($this->once())->method("getError")->willReturn($error1); |
114
|
|
|
|
115
|
|
|
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
116
|
|
|
$validation2->expects($this->any())->method("isValid")->willReturn(false); |
117
|
|
|
$validation2->expects($this->never())->method("getWarnings")->willReturn([]); |
118
|
|
|
$validation2->expects($this->never())->method("getError")->willReturn($error2); |
119
|
|
|
|
120
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2], MultipleValidationWithAnd::STOP_ON_ERROR); |
|
|
|
|
121
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
|
|
|
122
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getError()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testBreakOutOfLoopWhenError() |
126
|
|
|
{ |
127
|
|
|
$error = new CommaInDomain(); |
128
|
|
|
|
129
|
|
|
$expectedResult = new MultipleErrors([$error]); |
130
|
|
|
|
131
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
132
|
|
|
|
133
|
|
|
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
134
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(false); |
135
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); |
136
|
|
|
$validation1->expects($this->once())->method("getError")->willReturn($error); |
137
|
|
|
|
138
|
|
|
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
139
|
|
|
$validation2->expects($this->never())->method("isValid"); |
140
|
|
|
$validation2->expects($this->never())->method("getWarnings"); |
141
|
|
|
$validation2->expects($this->never())->method("getError"); |
142
|
|
|
|
143
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2], MultipleValidationWithAnd::STOP_ON_ERROR); |
|
|
|
|
144
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
|
|
|
145
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getError()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testBreakoutOnInvalidEmail() |
149
|
|
|
{ |
150
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
151
|
|
|
|
152
|
|
|
$validationNotCalled = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
153
|
|
|
$validationNotCalled->expects($this->never())->method("isValid"); |
154
|
|
|
$validationNotCalled->expects($this->never())->method("getWarnings"); |
155
|
|
|
$validationNotCalled->expects($this->never())->method("getError"); |
156
|
|
|
$multipleValidation = new MultipleValidationWithAnd([new RFCValidation(), $validationNotCalled], MultipleValidationWithAnd::STOP_ON_ERROR); |
|
|
|
|
157
|
|
|
$this->assertFalse($multipleValidation->isValid("invalid-email", $lexer)); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: