-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement DNSCheckValidation (#250)
* Reimplement DNSCheckValidation to utilise get_dns_record and extend support for detection of Null MX records (RFC7505) and reserved, mDNS and private namespaces (RFC2606 & RFC6762) * Change valid email tests to use github.com instead of example.com as example.com now (correctly) fails the tests due to the Null MX case * Tweak to pass CI psalm issue * Encapsulate dns check code. Simplify local and reserved domain checks. Switch test domain * Set PHPDoc return type * Use strict comparisons
- Loading branch information
Showing
4 changed files
with
175 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Egulias\EmailValidator\Exception; | ||
|
||
class DomainAcceptsNoMail extends InvalidEmail | ||
{ | ||
const CODE = 154; | ||
const REASON = 'Domain accepts no mail (Null MX, RFC7505)'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Egulias\EmailValidator\Exception; | ||
|
||
class LocalOrReservedDomain extends InvalidEmail | ||
{ | ||
const CODE = 153; | ||
const REASON = 'Local, mDNS or reserved domain (RFC2606, RFC6762)'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
|
||
use Egulias\EmailValidator\EmailLexer; | ||
use Egulias\EmailValidator\Exception\NoDNSRecord; | ||
use Egulias\EmailValidator\Exception\LocalOrReservedDomain; | ||
use Egulias\EmailValidator\Exception\DomainAcceptsNoMail; | ||
use Egulias\EmailValidator\Validation\DNSCheckValidation; | ||
use Egulias\EmailValidator\Warning\NoDNSMXRecord; | ||
use PHPUnit\Framework\TestCase; | ||
|
@@ -14,22 +16,44 @@ public function validEmailsProvider() | |
{ | ||
return [ | ||
// dot-atom | ||
['Abc@example.com'], | ||
['ABC@EXAMPLE.COM'], | ||
['Abc.123@example.com'], | ||
['user+mailbox/department=shipping@example.com'], | ||
['!#$%&\'*+-/=?^_`.{|}~@example.com'], | ||
['Abc@ietf.org'], | ||
['ABC@ietf.org'], | ||
['Abc.123@ietf.org'], | ||
['user+mailbox/department=shipping@ietf.org'], | ||
['!#$%&\'*+-/=?^_`.{|}~@ietf.org'], | ||
|
||
// quoted string | ||
['"Abc@def"@example.com'], | ||
['"Fred\ Bloggs"@example.com'], | ||
['"Joe.\\Blow"@example.com'], | ||
['"Abc@def"@ietf.org'], | ||
['"Fred\ Bloggs"@ietf.org'], | ||
['"Joe.\\Blow"@ietf.org'], | ||
|
||
// unicide | ||
['ñandu.cl'], | ||
]; | ||
} | ||
|
||
public function localOrReservedEmailsProvider() | ||
{ | ||
return [ | ||
// Reserved Top Level DNS Names | ||
['test'], | ||
['example'], | ||
['invalid'], | ||
['localhost'], | ||
|
||
// mDNS | ||
['local'], | ||
|
||
// Private DNS Namespaces | ||
['intranet'], | ||
['internal'], | ||
['private'], | ||
['corp'], | ||
['home'], | ||
['lan'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider validEmailsProvider | ||
*/ | ||
|
@@ -45,13 +69,35 @@ public function testInvalidDNS() | |
$this->assertFalse($validation->isValid("[email protected]", new EmailLexer())); | ||
} | ||
|
||
/** | ||
* @dataProvider localOrReservedEmailsProvider | ||
*/ | ||
public function testLocalOrReservedDomainError($localOrReservedEmails) | ||
{ | ||
$validation = new DNSCheckValidation(); | ||
$expectedError = new LocalOrReservedDomain(); | ||
$validation->isValid($localOrReservedEmails, new EmailLexer()); | ||
$this->assertEquals($expectedError, $validation->getError()); | ||
} | ||
|
||
public function testDomainAcceptsNoMailError() | ||
{ | ||
$validation = new DNSCheckValidation(); | ||
$expectedError = new DomainAcceptsNoMail(); | ||
$isValidResult = $validation->isValid("[email protected]", new EmailLexer()); | ||
$this->assertEquals($expectedError, $validation->getError()); | ||
$this->assertFalse($isValidResult); | ||
} | ||
|
||
/* | ||
public function testDNSWarnings() | ||
{ | ||
$validation = new DNSCheckValidation(); | ||
$expectedWarnings = [NoDNSMXRecord::CODE => new NoDNSMXRecord()]; | ||
$validation->isValid("[email protected]", new EmailLexer()); | ||
$this->assertEquals($expectedWarnings, $validation->getWarnings()); | ||
} | ||
*/ | ||
|
||
public function testNoDNSError() | ||
{ | ||
|
@@ -60,4 +106,4 @@ public function testNoDNSError() | |
$validation->isValid("[email protected]", new EmailLexer()); | ||
$this->assertEquals($expectedError, $validation->getError()); | ||
} | ||
} | ||
} |