Skip to content

Commit

Permalink
Merge pull request #23 from daveearley/ISSUE-22-DNS-check-update
Browse files Browse the repository at this point in the history
Check for DNS A/AAA records
  • Loading branch information
daveearley authored Jun 21, 2020
2 parents 392bb7a + 4b29c05 commit e96b638
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ private function getEmailPart(int $partNumber): string
return (explode('@', $this->emailAddress))[$partNumber];
}

public function getHostPart(): ?string
public function getHostPart(bool $returnFqdn = false): ?string
{
if ($this->isValidEmailAddressFormat()) {
return $this->getEmailPart(self::EMAIL_HOST_PART);
return $this->getEmailPart(self::EMAIL_HOST_PART) . ($returnFqdn ? '.' : '');
}

return null;
Expand Down
14 changes: 11 additions & 3 deletions src/Validations/MxRecordsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@

class MxRecordsValidator extends Validator implements ValidatorInterface
{
private const VALID_DNS_RECORDS = ['MX', 'A', 'AAAA', 'NS'];

public function getValidatorName(): string
{
return 'valid_mx_records'; // @codeCoverageIgnore
}

public function getResultResponse(): bool
{
if ($this->getEmailAddress()->isValidEmailAddressFormat()) {
return $this->checkDns($this->getEmailAddress()->getHostPart(), 'MX');
if (!$this->getEmailAddress()->isValidEmailAddressFormat()) {
return false; // @codeCoverageIgnore
}

$results = [];
foreach (self::VALID_DNS_RECORDS as $dns) {
$results[] = $this->checkDns($this->getEmailAddress()->getHostPart(true), $dns);
}

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

protected function checkDns(string $host, string $type = null): bool
Expand Down
14 changes: 12 additions & 2 deletions tests/Validations/MxRecordsValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ class MxRecordsValidatorTest extends TestCase

public function testMxRecordIsChecked(): void
{
$this->mxValidator->shouldReceive('checkDns')->with('dave@gmail.com', 'MX');
foreach (['MX', 'AAAA', 'NS', 'A'] as $dns) {
$this->mxValidator
->shouldReceive('checkDns')
->with('gmail.com.', $dns);
}

$this->mxValidator->getResultResponse();
}

Expand All @@ -26,6 +31,11 @@ protected function setUp(): void
new EmailAddress('dave@gmail.com'),
])
->shouldAllowMockingProtectedMethods()
->makePartial();
->shouldReceive('getResultResponse')
->passthru()
->getMock()
->shouldReceive('getEmailAddress')
->passthru()
->getMock();
}
}

0 comments on commit e96b638

Please sign in to comment.