Skip to content

Commit e96b638

Browse files
authored
Merge pull request #23 from daveearley/ISSUE-22-DNS-check-update
Check for DNS A/AAA records
2 parents 392bb7a + 4b29c05 commit e96b638

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/EmailAddress.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ private function getEmailPart(int $partNumber): string
3535
return (explode('@', $this->emailAddress))[$partNumber];
3636
}
3737

38-
public function getHostPart(): ?string
38+
public function getHostPart(bool $returnFqdn = false): ?string
3939
{
4040
if ($this->isValidEmailAddressFormat()) {
41-
return $this->getEmailPart(self::EMAIL_HOST_PART);
41+
return $this->getEmailPart(self::EMAIL_HOST_PART) . ($returnFqdn ? '.' : '');
4242
}
4343

4444
return null;

src/Validations/MxRecordsValidator.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@
66

77
class MxRecordsValidator extends Validator implements ValidatorInterface
88
{
9+
private const VALID_DNS_RECORDS = ['MX', 'A', 'AAAA', 'NS'];
10+
911
public function getValidatorName(): string
1012
{
1113
return 'valid_mx_records'; // @codeCoverageIgnore
1214
}
1315

1416
public function getResultResponse(): bool
1517
{
16-
if ($this->getEmailAddress()->isValidEmailAddressFormat()) {
17-
return $this->checkDns($this->getEmailAddress()->getHostPart(), 'MX');
18+
if (!$this->getEmailAddress()->isValidEmailAddressFormat()) {
19+
return false; // @codeCoverageIgnore
20+
}
21+
22+
$results = [];
23+
foreach (self::VALID_DNS_RECORDS as $dns) {
24+
$results[] = $this->checkDns($this->getEmailAddress()->getHostPart(true), $dns);
1825
}
1926

20-
return false; // @codeCoverageIgnore
27+
// To be considered valid we needs an NS record and at least one MX, A or AAA record
28+
return count(array_filter($results)) >= 2;
2129
}
2230

2331
protected function checkDns(string $host, string $type = null): bool

tests/Validations/MxRecordsValidatorTest.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ class MxRecordsValidatorTest extends TestCase
1616

1717
public function testMxRecordIsChecked(): void
1818
{
19-
$this->mxValidator->shouldReceive('checkDns')->with('dave@gmail.com', 'MX');
19+
foreach (['MX', 'AAAA', 'NS', 'A'] as $dns) {
20+
$this->mxValidator
21+
->shouldReceive('checkDns')
22+
->with('gmail.com.', $dns);
23+
}
24+
2025
$this->mxValidator->getResultResponse();
2126
}
2227

@@ -26,6 +31,11 @@ protected function setUp(): void
2631
new EmailAddress('dave@gmail.com'),
2732
])
2833
->shouldAllowMockingProtectedMethods()
29-
->makePartial();
34+
->shouldReceive('getResultResponse')
35+
->passthru()
36+
->getMock()
37+
->shouldReceive('getEmailAddress')
38+
->passthru()
39+
->getMock();
3040
}
3141
}

0 commit comments

Comments
 (0)