Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
"require": {
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "~4"
},
"suggest": {
"ext-intl": "*",
"true/punycode": "to handle UTF-8 emails, if you don't have ext-intl"
},
"autoload": {
"psr-0": {
"Assert": "lib/"
Expand Down
25 changes: 24 additions & 1 deletion lib/Assert/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ public static function email($value, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);

if ( ! filter_var($value, FILTER_VALIDATE_EMAIL)) {
if ( ! filter_var(self::convertEmailToIdna($value), FILTER_VALIDATE_EMAIL)) {
$message = sprintf(
$message ?: 'Value "%s" was expected to be a valid e-mail address.',
self::stringify($value)
Expand All @@ -1139,6 +1139,29 @@ public static function email($value, $message = null, $propertyPath = null)
}
}

private static function convertEmailToIdna($email)
{
$convertIdna = function ($value) {
if (function_exists('idn_to_ascii')) {
return idn_to_ascii($value);
} elseif (class_exists('\True\Punycode')) {
$prevEncoding = mb_internal_encoding();
mb_internal_encoding('utf-8');
$punycode = new \True\Punycode();
$result = $punycode->encode($value);
mb_internal_encoding($prevEncoding);

return $result;
}

return $value;
};

$result = implode('@', array_map($convertIdna, explode('@', $email)));

return $result;
}

/**
* Assert that value is an URL.
*
Expand Down
39 changes: 31 additions & 8 deletions tests/Assert/Tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,25 +457,48 @@ public function testValidRange()
Assertion::range(2.5, 2.25, 2.75);
}

public function testInvalidEmail()
/**
* @param string $invalidEmail
* @dataProvider dataInvalidEmail
*/
public function testInvalidEmail($invalidEmail)
{
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_EMAIL);
Assertion::email("foo");
Assertion::email($invalidEmail);
}

public function testValidEmail()
public static function dataInvalidEmail()
{
Assertion::email("[email protected]");
return array(
'no @' => array("foo"),
'no domain' => array("foo@"),
'two ats' => array("foo@[email protected]"),
'double quot' => array('foo"[email protected]'),
'double quot in domain' => array('fooa@exam"ple.com'),
"\xb2 symbol" => array("foo\[email protected]"),
'contains space' => array("fo [email protected]"),
'leading space' => array(" [email protected]"),
'trailing space' => array("[email protected] "),
);
}

/**
* @dataProvider dataInvalidUrl
* @param string $validEmail
* @dataProvider dataValidEmail
*/
public function testInvalidUrl($url)
public function testValidEmail($validEmail)
{
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_URL);
Assertion::email($validEmail);
}

Assertion::url($url);
public static function dataValidEmail()
{
return array(
'regular email' => array("[email protected]"),
'mixed case' => array("[email protected]"),
'valid special chars' => array("!#$%&'*+-=?^_`{|}[email protected]"),
'cyrillic characters ' => array("вася@домен.рф"),
);
}

public static function dataInvalidUrl()
Expand Down