1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\Tests\EmailValidator\Validation; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailLexer; |
6
|
|
|
use Egulias\EmailValidator\Helper\SmtpSocketHelper; |
7
|
|
|
use Egulias\EmailValidator\Validation\Error\IllegalMailbox; |
8
|
|
|
use Egulias\EmailValidator\Validation\MailboxCheckValidation; |
9
|
|
|
use Egulias\EmailValidator\Warning\NoDNSMXRecord; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class MailboxCheckValidationTest extends TestCase |
13
|
|
|
{ |
14
|
|
View Code Duplication |
public function testValidMailbox() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$socketHelperMock = $this->getMockBuilder(SmtpSocketHelper::class) |
17
|
|
|
->disableOriginalConstructor() |
18
|
|
|
->getMock(); |
19
|
|
|
|
20
|
|
|
$socketHelperMock |
21
|
|
|
->expects($this->any()) |
22
|
|
|
->method('isResource') |
23
|
|
|
->willReturn(true) |
24
|
|
|
; |
25
|
|
|
|
26
|
|
|
$socketHelperMock |
27
|
|
|
->expects($this->any()) |
28
|
|
|
->method('getResponseCode') |
29
|
|
|
->willReturnOnConsecutiveCalls(220, 250, 250, 250) |
30
|
|
|
; |
31
|
|
|
|
32
|
|
|
$validation = new MailboxCheckValidation($socketHelperMock, '[email protected]'); |
33
|
|
|
|
34
|
|
|
$this->assertTrue($validation->isValid('[email protected]', new EmailLexer())); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function testDNSWarnings() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$validation = new MailboxCheckValidation(new SmtpSocketHelper(), '[email protected]'); |
40
|
|
|
$expectedWarnings = [NoDNSMXRecord::CODE => new NoDNSMXRecord()]; |
41
|
|
|
$validation->isValid('[email protected]', new EmailLexer()); |
42
|
|
|
$this->assertEquals($expectedWarnings, $validation->getWarnings()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function testIllegalMailboxError() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$socketHelperMock = $this->getMockBuilder(SmtpSocketHelper::class) |
48
|
|
|
->disableOriginalConstructor() |
49
|
|
|
->getMock(); |
50
|
|
|
|
51
|
|
|
$socketHelperMock |
52
|
|
|
->expects($this->any()) |
53
|
|
|
->method('isResource') |
54
|
|
|
->willReturn(true) |
55
|
|
|
; |
56
|
|
|
|
57
|
|
|
$socketHelperMock |
58
|
|
|
->expects($this->any()) |
59
|
|
|
->method('getResponseCode') |
60
|
|
|
->willReturnOnConsecutiveCalls(220, 250, 250, 550) |
61
|
|
|
; |
62
|
|
|
|
63
|
|
|
$validation = new MailboxCheckValidation($socketHelperMock, '[email protected]'); |
64
|
|
|
$validation->isValid('[email protected]', new EmailLexer()); |
65
|
|
|
$this->assertEquals(new IllegalMailbox(550), $validation->getError()); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
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.