mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-08-31 20:50:01 +00:00
Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8790f59415 | |||
| 114b663f20 | |||
| a0e0fadfce | |||
| 74c0f07434 | |||
| ad1a0e9773 | |||
| aeb6851388 | |||
| a23fcc1315 | |||
| 1bec00a100 | |||
| 3abad65421 | |||
| 3fdfa261ad | |||
| 14ca3785c1 | |||
| bc31baa11e | |||
| c0c888ae10 | |||
| 181a2fc726 | |||
| a1e85fa9fb | |||
| 7ff8caaae5 | |||
| 5d56347ddf | |||
| 80ad87acdd | |||
| 30562b69fc | |||
| cbb8b9e152 | |||
| cf670406e5 | |||
| 4d446bf971 | |||
| 4f0f9023f0 | |||
| 9160e26b13 | |||
| eb036b7d1c |
@@ -1,2 +1,3 @@
|
||||
report/
|
||||
vendor/
|
||||
.idea
|
||||
|
||||
+3
-2
@@ -6,6 +6,8 @@ php:
|
||||
- 5.5
|
||||
- 5.6
|
||||
- 7.0
|
||||
- 7.1
|
||||
- 7.2
|
||||
- hhvm
|
||||
|
||||
env:
|
||||
@@ -27,8 +29,7 @@ install:
|
||||
|
||||
script:
|
||||
- mkdir -p build/logs
|
||||
- phpunit --coverage-clover build/logs/clover.xml
|
||||
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
|
||||
|
||||
after_script:
|
||||
- php vendor/bin/coveralls
|
||||
|
||||
|
||||
@@ -67,8 +67,6 @@ class EmailLexer extends AbstractLexer
|
||||
"\n" => self::S_LF,
|
||||
"\r\n" => self::CRLF,
|
||||
'IPv6' => self::S_IPV6TAG,
|
||||
'<' => self::S_LOWERTHAN,
|
||||
'>' => self::S_GREATERTHAN,
|
||||
'{' => self::S_OPENQBRACKET,
|
||||
'}' => self::S_CLOSEQBRACKET,
|
||||
'' => self::S_EMPTY,
|
||||
|
||||
@@ -11,7 +11,7 @@ class EmailValidator
|
||||
* @var EmailLexer
|
||||
*/
|
||||
private $lexer;
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
@@ -21,7 +21,7 @@ class EmailValidator
|
||||
* @var InvalidEmail
|
||||
*/
|
||||
protected $error;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->lexer = new EmailLexer();
|
||||
@@ -37,7 +37,7 @@ class EmailValidator
|
||||
$isValid = $emailValidation->isValid($email, $this->lexer);
|
||||
$this->warnings = $emailValidation->getWarnings();
|
||||
$this->error = $emailValidation->getError();
|
||||
|
||||
|
||||
return $isValid;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ abstract class InvalidEmail extends \InvalidArgumentException
|
||||
{
|
||||
const REASON = "Invalid email";
|
||||
const CODE = 0;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(static::REASON, static::CODE);
|
||||
|
||||
@@ -170,7 +170,7 @@ class DomainPart extends Parser
|
||||
|
||||
return $domain;
|
||||
}
|
||||
|
||||
|
||||
private function checkNotAllowedChars($token)
|
||||
{
|
||||
$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true];
|
||||
|
||||
@@ -44,9 +44,11 @@ class DNSCheckValidation implements EmailValidation
|
||||
|
||||
protected function checkDNS($host)
|
||||
{
|
||||
$host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46), '.') . '.';
|
||||
|
||||
$Aresult = true;
|
||||
$MXresult = checkdnsrr($host, 'MX');
|
||||
|
||||
|
||||
if (!$MXresult) {
|
||||
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
|
||||
$Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA');
|
||||
|
||||
@@ -4,18 +4,31 @@ namespace Egulias\EmailValidator\Validation;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Exception\InvalidEmail;
|
||||
use Egulias\EmailValidator\Warning\Warning;
|
||||
|
||||
interface EmailValidation
|
||||
{
|
||||
/**
|
||||
* Returns true if the given email is valid.
|
||||
*
|
||||
* @param string $email The email you want to validate.
|
||||
* @param EmailLexer $emailLexer The email lexer.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($email, EmailLexer $emailLexer);
|
||||
|
||||
/**
|
||||
* @return InvalidEmail
|
||||
* Returns the validation error.
|
||||
*
|
||||
* @return InvalidEmail|null
|
||||
*/
|
||||
public function getError();
|
||||
|
||||
/**
|
||||
* @return array of Warning
|
||||
* Returns the validation warnings.
|
||||
*
|
||||
* @return Warning[]
|
||||
*/
|
||||
public function getWarnings();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\EmailValidator\Validation\Error;
|
||||
|
||||
use Egulias\EmailValidator\Exception\InvalidEmail;
|
||||
|
||||
class RFCWarnings extends InvalidEmail
|
||||
{
|
||||
const CODE = 997;
|
||||
const REASON = 'Warnings were found.';
|
||||
}
|
||||
@@ -12,13 +12,13 @@ class MultipleErrors extends InvalidEmail
|
||||
* @var array
|
||||
*/
|
||||
private $errors = [];
|
||||
|
||||
|
||||
public function __construct(array $errors)
|
||||
{
|
||||
$this->errors = $errors;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
public function getErrors()
|
||||
{
|
||||
return $this->errors;
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Egulias\EmailValidator\Validation;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Exception\InvalidEmail;
|
||||
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
|
||||
|
||||
class MultipleValidationWithAnd implements EmailValidation
|
||||
@@ -49,7 +48,7 @@ class MultipleValidationWithAnd implements EmailValidation
|
||||
if (count($validations) == 0) {
|
||||
throw new EmptyValidationList();
|
||||
}
|
||||
|
||||
|
||||
$this->validations = $validations;
|
||||
$this->mode = $mode;
|
||||
}
|
||||
@@ -65,17 +64,29 @@ class MultipleValidationWithAnd implements EmailValidation
|
||||
$emailLexer->reset();
|
||||
$result = $result && $validation->isValid($email, $emailLexer);
|
||||
$this->warnings = array_merge($this->warnings, $validation->getWarnings());
|
||||
$errors[] = $validation->getError();
|
||||
$errors = $this->addNewError($validation->getError(), $errors);
|
||||
|
||||
if ($this->shouldStop($result)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->error = new MultipleErrors($errors);
|
||||
|
||||
|
||||
if (!empty($errors)) {
|
||||
$this->error = new MultipleErrors($errors);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function addNewError($possibleError, array $errors)
|
||||
{
|
||||
if (null !== $possibleError) {
|
||||
$errors[] = $possibleError;
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
private function shouldStop($result)
|
||||
{
|
||||
return !$result && $this->mode === self::STOP_ON_ERROR;
|
||||
|
||||
@@ -3,11 +3,39 @@
|
||||
namespace Egulias\EmailValidator\Validation;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Exception\InvalidEmail;
|
||||
use Egulias\EmailValidator\Validation\Error\RFCWarnings;
|
||||
|
||||
class NoRFCWarningsValidation extends RFCValidation
|
||||
{
|
||||
/**
|
||||
* @var InvalidEmail
|
||||
*/
|
||||
private $error;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isValid($email, EmailLexer $emailLexer)
|
||||
{
|
||||
return parent::isValid($email, $emailLexer) && empty($this->getWarnings());
|
||||
if (!parent::isValid($email, $emailLexer)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($this->getWarnings())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->error = new RFCWarnings();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error ?: parent::getError();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ class RFCValidation implements EmailValidation
|
||||
* @var InvalidEmail
|
||||
*/
|
||||
private $error;
|
||||
|
||||
|
||||
public function isValid($email, EmailLexer $emailLexer)
|
||||
{
|
||||
$this->parser = new EmailParser($emailLexer);
|
||||
@@ -32,7 +32,7 @@ class RFCValidation implements EmailValidation
|
||||
$this->error = $invalid;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
$this->warnings = $this->parser->getWarnings();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -14,15 +14,22 @@ class SpoofCheckValidation implements EmailValidation
|
||||
*/
|
||||
private $error;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if (!class_exists(Spoofchecker::class)) {
|
||||
throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__));
|
||||
}
|
||||
}
|
||||
|
||||
public function isValid($email, EmailLexer $emailLexer)
|
||||
{
|
||||
$checker = new Spoofchecker();
|
||||
$checker->setChecks(Spoofchecker::SINGLE_SCRIPT);
|
||||
|
||||
|
||||
if ($checker->isSuspicious($email)) {
|
||||
$this->error = new SpoofEmail();
|
||||
}
|
||||
|
||||
|
||||
return $this->error === null;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ abstract class Warning
|
||||
{
|
||||
return $this->rfcNumber;
|
||||
}
|
||||
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->message() . " rfc: " . $this->rfcNumber . "interal code: " . static::CODE;
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
#EmailValidator
|
||||
# EmailValidator
|
||||
[](https://travis-ci.org/egulias/EmailValidator) [](https://coveralls.io/r/egulias/EmailValidator?branch=master) [](https://scrutinizer-ci.com/g/egulias/EmailValidator/?branch=master) [](https://insight.sensiolabs.com/projects/22ba6692-9c02-42e5-a65d-1c5696bfffc6)
|
||||
=============================
|
||||
With the help of
|
||||
With the help of [PHPStorm](https://www.jetbrains.com/phpstorm/)
|
||||
|
||||

|
||||
##Installation##
|
||||
## Requirements ##
|
||||
|
||||
* [Composer](https://getcomposer.org) is required for installation
|
||||
* [Spoofchecking](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/SpoofCheckValidation.php) validation requires that your PHP system have the [PHP Internationalization Libraries](https://php.net/manual/en/book.intl.php) (also known as PHP Intl)
|
||||
|
||||
## Installation ##
|
||||
|
||||
Run the command below to install via Composer
|
||||
|
||||
```shell
|
||||
composer require egulias/email-validator "~2.0"
|
||||
composer require egulias/email-validator "~2.1"
|
||||
```
|
||||
|
||||
##Getting Started##
|
||||
## Getting Started ##
|
||||
`EmailValidator`requires you to decide which (or combination of them) validation/s strategy/ies you'd like to follow for each [validation](#available-validations).
|
||||
|
||||
A basic example with the RFC validation
|
||||
@@ -20,16 +24,17 @@ A basic example with the RFC validation
|
||||
<?php
|
||||
|
||||
use Egulias\EmailValidator\EmailValidator;
|
||||
use Egulias\EmailValidator\Validation\RFCValidation;
|
||||
|
||||
$validator = new EmailValidator();
|
||||
$validator->isValid("example@example.com", new RFCValidation()) //true
|
||||
$validator->isValid("example@example.com", new RFCValidation()); //true
|
||||
```
|
||||
|
||||
|
||||
###Available validations###
|
||||
### Available validations ###
|
||||
|
||||
1. [RFCValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/RFCValidation.php)
|
||||
2. [NoWarningsRFCValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/NoRFCWarningsValidation.php)
|
||||
2. [NoRFCWarningsValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/NoRFCWarningsValidation.php)
|
||||
3. [DNSCheckValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/DNSCheckValidation.php)
|
||||
4. [SpoofCheckValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/SpoofCheckValidation.php)
|
||||
5. [MultipleValidationWithAnd](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/MultipleValidationWithAnd.php)
|
||||
@@ -51,24 +56,24 @@ $validator = new EmailValidator();
|
||||
$multipleValidations = new MultipleValidationWithAnd([
|
||||
new RFCValidation(),
|
||||
new DNSCheckValidation()
|
||||
])
|
||||
$validator->isValid("example@example.com", $multipleValidations) //true
|
||||
]);
|
||||
$validator->isValid("example@example.com", $multipleValidations); //true
|
||||
```
|
||||
|
||||
###How to extend###
|
||||
### How to extend ###
|
||||
|
||||
It's easy! You just need to extend [EmailValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/EmailValidation.php) and you can use your own validation.
|
||||
It's easy! You just need to implement [EmailValidation](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/EmailValidation.php) and you can use your own validation.
|
||||
|
||||
|
||||
##Other Contributors##
|
||||
## Other Contributors ##
|
||||
(You can find current contributors [here](https://github.com/egulias/EmailValidator/graphs/contributors))
|
||||
|
||||
As this is a port from another library and work, here are other people related to the previous one:
|
||||
|
||||
* Ricard Clau [@ricardclau](http://github.com/ricardclau): Performance against PHP built-in filter_var
|
||||
* Josepf Bielawski [@stloyd](http://github.com/stloyd): For its first re-work of Dominic's lib
|
||||
* Dominic Sayers [@dominicsayers](http://github.com/dominicsayers): The original isemail function
|
||||
* Ricard Clau [@ricardclau](https://github.com/ricardclau): Performance against PHP built-in filter_var
|
||||
* Josepf Bielawski [@stloyd](https://github.com/stloyd): For its first re-work of Dominic's lib
|
||||
* Dominic Sayers [@dominicsayers](https://github.com/dominicsayers): The original isemail function
|
||||
|
||||
##License##
|
||||
## License ##
|
||||
Released under the MIT License attached with this code.
|
||||
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
namespace Egulias\Tests\EmailValidator;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class EmailLexerTests extends \PHPUnit_Framework_TestCase
|
||||
class EmailLexerTests extends TestCase
|
||||
{
|
||||
|
||||
public function testLexerExtendsLib()
|
||||
|
||||
@@ -4,24 +4,25 @@ namespace Egulias\Tests\EmailValidator;
|
||||
|
||||
use Egulias\EmailValidator\EmailValidator;
|
||||
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
class EmailValidatorTest extends TestCase
|
||||
{
|
||||
public function testValidationIsUsed()
|
||||
{
|
||||
$validator = new EmailValidator();
|
||||
$validation = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
||||
$validation = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
$validation->expects($this->once())->method("isValid")->willReturn(true);
|
||||
$validation->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||
$validation->expects($this->once())->method("getError")->willReturn(null);
|
||||
|
||||
$this->assertTrue($validator->isValid("example@example.com", $validation));
|
||||
}
|
||||
|
||||
|
||||
public function testMultipleValidation()
|
||||
{
|
||||
$validator = new EmailValidator();
|
||||
$validation = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
||||
$validation = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
$validation->expects($this->once())->method("isValid")->willReturn(true);
|
||||
$validation->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||
$validation->expects($this->once())->method("getError")->willReturn(null);
|
||||
|
||||
@@ -6,8 +6,9 @@ use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Exception\NoDNSRecord;
|
||||
use Egulias\EmailValidator\Validation\DNSCheckValidation;
|
||||
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DNSCheckValidationTest extends \PHPUnit_Framework_TestCase
|
||||
class DNSCheckValidationTest extends TestCase
|
||||
{
|
||||
public function validEmailsProvider()
|
||||
{
|
||||
@@ -23,6 +24,9 @@ class DNSCheckValidationTest extends \PHPUnit_Framework_TestCase
|
||||
['"Abc@def"@example.com'],
|
||||
['"Fred\ Bloggs"@example.com'],
|
||||
['"Joe.\\Blow"@example.com'],
|
||||
|
||||
// unicide
|
||||
['ñandu.cl'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -34,13 +38,13 @@ class DNSCheckValidationTest extends \PHPUnit_Framework_TestCase
|
||||
$validation = new DNSCheckValidation();
|
||||
$this->assertTrue($validation->isValid($validEmail, new EmailLexer()));
|
||||
}
|
||||
|
||||
|
||||
public function testInvalidDNS()
|
||||
{
|
||||
$validation = new DNSCheckValidation();
|
||||
$this->assertFalse($validation->isValid("example@invalid.example.com", new EmailLexer()));
|
||||
}
|
||||
|
||||
|
||||
public function testDNSWarnings()
|
||||
{
|
||||
$validation = new DNSCheckValidation();
|
||||
@@ -48,7 +52,7 @@ class DNSCheckValidationTest extends \PHPUnit_Framework_TestCase
|
||||
$validation->isValid("example@invalid.example.com", new EmailLexer());
|
||||
$this->assertEquals($expectedWarnings, $validation->getWarnings());
|
||||
}
|
||||
|
||||
|
||||
public function testNoDNSError()
|
||||
{
|
||||
$validation = new DNSCheckValidation();
|
||||
|
||||
@@ -7,8 +7,9 @@ use Egulias\EmailValidator\Validation\DNSCheckValidation;
|
||||
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
|
||||
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
|
||||
use Egulias\EmailValidator\Validation\RFCValidation;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class IsEmailFunctionTests extends \PHPUnit_Framework_TestCase
|
||||
class IsEmailFunctionTests extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider isEmailTestSuite
|
||||
@@ -28,8 +29,8 @@ class IsEmailFunctionTests extends \PHPUnit_Framework_TestCase
|
||||
public function isEmailTestSuite()
|
||||
{
|
||||
$testSuite = dirname(__FILE__) . '/../../resources/is_email_tests.xml';
|
||||
$document = new \DOMDocument();
|
||||
$document->load($testSuite);
|
||||
$document = new \DOMDocument();
|
||||
$document->load($testSuite);
|
||||
$elements = $document->getElementsByTagName('test');
|
||||
$tests = [];
|
||||
|
||||
@@ -40,5 +41,4 @@ class IsEmailFunctionTests extends \PHPUnit_Framework_TestCase
|
||||
|
||||
return $tests;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,16 +8,17 @@ use Egulias\EmailValidator\Validation\MultipleErrors;
|
||||
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
|
||||
use Egulias\EmailValidator\Warning\AddressLiteral;
|
||||
use Egulias\EmailValidator\Warning\DomainLiteral;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
||||
class MultipleValidationWitAndTest extends TestCase
|
||||
{
|
||||
public function testUsesAndLogicalOperation()
|
||||
{
|
||||
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer");
|
||||
$validationTrue = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
||||
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock();
|
||||
$validationTrue = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
$validationTrue->expects($this->any())->method("isValid")->willReturn(true);
|
||||
$validationTrue->expects($this->any())->method("getWarnings")->willReturn([]);
|
||||
$validationFalse = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
||||
$validationFalse = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
$validationFalse->expects($this->any())->method("isValid")->willReturn(false);
|
||||
$validationFalse->expects($this->any())->method("getWarnings")->willReturn([]);
|
||||
$multipleValidation = new MultipleValidationWithAnd([$validationTrue, $validationFalse]);
|
||||
@@ -32,6 +33,19 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
||||
new MultipleValidationWithAnd([]);
|
||||
}
|
||||
|
||||
public function testValidationIsValid()
|
||||
{
|
||||
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock();
|
||||
|
||||
$validation = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
$validation->expects($this->any())->method("isValid")->willReturn(true);
|
||||
$validation->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||
|
||||
$multipleValidation = new MultipleValidationWithAnd([$validation]);
|
||||
$this->assertTrue($multipleValidation->isValid("example@example.com", $lexer));
|
||||
$this->assertNull($multipleValidation->getError());
|
||||
}
|
||||
|
||||
public function testAccumulatesWarnings()
|
||||
{
|
||||
$warnings1 = [
|
||||
@@ -41,16 +55,17 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
||||
DomainLiteral::CODE => new DomainLiteral()
|
||||
];
|
||||
$expectedResult = array_merge($warnings1, $warnings2);
|
||||
|
||||
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer");
|
||||
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
||||
|
||||
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock();
|
||||
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
$validation1->expects($this->any())->method("isValid")->willReturn(true);
|
||||
$validation1->expects($this->once())->method("getWarnings")->willReturn($warnings1);
|
||||
|
||||
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
||||
|
||||
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
|
||||
$validation2->expects($this->any())->method("isValid")->willReturn(false);
|
||||
$validation2->expects($this->once())->method("getWarnings")->willReturn($warnings2);
|
||||
|
||||
|
||||
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]);
|
||||
$multipleValidation->isValid("example@example.com", $lexer);
|
||||
$this->assertEquals($expectedResult, $multipleValidation->getWarnings());
|
||||
@@ -60,17 +75,17 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$error1 = new CommaInDomain();
|
||||
$error2 = new NoDomainPart();
|
||||
|
||||
|
||||
$expectedResult = new MultipleErrors([$error1, $error2]);
|
||||
|
||||
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer");
|
||||
|
||||
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
||||
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock();
|
||||
|
||||
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
$validation1->expects($this->any())->method("isValid")->willReturn(true);
|
||||
$validation1->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||
$validation1->expects($this->once())->method("getError")->willReturn($error1);
|
||||
|
||||
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
||||
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
$validation2->expects($this->any())->method("isValid")->willReturn(false);
|
||||
$validation2->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||
$validation2->expects($this->once())->method("getError")->willReturn($error2);
|
||||
@@ -86,14 +101,14 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$expectedResult = new MultipleErrors([$error]);
|
||||
|
||||
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer");
|
||||
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock();
|
||||
|
||||
$validation1 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
||||
$validation1 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
$validation1->expects($this->any())->method("isValid")->willReturn(false);
|
||||
$validation1->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||
$validation1->expects($this->once())->method("getError")->willReturn($error);
|
||||
|
||||
$validation2 = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
||||
$validation2 = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||
$validation2->expects($this->never())->method("isValid");
|
||||
$validation2->expects($this->never())->method("getWarnings");
|
||||
$validation2->expects($this->never())->method("getError");
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\Tests\EmailValidator\Validation;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Exception\NoDomainPart;
|
||||
use Egulias\EmailValidator\Validation\Error\RFCWarnings;
|
||||
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class NoRFCWarningsValidationTest extends TestCase
|
||||
{
|
||||
public function testInvalidEmailIsInvalid()
|
||||
{
|
||||
$validation = new NoRFCWarningsValidation();
|
||||
|
||||
$this->assertFalse($validation->isValid('non-email-string', new EmailLexer()));
|
||||
$this->assertInstanceOf(NoDomainPart::class, $validation->getError());
|
||||
}
|
||||
|
||||
public function testEmailWithWarningsIsInvalid()
|
||||
{
|
||||
$validation = new NoRFCWarningsValidation();
|
||||
|
||||
$this->assertFalse($validation->isValid(str_repeat('x', 254).'@example.com', new EmailLexer())); // too long email
|
||||
$this->assertInstanceOf(RFCWarnings::class, $validation->getError());
|
||||
}
|
||||
|
||||
public function testEmailWithoutWarningsIsValid()
|
||||
{
|
||||
$validation = new NoRFCWarningsValidation();
|
||||
|
||||
$this->assertTrue($validation->isValid('example@example.com', new EmailLexer()));
|
||||
$this->assertNull($validation->getError());
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\Tests\EmailValidator\Validation;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
|
||||
|
||||
class NoWarningsRFCValidationTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testEmailWithWarningsIsInvalid()
|
||||
{
|
||||
$validation = new NoRFCWarningsValidation();
|
||||
|
||||
$this->assertFalse($validation->isValid('examp"l"e@example.com', new EmailLexer()));
|
||||
}
|
||||
|
||||
public function testEmailWithoutWarningsIsValid()
|
||||
{
|
||||
$validation = new NoRFCWarningsValidation();
|
||||
|
||||
$this->assertTrue($validation->isValid('example@example.com', new EmailLexer()));
|
||||
}
|
||||
}
|
||||
@@ -35,8 +35,9 @@ use Egulias\EmailValidator\Warning\LabelTooLong;
|
||||
use Egulias\EmailValidator\Warning\LocalTooLong;
|
||||
use Egulias\EmailValidator\Warning\ObsoleteDTEXT;
|
||||
use Egulias\EmailValidator\Warning\QuotedString;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RFCValidationTest extends \PHPUnit_Framework_TestCase
|
||||
class RFCValidationTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var RFCValidation
|
||||
@@ -47,7 +48,7 @@ class RFCValidationTest extends \PHPUnit_Framework_TestCase
|
||||
* @var EmailLexer
|
||||
*/
|
||||
protected $lexer;
|
||||
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->validator = new RFCValidation();
|
||||
@@ -106,7 +107,7 @@ class RFCValidationTest extends \PHPUnit_Framework_TestCase
|
||||
$email = "\x80\x81\x82@\x83\x84\x85.\x86\x87\x88";
|
||||
$this->assertFalse($this->validator->isValid($email, $this->lexer));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidEmails
|
||||
*/
|
||||
@@ -219,16 +220,16 @@ class RFCValidationTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$this->assertTrue($this->validator->isValid($email, $this->lexer));
|
||||
$warnings = $this->validator->getWarnings();
|
||||
$this->assertTrue(
|
||||
count($warnings) === count($expectedWarnings),
|
||||
$this->assertCount(
|
||||
count($warnings), $expectedWarnings,
|
||||
"Expected: " . implode(",", $expectedWarnings) . " and got " . implode(",", $warnings)
|
||||
);
|
||||
|
||||
foreach ($warnings as $warning) {
|
||||
$this->assertTrue(isset($expectedWarnings[$warning->code()]));
|
||||
$this->assertArrayHasKey($warning->code(), $expectedWarnings);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function getInvalidEmailsWithWarnings()
|
||||
{
|
||||
return [
|
||||
|
||||
@@ -4,8 +4,9 @@ namespace Egulias\Tests\EmailValidator\Validation;
|
||||
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\Validation\SpoofCheckValidation;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SpoofCheckValidationTest extends \PHPUnit_Framework_TestCase
|
||||
class SpoofCheckValidationTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider validUTF8EmailsProvider
|
||||
@@ -14,17 +15,17 @@ class SpoofCheckValidationTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$this->markTestSkipped("Skipped for Travis CI since it is failing on this test for unknown reasons.");
|
||||
$validation = new SpoofCheckValidation();
|
||||
|
||||
|
||||
$this->assertTrue($validation->isValid($email, new EmailLexer()));
|
||||
}
|
||||
|
||||
|
||||
public function testEmailWithSpoofsIsInvalid()
|
||||
{
|
||||
$validation = new SpoofCheckValidation();
|
||||
|
||||
$this->assertFalse($validation->isValid("Кириллица"."latin漢字"."ひらがな"."カタカナ", new EmailLexer()));
|
||||
}
|
||||
|
||||
|
||||
public function validUTF8EmailsProvider()
|
||||
{
|
||||
return [
|
||||
|
||||
+5
-2
@@ -24,10 +24,13 @@
|
||||
"doctrine/lexer": "^1.0.1"
|
||||
},
|
||||
"require-dev" : {
|
||||
"satooshi/php-coveralls": "dev-master",
|
||||
"phpunit/phpunit": "^4.8.0",
|
||||
"satooshi/php-coveralls": "^1.0.1",
|
||||
"phpunit/phpunit": "^4.8.35||^5.7||^6.0",
|
||||
"dominicsayers/isemail": "dev-master"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-intl": "PHP Internationalization Libraries are required to use the SpoofChecking validation"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Egulias\\EmailValidator\\": "EmailValidator"
|
||||
|
||||
Generated
-1661
File diff suppressed because it is too large
Load Diff
@@ -1,23 +1,23 @@
|
||||
Email length
|
||||
------------
|
||||
http://tools.ietf.org/html/rfc5321#section-4.1.2
|
||||
https://tools.ietf.org/html/rfc5321#section-4.1.2
|
||||
Forward-path = Path
|
||||
|
||||
Path = "<" [ A-d-l ":" ] Mailbox ">"
|
||||
|
||||
http://tools.ietf.org/html/rfc5321#section-4.5.3.1.3
|
||||
http://tools.ietf.org/html/rfc1035#section-2.3.4
|
||||
https://tools.ietf.org/html/rfc5321#section-4.5.3.1.3
|
||||
https://tools.ietf.org/html/rfc1035#section-2.3.4
|
||||
|
||||
DNS
|
||||
---
|
||||
|
||||
http://tools.ietf.org/html/rfc5321#section-2.3.5
|
||||
https://tools.ietf.org/html/rfc5321#section-2.3.5
|
||||
Names that can
|
||||
be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
|
||||
in Section 5) are permitted, as are CNAME RRs whose targets can be
|
||||
resolved, in turn, to MX or address RRs.
|
||||
|
||||
http://tools.ietf.org/html/rfc5321#section-5.1
|
||||
https://tools.ietf.org/html/rfc5321#section-5.1
|
||||
The lookup first attempts to locate an MX record associated with the
|
||||
name. If a CNAME record is found, the resulting name is processed as
|
||||
if it were the initial name. ... If an empty list of MXs is returned,
|
||||
@@ -37,7 +37,7 @@ status to these addresses on the basis that they are more likely
|
||||
to be typos than genuine addresses (unless we've already
|
||||
established that the domain does have an MX record)
|
||||
|
||||
http://tools.ietf.org/html/rfc5321#section-2.3.5
|
||||
https://tools.ietf.org/html/rfc5321#section-2.3.5
|
||||
In the case
|
||||
of a top-level domain used by itself in an email address, a single
|
||||
string is used without any dots. This makes the requirement,
|
||||
@@ -57,7 +57,7 @@ an ambiguity. The most authoritative statement on TLD formats that
|
||||
the author can find is in a (rejected!) erratum to RFC 1123
|
||||
submitted by John Klensin, the author of RFC 5321:
|
||||
|
||||
http://www.rfc-editor.org/errata_search.php?rfc=1123&eid=1353
|
||||
https://www.rfc-editor.org/errata_search.php?rfc=1123&eid=1353
|
||||
However, a valid host name can never have the dotted-decimal
|
||||
form #.#.#.#, since this change does not permit the highest-level
|
||||
component label to start with a digit even if it is not all-numeric.
|
||||
@@ -66,4 +66,4 @@ Comments
|
||||
--------
|
||||
Comments at the start of the domain are deprecated in the text
|
||||
Comments at the start of a subdomain are obs-domain
|
||||
(http://tools.ietf.org/html/rfc5322#section-3.4.1)
|
||||
(https://tools.ietf.org/html/rfc5322#section-3.4.1)
|
||||
|
||||
Reference in New Issue
Block a user