1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\Tests\EmailValidator; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailValidator; |
6
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
7
|
|
|
use Egulias\EmailValidator\Validation\EmailValidation; |
8
|
|
|
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd; |
9
|
|
|
use Egulias\Tests\EmailValidator\Dummy\DummyReason; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class EmailValidatorTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
|
16
|
|
View Code Duplication |
public function testValidationIsUsed() |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$invalidEmail = new InvalidEmail(new DummyReason(), ''); |
19
|
|
|
$validator = new EmailValidator(); |
20
|
|
|
$validation = $this->getMockBuilder(EmailValidation::class)->getMock(); |
21
|
|
|
$validation->expects($this->once())->method("isValid")->willReturn(true); |
22
|
|
|
$validation->expects($this->once())->method("getWarnings")->willReturn([]); |
23
|
|
|
$validation->expects($this->once())->method("getError")->willReturn($invalidEmail); |
24
|
|
|
|
25
|
|
|
$this->assertTrue($validator->isValid("[email protected]", $validation)); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
public function testMultipleValidation() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$invalidEmail = new InvalidEmail(new DummyReason(), ''); |
31
|
|
|
$validator = new EmailValidator(); |
32
|
|
|
$validation = $this->getMockBuilder(EmailValidation::class)->getMock(); |
33
|
|
|
$validation->expects($this->once())->method("isValid")->willReturn(true); |
34
|
|
|
$validation->expects($this->once())->method("getWarnings")->willReturn([]); |
35
|
|
|
$validation->expects($this->exactly(2))->method("getError")->willReturn($invalidEmail); |
36
|
|
|
$multiple = new MultipleValidationWithAnd([$validation]); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$this->assertTrue($validator->isValid("[email protected]", $multiple)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testValidationIsFalse() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$invalidEmail = new InvalidEmail(new DummyReason(), ''); |
44
|
|
|
$validator = new EmailValidator(); |
45
|
|
|
$validation = $this->getMockBuilder(EmailValidation::class)->getMock(); |
46
|
|
|
$validation->expects($this->once())->method("isValid")->willReturn(false); |
47
|
|
|
$validation->expects($this->once())->method("getWarnings")->willReturn([]); |
48
|
|
|
$validation->expects($this->once())->method("getError")->willReturn($invalidEmail); |
49
|
|
|
|
50
|
|
|
$this->assertFalse($validator->isValid("[email protected]", $validation)); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
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.