diff --git a/.gitignore b/.gitignore index 5eba32a..b24e2c7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ report/ vendor/ .idea +.local.php.vimrc diff --git a/composer.json b/composer.json index 62461c0..c6dfc05 100644 --- a/composer.json +++ b/composer.json @@ -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/" } } } diff --git a/composer.lock b/composer.lock index 15eca77..f1ca564 100644 --- a/composer.lock +++ b/composer.lock @@ -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" }, diff --git a/src/Egulias/Constraints/EmailExtra.php b/src/Egulias/Constraints/EmailExtra.php new file mode 100644 index 0000000..7de110d --- /dev/null +++ b/src/Egulias/Constraints/EmailExtra.php @@ -0,0 +1,11 @@ + + */ +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)); + } + + + } +} diff --git a/src/egulias/EmailValidator/EmailLexer.php b/src/Egulias/EmailValidator/EmailLexer.php similarity index 99% rename from src/egulias/EmailValidator/EmailLexer.php rename to src/Egulias/EmailValidator/EmailLexer.php index e0cce07..f5b9696 100644 --- a/src/egulias/EmailValidator/EmailLexer.php +++ b/src/Egulias/EmailValidator/EmailLexer.php @@ -1,6 +1,6 @@ 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'), + ); + } +} diff --git a/tests/egulias/Tests/EmailValidator/EmailLexerTest.php b/tests/egulias/Tests/EmailValidator/EmailLexerTest.php index 2cf1674..311614f 100644 --- a/tests/egulias/Tests/EmailValidator/EmailLexerTest.php +++ b/tests/egulias/Tests/EmailValidator/EmailLexerTest.php @@ -1,8 +1,8 @@