Skip to content

Commit

Permalink
Improve exception message for Email::_validateEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hoffmann committed Jul 4, 2017
1 parent 1362f78 commit ecdc092
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/Mailer/Email.php
Expand Up @@ -882,7 +882,7 @@ public function emailPattern($regex = false)
protected function _setEmail($varName, $email, $name) protected function _setEmail($varName, $email, $name)
{ {
if (!is_array($email)) { if (!is_array($email)) {
$this->_validateEmail($email); $this->_validateEmail($email, $varName);
if ($name === null) { if ($name === null) {
$name = $email; $name = $email;
} }
Expand All @@ -895,7 +895,7 @@ protected function _setEmail($varName, $email, $name)
if (is_int($key)) { if (is_int($key)) {
$key = $value; $key = $value;
} }
$this->_validateEmail($key); $this->_validateEmail($key, $varName);
$list[$key] = $value; $list[$key] = $value;
} }
$this->{$varName} = $list; $this->{$varName} = $list;
Expand All @@ -907,10 +907,11 @@ protected function _setEmail($varName, $email, $name)
* Validate email address * Validate email address
* *
* @param string $email Email address to validate * @param string $email Email address to validate
* @param string $context Which property was set
* @return void * @return void
* @throws \InvalidArgumentException If email address does not validate * @throws \InvalidArgumentException If email address does not validate
*/ */
protected function _validateEmail($email) protected function _validateEmail($email, $context)
{ {
if ($this->_emailPattern === null) { if ($this->_emailPattern === null) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
Expand All @@ -919,7 +920,12 @@ protected function _validateEmail($email)
} elseif (preg_match($this->_emailPattern, $email)) { } elseif (preg_match($this->_emailPattern, $email)) {
return; 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));
} }


/** /**
Expand Down Expand Up @@ -958,7 +964,7 @@ protected function _setEmailSingle($varName, $email, $name, $throwMessage)
protected function _addEmail($varName, $email, $name) protected function _addEmail($varName, $email, $name)
{ {
if (!is_array($email)) { if (!is_array($email)) {
$this->_validateEmail($email); $this->_validateEmail($email, $varName);
if ($name === null) { if ($name === null) {
$name = $email; $name = $email;
} }
Expand All @@ -971,7 +977,7 @@ protected function _addEmail($varName, $email, $name)
if (is_int($key)) { if (is_int($key)) {
$key = $value; $key = $value;
} }
$this->_validateEmail($key); $this->_validateEmail($key, $varName);
$list[$key] = $value; $list[$key] = $value;
} }
$this->{$varName} = array_merge($this->{$varName}, $list); $this->{$varName} = array_merge($this->{$varName}, $list);
Expand Down
16 changes: 15 additions & 1 deletion tests/TestCase/Mailer/EmailTest.php
Expand Up @@ -400,7 +400,7 @@ public function testClassNameException()
* @return void * @return void
* *
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid email: "fail.@example.com" * @expectedExceptionMessage Invalid email set for "to". You passed "fail.@example.com".
*/ */
public function testUnsetEmailPattern() public function testUnsetEmailPattern()
{ {
Expand All @@ -414,6 +414,20 @@ public function testUnsetEmailPattern()
$email->to('fail.@example.com'); $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 * testFormatAddress method
* *
Expand Down

0 comments on commit ecdc092

Please sign in to comment.