Merge branch '3.x' of github.com:egulias/EmailValidator into message-id-validator

This commit is contained in:
Eduardo Gulias Davis
2021-03-06 18:02:37 +01:00
13 changed files with 89 additions and 24 deletions
+1 -1
View File
@@ -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'
+1 -1
View File
@@ -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:
+2 -2
View File
@@ -5,7 +5,7 @@
[![Test Coverage](https://scrutinizer-ci.com/g/egulias/EmailValidator/badges/coverage.png?b=3.x)](https://scrutinizer-ci.com/g/egulias/EmailValidator/?branch=3.x)
[![SymfonyInsight](https://insight.symfony.com/projects/22ba6692-9c02-42e5-a65d-1c5696bfffc6/mini.svg)](https://insight.symfony.com/projects/22ba6692-9c02-42e5-a65d-1c5696bfffc6)
A library for validatin emails against several RFC.
A library for validating emails against several RFC.
## Supported RFCs ##
@@ -16,7 +16,7 @@ This library aims to support RFCs:
* [6530](https://tools.ietf.org/html/rfc6530),
* [6531](https://tools.ietf.org/html/rfc6531),
* [6532](https://tools.ietf.org/html/rfc6532),
* [1030](https://tools.ietf.org/html/rfc1030)
* [1035](https://tools.ietf.org/html/rfc1035)
## Supported versions
+5 -2
View File
@@ -1,14 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
executionOrder="defects"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="EmailValidator Test Suite">
+1
View File
@@ -126,6 +126,7 @@ class EmailLexer extends AbstractLexer
*
* @psalm-suppress NonInvariantDocblockPropertyType
* @psalm-var array{value:string, type:null|int, position:int}
* @psalm-suppress NonInvariantDocblockPropertyType
*/
public $token;
@@ -0,0 +1,19 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
/**
* Used on SERVFAIL, TIMEOUT or other runtime and network errors
*/
class UnableToGetDNSRecord extends NoDNSRecord
{
public function code() : int
{
return 3;
}
public function description() : string
{
return 'Unable to get DNS records for the host';
}
}
+25 -4
View File
@@ -5,12 +5,18 @@ namespace Egulias\EmailValidator\Validation;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\DomainAcceptsNoMail;
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
use Egulias\EmailValidator\Result\Reason\LocalOrReservedDomain;
use Egulias\EmailValidator\Result\Reason\NoDNSRecord as ReasonNoDNSRecord;
use Egulias\EmailValidator\Result\Reason\UnableToGetDNSRecord;
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
class DNSCheckValidation implements EmailValidation
{
/**
* @var int
*/
protected const DNS_RECORD_TYPES_TO_CHECK = DNS_MX + DNS_A + DNS_AAAA;
/**
* @var array
*/
@@ -114,12 +120,27 @@ class DNSCheckValidation implements EmailValidation
*/
private function validateDnsRecords($host) : bool
{
// Get all MX, A and AAAA DNS records for host
$dnsRecords = @dns_get_record($host, DNS_MX + DNS_A + DNS_AAAA);
// A workaround to fix https://bugs.php.net/bug.php?id=73149
/** @psalm-suppress InvalidArgument */
set_error_handler(
static function (int $errorLevel, string $errorMessage): ?bool {
throw new \RuntimeException("Unable to get DNS record for the host: $errorMessage");
}
);
try {
// Get all MX, A and AAAA DNS records for host
$dnsRecords = dns_get_record($host, static::DNS_RECORD_TYPES_TO_CHECK);
} catch (\RuntimeException $exception) {
$this->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;
}
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Egulias\EmailValidator\Tests\EmailValidator;
use Egulias\EmailValidator\EmailLexer;
use PHPUnit\Framework\TestCase;
class EmailLexerTests extends TestCase
class EmailLexerTest extends TestCase
{
public function testLexerExtendsLib()
+2 -2
View File
@@ -6,7 +6,7 @@ use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\EmailParser;
use PHPUnit\Framework\TestCase;
class EmailParserTests extends TestCase
class EmailParserTest extends TestCase
{
public function emailPartsProvider()
{
@@ -28,4 +28,4 @@ class EmailParserTests extends TestCase
$this->assertEquals($local, $parser->getLocalPart());
$this->assertEquals($domain, $parser->getDomainPart());
}
}
}
+4 -2
View File
@@ -1,11 +1,13 @@
<?php
namespace Egulias\EmailValidator\Tests\EmailValidator;
use PHPUnit\Framework\TestCase;
class LexerTokensTest extends TestCase
{
public function testToken()
{
$this->markTestIncomplete("implement beter lexer tokens");
$this->markTestIncomplete("implement better lexer tokens");
}
}
}
@@ -7,7 +7,7 @@ use Egulias\EmailValidator\Result\MultipleErrors;
use Egulias\EmailValidator\Tests\EmailValidator\Dummy\AnotherDummyReason;
use Egulias\EmailValidator\Tests\EmailValidator\Dummy\DummyReason;
class MultipleErrorsTests extends TestCase
class MultipleErrorsTest extends TestCase
{
public function testRegisterSameReason()
{
@@ -35,4 +35,4 @@ class MultipleErrorsTests extends TestCase
$this->assertEquals($expectedReason, $multiError->description());
$this->assertEquals($error1, $multiError->reason());
}
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
<?php
namespace Egulias\Tests\EmailValidator\Validation;
namespace Egulias\EmailValidator\Tests\EmailValidator\Result;
use PHPUnit\Framework\TestCase;
use Egulias\EmailValidator\Result\ValidEmail;
@@ -32,4 +32,4 @@ class ResultTest extends TestCase
$this->assertEquals($expectedCode, $result->code());
$this->assertEquals($expectedDescription, $result->description());
}
}
}
@@ -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 cant 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());
}
}