diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 5025c3a..c272762 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -7,7 +7,7 @@ build: tests: override: - - command: 'vendor/bin/phpunit --coverage-clover=clover.xml' + command: 'vendor/bin/phpunit --coverage-clover=clover.xml --exclude-group slow' coverage: file: 'clover.xml' format: 'clover' diff --git a/.travis.yml b/.travis.yml index d075c28..f8c1e37 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,7 +38,7 @@ before_script: - mkdir -p build/logs script: - - vendor/bin/phpunit --coverage-clover build/logs/clover.xml + - vendor/bin/phpunit --coverage-clover build/logs/clover.xml --exclude-group slow - if [ "$psalm" = "yes" ]; then vendor/bin/psalm; fi after_script: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b3f0a30..253eb2c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,17 @@ - diff --git a/src/EmailLexer.php b/src/EmailLexer.php index 853f100..fdf879f 100644 --- a/src/EmailLexer.php +++ b/src/EmailLexer.php @@ -125,6 +125,7 @@ class EmailLexer extends AbstractLexer * @var array * * @psalm-var array{value:string, type:null|int, position:int} + * @psalm-suppress NonInvariantDocblockPropertyType */ public $token; diff --git a/src/Result/Reason/UnableToGetDNSRecord.php b/src/Result/Reason/UnableToGetDNSRecord.php new file mode 100644 index 0000000..f178b1a --- /dev/null +++ b/src/Result/Reason/UnableToGetDNSRecord.php @@ -0,0 +1,19 @@ +error = new InvalidEmail(new UnableToGetDNSRecord(), ''); + + return false; + } finally { + restore_error_handler(); + } // No MX, A or AAAA DNS records - if (empty($dnsRecords)) { + if ($dnsRecords === [] || $dnsRecords === false) { $this->error = new InvalidEmail(new ReasonNoDNSRecord(), ''); return false; } diff --git a/tests/EmailValidator/Validation/DNSCheckValidationTest.php b/tests/EmailValidator/Validation/DNSCheckValidationTest.php index 624b172..bd2fe3d 100644 --- a/tests/EmailValidator/Validation/DNSCheckValidationTest.php +++ b/tests/EmailValidator/Validation/DNSCheckValidationTest.php @@ -2,14 +2,15 @@ namespace Egulias\EmailValidator\Tests\EmailValidator\Validation; -use PHPUnit\Framework\TestCase; use Egulias\EmailValidator\EmailLexer; use Egulias\EmailValidator\Result\InvalidEmail; -use Egulias\EmailValidator\Warning\NoDNSMXRecord; -use Egulias\EmailValidator\Validation\DNSCheckValidation; use Egulias\EmailValidator\Result\Reason\DomainAcceptsNoMail; use Egulias\EmailValidator\Result\Reason\LocalOrReservedDomain; use Egulias\EmailValidator\Result\Reason\NoDNSRecord; +use Egulias\EmailValidator\Result\Reason\UnableToGetDNSRecord; +use Egulias\EmailValidator\Validation\DNSCheckValidation; +use Egulias\EmailValidator\Warning\NoDNSMXRecord; +use PHPUnit\Framework\TestCase; class DNSCheckValidationTest extends TestCase { @@ -92,7 +93,7 @@ class DNSCheckValidationTest extends TestCase public function testDNSWarnings() { - $this->markTestSkipped('Need to found a domain with AAAA redords and no MX that fails later in the validations'); + $this->markTestSkipped('Need to found a domain with AAAA records and no MX that fails later in the validations'); $validation = new DNSCheckValidation(); $expectedWarnings = [NoDNSMXRecord::CODE => new NoDNSMXRecord()]; $validation->isValid("example@invalid.example.com", new EmailLexer()); @@ -106,4 +107,22 @@ class DNSCheckValidationTest extends TestCase $validation->isValid("example@invalid.example.com", new EmailLexer()); $this->assertEquals($expectedError, $validation->getError()); } + + /** + * @group slow + */ + public function testUnableToGetDNSRecord() + { + error_reporting(\E_ALL); + + // UnableToGetDNSRecord raises on network errors (e.g. timeout) that we can‘t emulate in tests (for sure), + // but we can try to get timeout error by trying to fetch all DNS records + $validation = new class extends DNSCheckValidation { + protected const DNS_RECORD_TYPES_TO_CHECK = \DNS_ALL; + }; + $expectedError = new InvalidEmail(new UnableToGetDNSRecord(), ''); + + $validation->isValid('example@invalid.example.com', new EmailLexer()); + $this->assertEquals($expectedError, $validation->getError()); + } }