mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-08-28 02:58:29 +00:00
#4 - Implementing Symfony Validator Constraints
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
report/
|
||||
vendor/
|
||||
.idea
|
||||
.local.php.vimrc
|
||||
|
||||
+3
-2
@@ -10,11 +10,12 @@
|
||||
],
|
||||
"require": {
|
||||
"php": ">= 5.3.3",
|
||||
"doctrine/common": "~2.2"
|
||||
"doctrine/common": "~2.2",
|
||||
"symfony/validator": "dev-master"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"egulias\\": "src/"
|
||||
"Egulias\\": "src/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+120
-4
@@ -3,7 +3,7 @@
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
|
||||
],
|
||||
"hash": "b58e1c8afd1dc9b8c11e4be257e18967",
|
||||
"hash": "f22a825f48c4dcd5a4aef5b8bdbc0bcf",
|
||||
"packages": [
|
||||
{
|
||||
"name": "doctrine/common",
|
||||
@@ -73,6 +73,122 @@
|
||||
"spl"
|
||||
],
|
||||
"time": "2012-09-19 22:55:18"
|
||||
},
|
||||
{
|
||||
"name": "symfony/translation",
|
||||
"version": "v2.3.1",
|
||||
"target-dir": "Symfony/Component/Translation",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/Translation.git",
|
||||
"reference": "v2.3.1"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/Translation/zipball/v2.3.1",
|
||||
"reference": "v2.3.1",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/config": ">=2.0,<3.0",
|
||||
"symfony/yaml": ">=2.2,<3.0"
|
||||
},
|
||||
"suggest": {
|
||||
"symfony/config": "",
|
||||
"symfony/yaml": ""
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.3-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"Symfony\\Component\\Translation\\": ""
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Fabien Potencier",
|
||||
"email": "fabien@symfony.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "http://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "Symfony Translation Component",
|
||||
"homepage": "http://symfony.com",
|
||||
"time": "2013-05-13 14:36:40"
|
||||
},
|
||||
{
|
||||
"name": "symfony/validator",
|
||||
"version": "dev-master",
|
||||
"target-dir": "Symfony/Component/Validator",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/Validator.git",
|
||||
"reference": "7ebb05d2f47609e44793f6a0a541fbc2f6d61422"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/Validator/zipball/7ebb05d2f47609e44793f6a0a541fbc2f6d61422",
|
||||
"reference": "7ebb05d2f47609e44793f6a0a541fbc2f6d61422",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.3",
|
||||
"symfony/translation": ">=2.0,<3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/config": ">=2.2,<3.0",
|
||||
"symfony/http-foundation": ">=2.1,<3.0",
|
||||
"symfony/intl": ">=2.3,<3.0",
|
||||
"symfony/yaml": ">=2.0,<3.0"
|
||||
},
|
||||
"suggest": {
|
||||
"doctrine/common": "",
|
||||
"symfony/config": "",
|
||||
"symfony/http-foundation": "",
|
||||
"symfony/intl": "",
|
||||
"symfony/yaml": ""
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.4-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"Symfony\\Component\\Validator\\": ""
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Fabien Potencier",
|
||||
"email": "fabien@symfony.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "http://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "Symfony Validator Component",
|
||||
"homepage": "http://symfony.com",
|
||||
"time": "2013-06-11 07:15:38"
|
||||
}
|
||||
],
|
||||
"packages-dev": [
|
||||
@@ -82,9 +198,9 @@
|
||||
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [
|
||||
|
||||
],
|
||||
"stability-flags": {
|
||||
"symfony/validator": 20
|
||||
},
|
||||
"platform": {
|
||||
"php": ">= 5.3.3"
|
||||
},
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\Email;
|
||||
|
||||
class EmailExtra extends Email
|
||||
{
|
||||
public $strict = false;
|
||||
public $verbose = false;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
use Egulias\EmailValidator\EmailValidator;
|
||||
|
||||
/**
|
||||
* EmailExtraValidator
|
||||
*
|
||||
* @author Eduardo Gulias Davis <me@egulias.com>
|
||||
*/
|
||||
class EmailExtraValidator extends ConstraintValidator
|
||||
{
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (null === $value || '' === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
|
||||
throw new UnexpectedTypeException($value, 'string');
|
||||
}
|
||||
|
||||
$validator = new EmailValidator();
|
||||
$valid = $validator->isValid($value, $constraint->checkMX, $constraint->strict);
|
||||
|
||||
if ($constraint->verbose) {
|
||||
$constraint->message .= implode(',', $this->getWarnings());
|
||||
}
|
||||
|
||||
if (!$valid) {
|
||||
if ($constraint->verbose) {
|
||||
$constraint->message .= "Error code: {$validator->getError()}";
|
||||
}
|
||||
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace egulias\EmailValidator;
|
||||
namespace Egulias\EmailValidator;
|
||||
|
||||
use Doctrine\Common\Lexer;
|
||||
|
||||
+1
-3
@@ -1,8 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace egulias\EmailValidator;
|
||||
|
||||
use JMS\Parser\AbstractParser;
|
||||
namespace Egulias\EmailValidator;
|
||||
|
||||
/**
|
||||
* EmailParser
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace egulias\EmailValidator;
|
||||
namespace Egulias\EmailValidator;
|
||||
|
||||
/**
|
||||
* EmailValidator
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Egulias\Tests\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Validation;
|
||||
use Egulias\Constraints\EmailExtra;
|
||||
use Egulias\Constraints\EmailExtraValidator;
|
||||
|
||||
class EmailExtraValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $context;
|
||||
protected $validator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
||||
$this->validator = new EmailExtraValidator();
|
||||
$this->validator->initialize($this->context);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->context = null;
|
||||
$this->validator = null;
|
||||
}
|
||||
|
||||
public function testNullIsValid()
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validator->validate(null, new EmailExtra());
|
||||
}
|
||||
|
||||
public function testEmptyStringIsValid()
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validator->validate('', new EmailExtra());
|
||||
}
|
||||
|
||||
/**
|
||||
* * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
|
||||
* */
|
||||
public function testExpectsStringCompatibleType()
|
||||
{
|
||||
$this->validator->validate(new \stdClass(), new EmailExtra());
|
||||
}
|
||||
|
||||
/**
|
||||
* * @dataProvider getValidEmails
|
||||
* */
|
||||
public function testValidEmails($email)
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validator->validate($email, new EmailExtra());
|
||||
}
|
||||
|
||||
public function getValidEmails()
|
||||
{
|
||||
return array(
|
||||
array('fabien@symfony.com'),
|
||||
array('example@example.co.uk'),
|
||||
array('fabien_potencier@example.fr'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* * @dataProvider getInvalidEmails
|
||||
* */
|
||||
public function testInvalidEmails($email)
|
||||
{
|
||||
$constraint = new EmailExtra(
|
||||
array(
|
||||
'message' => 'myMessage'
|
||||
)
|
||||
);
|
||||
|
||||
$this->context->expects($this->once())->method('addViolation')
|
||||
->with(
|
||||
'myMessage',
|
||||
array('{{ value }}' => $email,)
|
||||
);
|
||||
|
||||
$this->validator->validate($email, $constraint);
|
||||
}
|
||||
|
||||
public function getInvalidEmails()
|
||||
{
|
||||
return array(
|
||||
//array('example'),
|
||||
array('example@'),
|
||||
//array('example@localhost'),
|
||||
array('example@example.com@example.com'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace egulias\EmailValidator\Tests;
|
||||
namespace Egulias\EmailValidator\Tests;
|
||||
|
||||
use egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\EmailLexer;
|
||||
|
||||
class EmailLexerTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace egulias\Tests\EmailValidator;
|
||||
namespace Egulias\Tests\EmailValidator;
|
||||
|
||||
use egulias\EmailValidator\EmailParser;
|
||||
use Egulias\EmailValidator\EmailParser;
|
||||
|
||||
class EmailParserTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace egulias\Tests\EmailValidator;
|
||||
namespace Egulias\Tests\EmailValidator;
|
||||
|
||||
use egulias\EmailValidator\EmailValidator;
|
||||
use Egulias\EmailValidator\EmailValidator;
|
||||
|
||||
class EmailValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user