|
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
|
|
|
public function testAccumulatesWarnings() |
|
36
|
|
|
{ |
|
37
|
|
|
$warnings1 = [ |
|
38
|
|
|
AddressLiteral::CODE => new AddressLiteral() |
|
39
|
|
|
]; |
|
40
|
|
|
$warnings2 = [ |
|
41
|
|
|
DomainLiteral::CODE => new DomainLiteral() |
|
42
|
|
|
]; |
|
43
|
|
|
$expectedResult = array_merge($warnings1, $warnings2); |
|
44
|
|
|
|
|
45
|
|
|
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); |
|
46
|
|
|
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
|
47
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(true); |
|
48
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn($warnings1); |
|
49
|
|
|
|
|
50
|
|
|
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
|
51
|
|
|
$validation2->expects($this->any())->method("isValid")->willReturn(false); |
|
52
|
|
|
$validation2->expects($this->once())->method("getWarnings")->willReturn($warnings2); |
|
53
|
|
|
|
|
54
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); |
|
55
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
56
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getWarnings()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testGathersAllTheErrors() |
|
60
|
|
|
{ |
|
61
|
|
|
$error1 = new CommaInDomain(); |
|
62
|
|
|
$error2 = new NoDomainPart(); |
|
63
|
|
|
|
|
64
|
|
|
$expectedResult = new MultipleErrors([$error1, $error2]); |
|
65
|
|
|
|
|
66
|
|
|
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); |
|
67
|
|
|
|
|
68
|
|
|
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
|
69
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(true); |
|
70
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); |
|
71
|
|
|
$validation1->expects($this->once())->method("getError")->willReturn($error1); |
|
72
|
|
|
|
|
73
|
|
|
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
|
74
|
|
|
$validation2->expects($this->any())->method("isValid")->willReturn(false); |
|
75
|
|
|
$validation2->expects($this->once())->method("getWarnings")->willReturn([]); |
|
76
|
|
|
$validation2->expects($this->once())->method("getError")->willReturn($error2); |
|
77
|
|
|
|
|
78
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]); |
|
79
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
80
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getError()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testBreakOutOfLoopWhenError() |
|
84
|
|
|
{ |
|
85
|
|
|
$error = new CommaInDomain(); |
|
86
|
|
|
|
|
87
|
|
|
$expectedResult = new MultipleErrors([$error]); |
|
88
|
|
|
|
|
89
|
|
|
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer"); |
|
90
|
|
|
|
|
91
|
|
|
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
|
92
|
|
|
$validation1->expects($this->any())->method("isValid")->willReturn(false); |
|
93
|
|
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]); |
|
94
|
|
|
$validation1->expects($this->once())->method("getError")->willReturn($error); |
|
95
|
|
|
|
|
96
|
|
|
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation"); |
|
97
|
|
|
$validation2->expects($this->never())->method("isValid"); |
|
98
|
|
|
$validation2->expects($this->never())->method("getWarnings"); |
|
99
|
|
|
$validation2->expects($this->never())->method("getError"); |
|
100
|
|
|
|
|
101
|
|
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2], true); |
|
|
|
|
|
|
102
|
|
|
$multipleValidation->isValid("[email protected]", $lexer); |
|
103
|
|
|
$this->assertEquals($expectedResult, $multipleValidation->getError()); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
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: