mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-09-06 07:29:10 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bc31baa11e | |||
| c0c888ae10 | |||
| 181a2fc726 | |||
| a1e85fa9fb | |||
| 7ff8caaae5 | |||
| 5d56347ddf | |||
| 80ad87acdd | |||
| 30562b69fc | |||
| cbb8b9e152 | |||
| cf670406e5 | |||
| 4d446bf971 | |||
| 4f0f9023f0 | |||
| 9160e26b13 | |||
| eb036b7d1c |
@@ -1,2 +1,3 @@
|
|||||||
report/
|
report/
|
||||||
vendor/
|
vendor/
|
||||||
|
.idea
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class EmailValidator
|
|||||||
* @var EmailLexer
|
* @var EmailLexer
|
||||||
*/
|
*/
|
||||||
private $lexer;
|
private $lexer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
@@ -21,7 +21,7 @@ class EmailValidator
|
|||||||
* @var InvalidEmail
|
* @var InvalidEmail
|
||||||
*/
|
*/
|
||||||
protected $error;
|
protected $error;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->lexer = new EmailLexer();
|
$this->lexer = new EmailLexer();
|
||||||
@@ -37,7 +37,7 @@ class EmailValidator
|
|||||||
$isValid = $emailValidation->isValid($email, $this->lexer);
|
$isValid = $emailValidation->isValid($email, $this->lexer);
|
||||||
$this->warnings = $emailValidation->getWarnings();
|
$this->warnings = $emailValidation->getWarnings();
|
||||||
$this->error = $emailValidation->getError();
|
$this->error = $emailValidation->getError();
|
||||||
|
|
||||||
return $isValid;
|
return $isValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ abstract class InvalidEmail extends \InvalidArgumentException
|
|||||||
{
|
{
|
||||||
const REASON = "Invalid email";
|
const REASON = "Invalid email";
|
||||||
const CODE = 0;
|
const CODE = 0;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct(static::REASON, static::CODE);
|
parent::__construct(static::REASON, static::CODE);
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ class DomainPart extends Parser
|
|||||||
|
|
||||||
return $domain;
|
return $domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkNotAllowedChars($token)
|
private function checkNotAllowedChars($token)
|
||||||
{
|
{
|
||||||
$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true];
|
$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true];
|
||||||
|
|||||||
@@ -44,9 +44,11 @@ class DNSCheckValidation implements EmailValidation
|
|||||||
|
|
||||||
protected function checkDNS($host)
|
protected function checkDNS($host)
|
||||||
{
|
{
|
||||||
|
$host = rtrim($host, '.') . '.';
|
||||||
|
|
||||||
$Aresult = true;
|
$Aresult = true;
|
||||||
$MXresult = checkdnsrr($host, 'MX');
|
$MXresult = checkdnsrr($host, 'MX');
|
||||||
|
|
||||||
if (!$MXresult) {
|
if (!$MXresult) {
|
||||||
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
|
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
|
||||||
$Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA');
|
$Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA');
|
||||||
|
|||||||
@@ -4,18 +4,31 @@ namespace Egulias\EmailValidator\Validation;
|
|||||||
|
|
||||||
use Egulias\EmailValidator\EmailLexer;
|
use Egulias\EmailValidator\EmailLexer;
|
||||||
use Egulias\EmailValidator\Exception\InvalidEmail;
|
use Egulias\EmailValidator\Exception\InvalidEmail;
|
||||||
|
use Egulias\EmailValidator\Warning\Warning;
|
||||||
|
|
||||||
interface EmailValidation
|
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);
|
public function isValid($email, EmailLexer $emailLexer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return InvalidEmail
|
* Returns the validation error.
|
||||||
|
*
|
||||||
|
* @return InvalidEmail|null
|
||||||
*/
|
*/
|
||||||
public function getError();
|
public function getError();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array of Warning
|
* Returns the validation warnings.
|
||||||
|
*
|
||||||
|
* @return Warning[]
|
||||||
*/
|
*/
|
||||||
public function getWarnings();
|
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
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $errors = [];
|
private $errors = [];
|
||||||
|
|
||||||
public function __construct(array $errors)
|
public function __construct(array $errors)
|
||||||
{
|
{
|
||||||
$this->errors = $errors;
|
$this->errors = $errors;
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getErrors()
|
public function getErrors()
|
||||||
{
|
{
|
||||||
return $this->errors;
|
return $this->errors;
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
namespace Egulias\EmailValidator\Validation;
|
namespace Egulias\EmailValidator\Validation;
|
||||||
|
|
||||||
use Egulias\EmailValidator\EmailLexer;
|
use Egulias\EmailValidator\EmailLexer;
|
||||||
use Egulias\EmailValidator\Exception\InvalidEmail;
|
|
||||||
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
|
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList;
|
||||||
|
|
||||||
class MultipleValidationWithAnd implements EmailValidation
|
class MultipleValidationWithAnd implements EmailValidation
|
||||||
@@ -49,7 +48,7 @@ class MultipleValidationWithAnd implements EmailValidation
|
|||||||
if (count($validations) == 0) {
|
if (count($validations) == 0) {
|
||||||
throw new EmptyValidationList();
|
throw new EmptyValidationList();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->validations = $validations;
|
$this->validations = $validations;
|
||||||
$this->mode = $mode;
|
$this->mode = $mode;
|
||||||
}
|
}
|
||||||
@@ -65,17 +64,29 @@ class MultipleValidationWithAnd implements EmailValidation
|
|||||||
$emailLexer->reset();
|
$emailLexer->reset();
|
||||||
$result = $result && $validation->isValid($email, $emailLexer);
|
$result = $result && $validation->isValid($email, $emailLexer);
|
||||||
$this->warnings = array_merge($this->warnings, $validation->getWarnings());
|
$this->warnings = array_merge($this->warnings, $validation->getWarnings());
|
||||||
$errors[] = $validation->getError();
|
$errors = $this->addNewError($validation->getError(), $errors);
|
||||||
|
|
||||||
if ($this->shouldStop($result)) {
|
if ($this->shouldStop($result)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->error = new MultipleErrors($errors);
|
|
||||||
|
if (!empty($errors)) {
|
||||||
|
$this->error = new MultipleErrors($errors);
|
||||||
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function addNewError($possibleError, array $errors)
|
||||||
|
{
|
||||||
|
if (null !== $possibleError) {
|
||||||
|
$errors[] = $possibleError;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $errors;
|
||||||
|
}
|
||||||
|
|
||||||
private function shouldStop($result)
|
private function shouldStop($result)
|
||||||
{
|
{
|
||||||
return !$result && $this->mode === self::STOP_ON_ERROR;
|
return !$result && $this->mode === self::STOP_ON_ERROR;
|
||||||
|
|||||||
@@ -3,11 +3,39 @@
|
|||||||
namespace Egulias\EmailValidator\Validation;
|
namespace Egulias\EmailValidator\Validation;
|
||||||
|
|
||||||
use Egulias\EmailValidator\EmailLexer;
|
use Egulias\EmailValidator\EmailLexer;
|
||||||
|
use Egulias\EmailValidator\Exception\InvalidEmail;
|
||||||
|
use Egulias\EmailValidator\Validation\Error\RFCWarnings;
|
||||||
|
|
||||||
class NoRFCWarningsValidation extends RFCValidation
|
class NoRFCWarningsValidation extends RFCValidation
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var InvalidEmail
|
||||||
|
*/
|
||||||
|
private $error;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function isValid($email, EmailLexer $emailLexer)
|
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
|
* @var InvalidEmail
|
||||||
*/
|
*/
|
||||||
private $error;
|
private $error;
|
||||||
|
|
||||||
public function isValid($email, EmailLexer $emailLexer)
|
public function isValid($email, EmailLexer $emailLexer)
|
||||||
{
|
{
|
||||||
$this->parser = new EmailParser($emailLexer);
|
$this->parser = new EmailParser($emailLexer);
|
||||||
@@ -32,7 +32,7 @@ class RFCValidation implements EmailValidation
|
|||||||
$this->error = $invalid;
|
$this->error = $invalid;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->warnings = $this->parser->getWarnings();
|
$this->warnings = $this->parser->getWarnings();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,11 +18,11 @@ class SpoofCheckValidation implements EmailValidation
|
|||||||
{
|
{
|
||||||
$checker = new Spoofchecker();
|
$checker = new Spoofchecker();
|
||||||
$checker->setChecks(Spoofchecker::SINGLE_SCRIPT);
|
$checker->setChecks(Spoofchecker::SINGLE_SCRIPT);
|
||||||
|
|
||||||
if ($checker->isSuspicious($email)) {
|
if ($checker->isSuspicious($email)) {
|
||||||
$this->error = new SpoofEmail();
|
$this->error = new SpoofEmail();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->error === null;
|
return $this->error === null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ abstract class Warning
|
|||||||
{
|
{
|
||||||
return $this->rfcNumber;
|
return $this->rfcNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString()
|
public function __toString()
|
||||||
{
|
{
|
||||||
return $this->message() . " rfc: " . $this->rfcNumber . "interal code: " . static::CODE;
|
return $this->message() . " rfc: " . $this->rfcNumber . "interal code: " . static::CODE;
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
#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)
|
[](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/)
|
||||||
|
|
||||||
|
##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](http://php.net/manual/en/book.intl.php) (also known as PHP Intl)
|
||||||
|
|
||||||

|
|
||||||
##Installation##
|
##Installation##
|
||||||
|
|
||||||
Run the command below to install via Composer
|
Run the command below to install via Composer
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
composer require egulias/email-validator "~2.0"
|
composer require egulias/email-validator "~2.1"
|
||||||
```
|
```
|
||||||
|
|
||||||
##Getting Started##
|
##Getting Started##
|
||||||
@@ -20,16 +24,17 @@ A basic example with the RFC validation
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Egulias\EmailValidator\EmailValidator;
|
use Egulias\EmailValidator\EmailValidator;
|
||||||
|
use Egulias\EmailValidator\Validation\RFCValidation;
|
||||||
|
|
||||||
$validator = new EmailValidator();
|
$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)
|
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)
|
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)
|
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)
|
5. [MultipleValidationWithAnd](https://github.com/egulias/EmailValidator/blob/master/EmailValidator/Validation/MultipleValidationWithAnd.php)
|
||||||
@@ -51,8 +56,8 @@ $validator = new EmailValidator();
|
|||||||
$multipleValidations = new MultipleValidationWithAnd([
|
$multipleValidations = new MultipleValidationWithAnd([
|
||||||
new RFCValidation(),
|
new RFCValidation(),
|
||||||
new DNSCheckValidation()
|
new DNSCheckValidation()
|
||||||
])
|
]);
|
||||||
$validator->isValid("example@example.com", $multipleValidations) //true
|
$validator->isValid("example@example.com", $multipleValidations); //true
|
||||||
```
|
```
|
||||||
|
|
||||||
###How to extend###
|
###How to extend###
|
||||||
|
|||||||
@@ -10,18 +10,18 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testValidationIsUsed()
|
public function testValidationIsUsed()
|
||||||
{
|
{
|
||||||
$validator = new EmailValidator();
|
$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("isValid")->willReturn(true);
|
||||||
$validation->expects($this->once())->method("getWarnings")->willReturn([]);
|
$validation->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||||
$validation->expects($this->once())->method("getError")->willReturn(null);
|
$validation->expects($this->once())->method("getError")->willReturn(null);
|
||||||
|
|
||||||
$this->assertTrue($validator->isValid("example@example.com", $validation));
|
$this->assertTrue($validator->isValid("example@example.com", $validation));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMultipleValidation()
|
public function testMultipleValidation()
|
||||||
{
|
{
|
||||||
$validator = new EmailValidator();
|
$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("isValid")->willReturn(true);
|
||||||
$validation->expects($this->once())->method("getWarnings")->willReturn([]);
|
$validation->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||||
$validation->expects($this->once())->method("getError")->willReturn(null);
|
$validation->expects($this->once())->method("getError")->willReturn(null);
|
||||||
|
|||||||
@@ -34,13 +34,13 @@ class DNSCheckValidationTest extends \PHPUnit_Framework_TestCase
|
|||||||
$validation = new DNSCheckValidation();
|
$validation = new DNSCheckValidation();
|
||||||
$this->assertTrue($validation->isValid($validEmail, new EmailLexer()));
|
$this->assertTrue($validation->isValid($validEmail, new EmailLexer()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInvalidDNS()
|
public function testInvalidDNS()
|
||||||
{
|
{
|
||||||
$validation = new DNSCheckValidation();
|
$validation = new DNSCheckValidation();
|
||||||
$this->assertFalse($validation->isValid("example@invalid.example.com", new EmailLexer()));
|
$this->assertFalse($validation->isValid("example@invalid.example.com", new EmailLexer()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDNSWarnings()
|
public function testDNSWarnings()
|
||||||
{
|
{
|
||||||
$validation = new DNSCheckValidation();
|
$validation = new DNSCheckValidation();
|
||||||
@@ -48,7 +48,7 @@ class DNSCheckValidationTest extends \PHPUnit_Framework_TestCase
|
|||||||
$validation->isValid("example@invalid.example.com", new EmailLexer());
|
$validation->isValid("example@invalid.example.com", new EmailLexer());
|
||||||
$this->assertEquals($expectedWarnings, $validation->getWarnings());
|
$this->assertEquals($expectedWarnings, $validation->getWarnings());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNoDNSError()
|
public function testNoDNSError()
|
||||||
{
|
{
|
||||||
$validation = new DNSCheckValidation();
|
$validation = new DNSCheckValidation();
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ class IsEmailFunctionTests extends \PHPUnit_Framework_TestCase
|
|||||||
public function isEmailTestSuite()
|
public function isEmailTestSuite()
|
||||||
{
|
{
|
||||||
$testSuite = dirname(__FILE__) . '/../../resources/is_email_tests.xml';
|
$testSuite = dirname(__FILE__) . '/../../resources/is_email_tests.xml';
|
||||||
$document = new \DOMDocument();
|
$document = new \DOMDocument();
|
||||||
$document->load($testSuite);
|
$document->load($testSuite);
|
||||||
$elements = $document->getElementsByTagName('test');
|
$elements = $document->getElementsByTagName('test');
|
||||||
$tests = [];
|
$tests = [];
|
||||||
|
|
||||||
@@ -40,5 +40,4 @@ class IsEmailFunctionTests extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
return $tests;
|
return $tests;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
public function testUsesAndLogicalOperation()
|
public function testUsesAndLogicalOperation()
|
||||||
{
|
{
|
||||||
$lexer = $this->getMock("Egulias\\EmailValidator\\EmailLexer");
|
$lexer = $this->getMockBuilder("Egulias\\EmailValidator\\EmailLexer")->getMock();
|
||||||
$validationTrue = $this->getMock("Egulias\\EmailValidator\\Validation\\EmailValidation");
|
$validationTrue = $this->getMockBuilder("Egulias\\EmailValidator\\Validation\\EmailValidation")->getMock();
|
||||||
$validationTrue->expects($this->any())->method("isValid")->willReturn(true);
|
$validationTrue->expects($this->any())->method("isValid")->willReturn(true);
|
||||||
$validationTrue->expects($this->any())->method("getWarnings")->willReturn([]);
|
$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("isValid")->willReturn(false);
|
||||||
$validationFalse->expects($this->any())->method("getWarnings")->willReturn([]);
|
$validationFalse->expects($this->any())->method("getWarnings")->willReturn([]);
|
||||||
$multipleValidation = new MultipleValidationWithAnd([$validationTrue, $validationFalse]);
|
$multipleValidation = new MultipleValidationWithAnd([$validationTrue, $validationFalse]);
|
||||||
@@ -32,6 +32,19 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
|||||||
new MultipleValidationWithAnd([]);
|
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()
|
public function testAccumulatesWarnings()
|
||||||
{
|
{
|
||||||
$warnings1 = [
|
$warnings1 = [
|
||||||
@@ -41,16 +54,17 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
|||||||
DomainLiteral::CODE => new DomainLiteral()
|
DomainLiteral::CODE => new DomainLiteral()
|
||||||
];
|
];
|
||||||
$expectedResult = array_merge($warnings1, $warnings2);
|
$expectedResult = array_merge($warnings1, $warnings2);
|
||||||
|
|
||||||
$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(true);
|
$validation1->expects($this->any())->method("isValid")->willReturn(true);
|
||||||
$validation1->expects($this->once())->method("getWarnings")->willReturn($warnings1);
|
$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->any())->method("isValid")->willReturn(false);
|
||||||
$validation2->expects($this->once())->method("getWarnings")->willReturn($warnings2);
|
$validation2->expects($this->once())->method("getWarnings")->willReturn($warnings2);
|
||||||
|
|
||||||
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]);
|
$multipleValidation = new MultipleValidationWithAnd([$validation1, $validation2]);
|
||||||
$multipleValidation->isValid("example@example.com", $lexer);
|
$multipleValidation->isValid("example@example.com", $lexer);
|
||||||
$this->assertEquals($expectedResult, $multipleValidation->getWarnings());
|
$this->assertEquals($expectedResult, $multipleValidation->getWarnings());
|
||||||
@@ -60,17 +74,17 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$error1 = new CommaInDomain();
|
$error1 = new CommaInDomain();
|
||||||
$error2 = new NoDomainPart();
|
$error2 = new NoDomainPart();
|
||||||
|
|
||||||
$expectedResult = new MultipleErrors([$error1, $error2]);
|
$expectedResult = new MultipleErrors([$error1, $error2]);
|
||||||
|
|
||||||
$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(true);
|
$validation1->expects($this->any())->method("isValid")->willReturn(true);
|
||||||
$validation1->expects($this->once())->method("getWarnings")->willReturn([]);
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||||
$validation1->expects($this->once())->method("getError")->willReturn($error1);
|
$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->any())->method("isValid")->willReturn(false);
|
||||||
$validation2->expects($this->once())->method("getWarnings")->willReturn([]);
|
$validation2->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||||
$validation2->expects($this->once())->method("getError")->willReturn($error2);
|
$validation2->expects($this->once())->method("getError")->willReturn($error2);
|
||||||
@@ -86,14 +100,14 @@ class MultipleValidationWitAndTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$expectedResult = new MultipleErrors([$error]);
|
$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->any())->method("isValid")->willReturn(false);
|
||||||
$validation1->expects($this->once())->method("getWarnings")->willReturn([]);
|
$validation1->expects($this->once())->method("getWarnings")->willReturn([]);
|
||||||
$validation1->expects($this->once())->method("getError")->willReturn($error);
|
$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("isValid");
|
||||||
$validation2->expects($this->never())->method("getWarnings");
|
$validation2->expects($this->never())->method("getWarnings");
|
||||||
$validation2->expects($this->never())->method("getError");
|
$validation2->expects($this->never())->method("getError");
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
class NoRFCWarningsValidationTest extends \PHPUnit_Framework_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()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -47,7 +47,7 @@ class RFCValidationTest extends \PHPUnit_Framework_TestCase
|
|||||||
* @var EmailLexer
|
* @var EmailLexer
|
||||||
*/
|
*/
|
||||||
protected $lexer;
|
protected $lexer;
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
$this->validator = new RFCValidation();
|
$this->validator = new RFCValidation();
|
||||||
@@ -106,7 +106,7 @@ class RFCValidationTest extends \PHPUnit_Framework_TestCase
|
|||||||
$email = "\x80\x81\x82@\x83\x84\x85.\x86\x87\x88";
|
$email = "\x80\x81\x82@\x83\x84\x85.\x86\x87\x88";
|
||||||
$this->assertFalse($this->validator->isValid($email, $this->lexer));
|
$this->assertFalse($this->validator->isValid($email, $this->lexer));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getInvalidEmails
|
* @dataProvider getInvalidEmails
|
||||||
*/
|
*/
|
||||||
@@ -228,7 +228,7 @@ class RFCValidationTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertTrue(isset($expectedWarnings[$warning->code()]));
|
$this->assertTrue(isset($expectedWarnings[$warning->code()]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInvalidEmailsWithWarnings()
|
public function getInvalidEmailsWithWarnings()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -14,17 +14,17 @@ class SpoofCheckValidationTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$this->markTestSkipped("Skipped for Travis CI since it is failing on this test for unknown reasons.");
|
$this->markTestSkipped("Skipped for Travis CI since it is failing on this test for unknown reasons.");
|
||||||
$validation = new SpoofCheckValidation();
|
$validation = new SpoofCheckValidation();
|
||||||
|
|
||||||
$this->assertTrue($validation->isValid($email, new EmailLexer()));
|
$this->assertTrue($validation->isValid($email, new EmailLexer()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEmailWithSpoofsIsInvalid()
|
public function testEmailWithSpoofsIsInvalid()
|
||||||
{
|
{
|
||||||
$validation = new SpoofCheckValidation();
|
$validation = new SpoofCheckValidation();
|
||||||
|
|
||||||
$this->assertFalse($validation->isValid("Кириллица"."latin漢字"."ひらがな"."カタカナ", new EmailLexer()));
|
$this->assertFalse($validation->isValid("Кириллица"."latin漢字"."ひらがな"."カタカナ", new EmailLexer()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function validUTF8EmailsProvider()
|
public function validUTF8EmailsProvider()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -28,6 +28,9 @@
|
|||||||
"phpunit/phpunit": "^4.8.0",
|
"phpunit/phpunit": "^4.8.0",
|
||||||
"dominicsayers/isemail": "dev-master"
|
"dominicsayers/isemail": "dev-master"
|
||||||
},
|
},
|
||||||
|
"suggest": {
|
||||||
|
"ext-intl": "PHP Internationalization Libraries are required to use the SpoofChecking validation"
|
||||||
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Egulias\\EmailValidator\\": "EmailValidator"
|
"Egulias\\EmailValidator\\": "EmailValidator"
|
||||||
|
|||||||
Reference in New Issue
Block a user