Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge 97a406c into cf921ee
Browse files Browse the repository at this point in the history
  • Loading branch information
Tauop committed Jan 31, 2015
2 parents cf921ee + 97a406c commit ebad6b9
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions library/Zend/Log/Writer/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public function __construct($mail, Transport\TransportInterface $transport = nul
}
$transport = isset($mail['transport']) ? $mail['transport'] : null;
$mail = isset($mail['mail']) ? $mail['mail'] : null;
if (is_array($mail)) {
$mail = new MailMessage($mail);
}
}

// Ensure we have a valid mail message
Expand Down
38 changes: 38 additions & 0 deletions library/Zend/Mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,44 @@ class Message
*/
protected $encoding = 'ASCII';

/**
* Constructor.
*
* @param null|array|Traversable $options (Optional) Options to set
* @param array|Traversable $options
*/
public function __construct($options = null)
{
if (null !== $options) {
$this->setOptions($options);
}
}

/**
* @param array|Traversable $options Options to set
* @return self
* @throws Exception\InvalidArgumentException
*/
private function setOptions($options)
{
if (!is_array($options) && !$options instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf(
'"%s" expects an array or Traversable; received "%s"',
__METHOD__,
(is_object($options) ? get_class($options) : gettype($options))
));
}

foreach ($options as $key => $value) {
$setter = 'set' . str_replace(' ', '', ucwords(str_replace('-', ' ', str_replace('_', ' ', $key))));
if (method_exists($this, $setter)) {
$this->{$setter}($value);
}
}

return $this;
}

/**
* Is the message valid?
*
Expand Down
17 changes: 17 additions & 0 deletions tests/ZendTest/Log/Writer/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,21 @@ public function testConstructWithOptions()
$this->assertCount(1, $filters);
$this->assertEquals($filter, $filters[0]);
}

public function testConstructWithMailOptions()
{
$message = array(
'from' => 'matthew@example.com',
'to' => 'zf-devteam@example.com',
'cc' => 'zf-contributors@example.com',
'bcc' => 'zf-devteam@example.com',
'subject' => 'subject',
);

$writer = new MailWriter(array(
'mail' => $message,
));

$this->assertAttributeInstanceOf('Zend\Mail\Message', 'mail', $writer);
}
}
39 changes: 39 additions & 0 deletions tests/ZendTest/Mail/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,43 @@ public function testPassEmptyArrayIntoSetPartsOfMimeMessageShouldReturnEmptyBody
$this->message->setBody($mimeMessage);
$this->assertEquals('', $this->message->getBodyText());
}

public function testConstructorWithOptions()
{
$options = array(
'encoding' => 'UTF-8',
'from' => 'matthew@example.com',
'to' => 'zf-devteam@example.com',
'cc' => 'zf-contributors@example.com',
'bcc' => 'zf-devteam@example.com',
'reply-to' => 'matthew@example.com',
'sender' => 'matthew@example.com',
'subject' => 'subject',
'body' => 'body',
'ignore' => 'ignore options',
);

$message = new Message(new \ArrayObject($options));

$this->assertEquals('UTF-8', $message->getEncoding());
$this->assertEquals('subject', $message->getSubject());
$this->assertEquals('body', $message->getBody());
$this->assertInstanceOf('Zend\Mail\Address', $message->getSender());
$this->assertEquals($options['sender'], $message->getSender()->getEmail());

$getMethods = array(
'from' => 'getFrom',
'to' => 'getTo',
'cc' => 'getCc',
'bcc' => 'getBcc',
'reply-to' => 'getReplyTo',
);

foreach ($getMethods as $key => $method) {
$value = $message->{$method}();
$this->assertInstanceOf('Zend\Mail\AddressList', $value);
$this->assertEquals(1, count($value));
$this->assertTrue($value->has($options[$key]));
}
}
}

0 comments on commit ebad6b9

Please sign in to comment.