Skip to content
Permalink
Browse files

Improve exception message for Email::_validateEmail

  • Loading branch information...
cleptric committed Jul 4, 2017
1 parent 1362f78 commit ecdc092e9ba427f7cce060a263a9fdfe9868b2b5
Showing with 27 additions and 7 deletions.
  1. +12 −6 src/Mailer/Email.php
  2. +15 −1 tests/TestCase/Mailer/EmailTest.php
@@ -882,7 +882,7 @@ public function emailPattern($regex = false)
protected function _setEmail($varName, $email, $name)
{
if (!is_array($email)) {
$this->_validateEmail($email);
$this->_validateEmail($email, $varName);
if ($name === null) {
$name = $email;
}
@@ -895,7 +895,7 @@ protected function _setEmail($varName, $email, $name)
if (is_int($key)) {
$key = $value;
}
$this->_validateEmail($key);
$this->_validateEmail($key, $varName);
$list[$key] = $value;
}
$this->{$varName} = $list;
@@ -907,10 +907,11 @@ protected function _setEmail($varName, $email, $name)
* Validate email address
*
* @param string $email Email address to validate
* @param string $context Which property was set
* @return void
* @throws \InvalidArgumentException If email address does not validate
*/
protected function _validateEmail($email)
protected function _validateEmail($email, $context)
{
if ($this->_emailPattern === null) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
@@ -919,7 +920,12 @@ protected function _validateEmail($email)
} elseif (preg_match($this->_emailPattern, $email)) {
return;
}
throw new InvalidArgumentException(sprintf('Invalid email: "%s"', $email));
$context = ltrim($context, '_');
if ($email == '') {
throw new InvalidArgumentException(sprintf('The email set for "%s" is empty.', $context));
}
throw new InvalidArgumentException(sprintf('Invalid email set for "%s". You passed "%s".', $context, $email));
}
/**
@@ -958,7 +964,7 @@ protected function _setEmailSingle($varName, $email, $name, $throwMessage)
protected function _addEmail($varName, $email, $name)
{
if (!is_array($email)) {
$this->_validateEmail($email);
$this->_validateEmail($email, $varName);
if ($name === null) {
$name = $email;
}
@@ -971,7 +977,7 @@ protected function _addEmail($varName, $email, $name)
if (is_int($key)) {
$key = $value;
}
$this->_validateEmail($key);
$this->_validateEmail($key, $varName);
$list[$key] = $value;
}
$this->{$varName} = array_merge($this->{$varName}, $list);
@@ -400,7 +400,7 @@ public function testClassNameException()
* @return void
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid email: "fail.@example.com"
* @expectedExceptionMessage Invalid email set for "to". You passed "fail.@example.com".
*/
public function testUnsetEmailPattern()
{
@@ -414,6 +414,20 @@ public function testUnsetEmailPattern()
$email->to('fail.@example.com');
}
/**
* Tests that passing an empty string throws an InvalidArgumentException.
*
* @return void
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The email set for "to" is empty.
*/
public function testEmptyTo()
{
$email = new Email();
$email->setTo('');
}
/**
* testFormatAddress method
*

0 comments on commit ecdc092

Please sign in to comment.
You can’t perform that action at this time.