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
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
class MultipleValidationWithAndTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testUsesAndLogicalOperation() |
16
|
|
|
{ |
17
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
18
|
|
|
$validationTrue = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
19
|
|
|
$validationTrue->expects($this->any())->method("isValid")->willReturn(true); |
20
|
|
|
$validationTrue->expects($this->any())->method("getWarnings")->willReturn([]); |
21
|
|
|
$validationFalse = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
22
|
|
|
$validationFalse->expects($this->any())->method("isValid")->willReturn(false); |
23
|
|
|
$validationFalse->expects($this->any())->method("getWarnings")->willReturn([]); |
24
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validationTrue, $validationFalse]); |
|
|
|
|
25
|
|
|
$this->assertFalse($multipleValidation->isValid("[email protected]", $lexer)); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @expectedException \Egulias\EmailValidator\Validation\Exception\EmptyValidationList |
30
|
|
|
*/ |
31
|
|
|
public function testEmptyListIsNotAllowed() |
32
|
|
|
{ |
33
|
|
|
new MultipleValidationWithAnd([]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
public function testValidationIsValid() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
39
|
|
|
|
40
|
|
|
$validation = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
41
|
|
|
$validation->expects($this->any())->method("isValid")->willReturn(true); |
42
|
|
|
$validation->expects($this->once())->method("getWarnings")->willReturn([]); |
43
|
|
|
|
44
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation]); |
|
|
|
|
45
|
|
|
$this->assertTrue($multipleValidation->isValid("[email protected]", $lexer)); |
|
|
|
|
46
|
|
|
$this->assertNull($multipleValidation->getError()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testAccumulatesWarnings() |
50
|
|
|
{ |
51
|
|
|
$warnings1 = [ |
52
|
|
|
AddressLiteral::CODE => new AddressLiteral() |
53
|
|
|
]; |
54
|
|
|
$warnings2 = [ |
55
|
|
|
DomainLiteral::CODE => new DomainLiteral() |
56
|
|
|
]; |
57
|
|
|
$expectedResult = array_merge($warnings1, $warnings2); |
58
|
|
|
|
59
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
60
|
|
|
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
61
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(true); |
62
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn($warnings1); |
63
|
|
|
|
64
|
|
|
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
65
|
|
|
|
66
|
|
|
$validation2->expects($this->any())->method("isValid")->willReturn(false); |
67
|
|
|
$validation2->expects($this->once())->method("getWarnings")->willReturn($warnings2); |
68
|
|
|
|
69
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); |
|
|
|
|
70
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
|
|
|
71
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getWarnings()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function testGathersAllTheErrors() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$error1 = new CommaInDomain(); |
77
|
|
|
$error2 = new NoDomainPart(); |
78
|
|
|
|
79
|
|
|
$expectedResult = new MultipleErrors([$error1, $error2]); |
80
|
|
|
|
81
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
82
|
|
|
|
83
|
|
|
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
84
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(true); |
85
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); |
86
|
|
|
$validation1->expects($this->once())->method("getError")->willReturn($error1); |
87
|
|
|
|
88
|
|
|
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
89
|
|
|
$validation2->expects($this->any())->method("isValid")->willReturn(false); |
90
|
|
|
$validation2->expects($this->once())->method("getWarnings")->willReturn([]); |
91
|
|
|
$validation2->expects($this->once())->method("getError")->willReturn($error2); |
92
|
|
|
|
93
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); |
|
|
|
|
94
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
|
|
|
95
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getError()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
public function testStopsAfterFirstError() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$error1 = new CommaInDomain(); |
101
|
|
|
$error2 = new NoDomainPart(); |
102
|
|
|
|
103
|
|
|
$expectedResult = new MultipleErrors([$error1]); |
104
|
|
|
|
105
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
106
|
|
|
|
107
|
|
|
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
108
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(true); |
109
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); |
110
|
|
|
$validation1->expects($this->once())->method("getError")->willReturn($error1); |
111
|
|
|
|
112
|
|
|
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
113
|
|
|
$validation2->expects($this->any())->method("isValid")->willReturn(false); |
114
|
|
|
$validation2->expects($this->once())->method("getWarnings")->willReturn([]); |
115
|
|
|
$validation2->expects($this->once())->method("getError")->willReturn($error2); |
116
|
|
|
|
117
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2], MultipleValidationWithAnd::STOP_ON_ERROR); |
|
|
|
|
118
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
|
|
|
119
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getError()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testBreakOutOfLoopWhenError() |
123
|
|
|
{ |
124
|
|
|
$error = new CommaInDomain(); |
125
|
|
|
|
126
|
|
|
$expectedResult = new MultipleErrors([$error]); |
127
|
|
|
|
128
|
|
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock(); |
129
|
|
|
|
130
|
|
|
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
131
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(false); |
132
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); |
133
|
|
|
$validation1->expects($this->once())->method("getError")->willReturn($error); |
134
|
|
|
|
135
|
|
|
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock(); |
136
|
|
|
$validation2->expects($this->never())->method("isValid"); |
137
|
|
|
$validation2->expects($this->never())->method("getWarnings"); |
138
|
|
|
$validation2->expects($this->never())->method("getError"); |
139
|
|
|
|
140
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2], MultipleValidationWithAnd::STOP_ON_ERROR); |
|
|
|
|
141
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
|
|
|
142
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getError()); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
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: