Browse Source

'yii\mail\MessageInterface' updated:

- setter methods renamed to have pure name
- method 'createMessage' renamed to 'compose'
tags/2.0.0-beta
Klimov Paul 11 years ago
parent
commit
87af95f712
  1. 2
      extensions/swiftmailer/yii/swiftmailer/Mailer.php
  2. 54
      extensions/swiftmailer/yii/swiftmailer/Message.php
  3. 37
      framework/yii/mail/BaseMailer.php
  4. 11
      framework/yii/mail/BaseMessage.php
  5. 2
      framework/yii/mail/MailerInterface.php
  6. 22
      framework/yii/mail/MessageInterface.php
  7. 72
      tests/unit/extensions/swiftmailer/MessageTest.php
  8. 71
      tests/unit/framework/mail/BaseMailerTest.php
  9. 18
      tests/unit/framework/mail/BaseMessageTest.php

2
extensions/swiftmailer/yii/swiftmailer/Mailer.php

@ -36,7 +36,7 @@ use Yii;
* *
* @see http://swiftmailer.org * @see http://swiftmailer.org
* *
* @method Message createMessage(array $config = []) creates new message instance from given configuration. * @method Message compose(array $config = []) creates new message instance from given configuration.
* *
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0

54
extensions/swiftmailer/yii/swiftmailer/Message.php

@ -59,7 +59,7 @@ class Message extends BaseMessage
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function setFrom($from) public function from($from)
{ {
$this->getSwiftMessage()->setFrom($from); $this->getSwiftMessage()->setFrom($from);
$this->getSwiftMessage()->setReplyTo($from); $this->getSwiftMessage()->setReplyTo($from);
@ -67,85 +67,45 @@ class Message extends BaseMessage
} }
/** /**
* @return string from address of this message.
*/
public function getFrom()
{
return $this->getSwiftMessage()->getFrom();
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function setTo($to) public function to($to)
{ {
$this->getSwiftMessage()->setTo($to); $this->getSwiftMessage()->setTo($to);
return $this; return $this;
} }
/** /**
* @return array To addresses of this message.
*/
public function getTo()
{
return $this->getSwiftMessage()->getTo();
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function setCc($cc) public function cc($cc)
{ {
$this->getSwiftMessage()->setCc($cc); $this->getSwiftMessage()->setCc($cc);
return $this; return $this;
} }
/** /**
* @return array Cc address of this message.
*/
public function getCc()
{
return $this->getSwiftMessage()->getCc();
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function setBcc($bcc) public function bcc($bcc)
{ {
$this->getSwiftMessage()->setBcc($bcc); $this->getSwiftMessage()->setBcc($bcc);
return $this; return $this;
} }
/** /**
* @return array Bcc addresses of this message.
*/
public function getBcc()
{
return $this->getSwiftMessage()->getBcc();
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function setSubject($subject) public function subject($subject)
{ {
$this->getSwiftMessage()->setSubject($subject); $this->getSwiftMessage()->setSubject($subject);
return $this; return $this;
} }
/** /**
* @return string the subject of this message.
*/
public function getSubject()
{
return $this->getSwiftMessage()->getSubject();
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function setText($text) public function text($text)
{ {
$this->setBody($text, 'text/plain'); $this->setBody($text, 'text/plain');
return $this; return $this;
@ -154,7 +114,7 @@ class Message extends BaseMessage
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function setHtml($html) public function html($html)
{ {
$this->setBody($html, 'text/html'); $this->setBody($html, 'text/html');
return $this; return $this;

37
framework/yii/mail/BaseMailer.php

@ -45,10 +45,18 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
/** /**
* @var array configuration, which should be applied by default to any new created * @var array configuration, which should be applied by default to any new created
* email message instance. * email message instance.
* In addition to normal [[Yii::createObject()]] behavior extra config keys are available:
* - 'from' invokes [[MessageInterface::from()]]
* - 'to' invokes [[MessageInterface::to()]]
* - 'cc' invokes [[MessageInterface::cc()]]
* - 'bcc' invokes [[MessageInterface::bcc()]]
* - 'subject' invokes [[MessageInterface::subject()]]
* - 'text' invokes [[MessageInterface::text()]]
* - 'html' invokes [[MessageInterface::html()]]
* For example: * For example:
* ~~~ * ~~~
* array( * array(
* 'encoding' => 'UTF-8', * 'charset' => 'UTF-8',
* 'from' => 'noreply@mydomain.com', * 'from' => 'noreply@mydomain.com',
* 'bcc' => 'email.test@mydomain.com', * 'bcc' => 'email.test@mydomain.com',
* ) * )
@ -100,16 +108,37 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
* Creates new message instance from given configuration. * Creates new message instance from given configuration.
* Message configuration will be merged with [[messageConfig]]. * Message configuration will be merged with [[messageConfig]].
* If 'class' parameter is omitted [[messageClass]], will be used. * If 'class' parameter is omitted [[messageClass]], will be used.
* @param array $config message configuration. * @param array $config message configuration. See [[messageConfig]]
* for the configuration format details.
* @return MessageInterface message instance. * @return MessageInterface message instance.
*/ */
public function createMessage(array $config = []) public function compose(array $config = [])
{ {
$config = array_merge($this->messageConfig, $config); $config = array_merge($this->messageConfig, $config);
if (!array_key_exists('class', $config)) { if (!array_key_exists('class', $config)) {
$config['class'] = $this->messageClass; $config['class'] = $this->messageClass;
} }
return Yii::createObject($config); $configMethodNames = [
'from',
'to',
'cc',
'bcc',
'subject',
'text',
'html',
];
$methodBasedConfig = [];
foreach ($config as $name => $value) {
if (in_array($name, $configMethodNames, true)) {
$methodBasedConfig[$name] = $value;
unset($config[$name]);
}
}
$message = Yii::createObject($config);
foreach ($methodBasedConfig as $name => $value) {
$message->$name($value);
}
return $message;
} }
/** /**

11
framework/yii/mail/BaseMessage.php

@ -22,13 +22,6 @@ use Yii;
* *
* @property \yii\mail\BaseMailer $mailer mailer component instance. This property is read-only. * @property \yii\mail\BaseMailer $mailer mailer component instance. This property is read-only.
* @property string $charset the character set of this message. * @property string $charset the character set of this message.
* @property string|array $from sender email address.
* @property string|array $to receiver email address.
* @property string|array $cc copy receiver email address.
* @property string|array $bcc hidden copy receiver email address.
* @property string $subject message subject.
* @property string $text message plain text content.
* @property string $html message HTML content.
* *
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0
@ -56,7 +49,7 @@ abstract class BaseMessage extends Object implements MessageInterface
*/ */
public function renderHtml($view, $params = []) public function renderHtml($view, $params = [])
{ {
$this->setHtml($this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout)); $this->html($this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout));
return $this; return $this;
} }
@ -65,7 +58,7 @@ abstract class BaseMessage extends Object implements MessageInterface
*/ */
public function renderText($view, $params = []) public function renderText($view, $params = [])
{ {
$this->setText($this->getMailer()->render($view, $params, $this->getMailer()->textLayout)); $this->text($this->getMailer()->render($view, $params, $this->getMailer()->textLayout));
return $this; return $this;
} }
} }

2
framework/yii/mail/MailerInterface.php

@ -22,7 +22,7 @@ interface MailerInterface
* @param array $config message configuration. * @param array $config message configuration.
* @return MessageInterface message instance. * @return MessageInterface message instance.
*/ */
public function createMessage(array $config = []); public function compose(array $config = []);
/** /**
* Sends the given email message. * Sends the given email message.

22
framework/yii/mail/MessageInterface.php

@ -12,10 +12,10 @@ namespace yii\mail;
* Together with application component, which matches the [[MailerInterface]], * Together with application component, which matches the [[MailerInterface]],
* it introduces following mail sending syntax: * it introduces following mail sending syntax:
* ~~~php * ~~~php
* Yii::$app->mail->createMessage() * Yii::$app->mail->compose()
* ->setFrom('from@domain.com') * ->from('from@domain.com')
* ->setTo('to@domain.com') * ->to('to@domain.com')
* ->setSubject('Message Subject') * ->subject('Message Subject')
* ->renderText('text/view') * ->renderText('text/view')
* ->renderHtml('html/view') * ->renderHtml('html/view')
* ->send(); * ->send();
@ -43,7 +43,7 @@ interface MessageInterface
* [email => name]. * [email => name].
* @return static self reference. * @return static self reference.
*/ */
public function setFrom($from); public function from($from);
/** /**
* Sets message receiver. * Sets message receiver.
@ -53,7 +53,7 @@ interface MessageInterface
* [email => name]. * [email => name].
* @return static self reference. * @return static self reference.
*/ */
public function setTo($to); public function to($to);
/** /**
* Set the Cc (additional copy receiver) addresses of this message. * Set the Cc (additional copy receiver) addresses of this message.
@ -63,7 +63,7 @@ interface MessageInterface
* [email => name]. * [email => name].
* @return static self reference. * @return static self reference.
*/ */
public function setCc($cc); public function cc($cc);
/** /**
* Set the Bcc (hidden copy receiver) addresses of this message. * Set the Bcc (hidden copy receiver) addresses of this message.
@ -73,28 +73,28 @@ interface MessageInterface
* [email => name]. * [email => name].
* @return static self reference. * @return static self reference.
*/ */
public function setBcc($bcc); public function bcc($bcc);
/** /**
* Sets message subject. * Sets message subject.
* @param string $subject message subject * @param string $subject message subject
* @return static self reference. * @return static self reference.
*/ */
public function setSubject($subject); public function subject($subject);
/** /**
* Sets message plain text content. * Sets message plain text content.
* @param string $text message plain text content. * @param string $text message plain text content.
* @return static self reference. * @return static self reference.
*/ */
public function setText($text); public function text($text);
/** /**
* Sets message HTML content. * Sets message HTML content.
* @param string $html message HTML content. * @param string $html message HTML content.
* @return static self reference. * @return static self reference.
*/ */
public function setHtml($html); public function html($html);
/** /**
* Attach specified content as file for the email message. * Attach specified content as file for the email message.

72
tests/unit/extensions/swiftmailer/MessageTest.php

@ -63,7 +63,7 @@ class MessageTest extends VendorTestCase
*/ */
protected function createTestMessage() protected function createTestMessage()
{ {
return Yii::$app->getComponent('mail')->createMessage(); return Yii::$app->getComponent('mail')->compose();
} }
/** /**
@ -120,16 +120,22 @@ class MessageTest extends VendorTestCase
$charset = 'utf-16'; $charset = 'utf-16';
$subject = 'Test Subject'; $subject = 'Test Subject';
$to = 'someuser@somedomain.com'; $to = 'someuser@somedomain.com';
$cc = 'ccuser@somedomain.com';
$bcc = 'bccuser@somedomain.com';
$messageString = $this->createTestMessage() $messageString = $this->createTestMessage()
->setCharset($charset) ->setCharset($charset)
->setSubject($subject) ->subject($subject)
->setTo($to) ->to($to)
->cc($cc)
->bcc($bcc)
->__toString(); ->__toString();
$this->assertContains('charset=' . $charset, $messageString, 'Incorrect charset!'); $this->assertContains('charset=' . $charset, $messageString, 'Incorrect charset!');
$this->assertContains('Subject: ' . $subject, $messageString, 'Incorrect "Subject" header!'); $this->assertContains('Subject: ' . $subject, $messageString, 'Incorrect "Subject" header!');
$this->assertContains('To: ' . $to, $messageString, 'Incorrect "To" header!'); $this->assertContains('To: ' . $to, $messageString, 'Incorrect "To" header!');
$this->assertContains('Cc: ' . $cc, $messageString, 'Incorrect "Cc" header!');
$this->assertContains('Bcc: ' . $bcc, $messageString, 'Incorrect "Bcc" header!');
} }
/** /**
@ -139,7 +145,7 @@ class MessageTest extends VendorTestCase
{ {
$from = 'someuser@somedomain.com'; $from = 'someuser@somedomain.com';
$messageString = $this->createTestMessage() $messageString = $this->createTestMessage()
->setFrom($from) ->from($from)
->__toString(); ->__toString();
$this->assertContains('From: ' . $from, $messageString, 'Incorrect "From" header!'); $this->assertContains('From: ' . $from, $messageString, 'Incorrect "From" header!');
$this->assertContains('Reply-To: ' . $from, $messageString, 'Incorrect "Reply-To" header!'); $this->assertContains('Reply-To: ' . $from, $messageString, 'Incorrect "Reply-To" header!');
@ -151,10 +157,10 @@ class MessageTest extends VendorTestCase
public function testSend() public function testSend()
{ {
$message = $this->createTestMessage(); $message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver); $message->to($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->from('someuser@somedomain.com');
$message->setSubject('Yii Swift Test'); $message->subject('Yii Swift Test');
$message->setText('Yii Swift Test body'); $message->text('Yii Swift Test body');
$this->assertTrue($message->send()); $this->assertTrue($message->send());
} }
@ -165,10 +171,10 @@ class MessageTest extends VendorTestCase
{ {
$message = $this->createTestMessage(); $message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver); $message->to($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->from('someuser@somedomain.com');
$message->setSubject('Yii Swift Attach File Test'); $message->subject('Yii Swift Attach File Test');
$message->setText('Yii Swift Attach File Test body'); $message->text('Yii Swift Attach File Test body');
$fileName = __FILE__; $fileName = __FILE__;
$message->attachFile($fileName); $message->attachFile($fileName);
@ -186,10 +192,10 @@ class MessageTest extends VendorTestCase
{ {
$message = $this->createTestMessage(); $message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver); $message->to($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->from('someuser@somedomain.com');
$message->setSubject('Yii Swift Create Attachment Test'); $message->subject('Yii Swift Create Attachment Test');
$message->setText('Yii Swift Create Attachment Test body'); $message->text('Yii Swift Create Attachment Test body');
$fileName = 'test.txt'; $fileName = 'test.txt';
$fileContent = 'Test attachment content'; $fileContent = 'Test attachment content';
$message->attachContent($fileContent, ['fileName' => $fileName]); $message->attachContent($fileContent, ['fileName' => $fileName]);
@ -212,10 +218,10 @@ class MessageTest extends VendorTestCase
$cid = $message->embedFile($fileName); $cid = $message->embedFile($fileName);
$message->setTo($this->testEmailReceiver); $message->to($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->from('someuser@somedomain.com');
$message->setSubject('Yii Swift Embed File Test'); $message->subject('Yii Swift Embed File Test');
$message->setHtml('Embed image: <img src="' . $cid. '" alt="pic">'); $message->html('Embed image: <img src="' . $cid. '" alt="pic">');
$this->assertTrue($message->send()); $this->assertTrue($message->send());
@ -238,10 +244,10 @@ class MessageTest extends VendorTestCase
$cid = $message->embedContent($fileContent, ['fileName' => $fileName, 'contentType' => $contentType]); $cid = $message->embedContent($fileContent, ['fileName' => $fileName, 'contentType' => $contentType]);
$message->setTo($this->testEmailReceiver); $message->to($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->from('someuser@somedomain.com');
$message->setSubject('Yii Swift Embed File Test'); $message->subject('Yii Swift Embed File Test');
$message->setHtml('Embed image: <img src="' . $cid. '" alt="pic">'); $message->html('Embed image: <img src="' . $cid. '" alt="pic">');
$this->assertTrue($message->send()); $this->assertTrue($message->send());
@ -258,11 +264,11 @@ class MessageTest extends VendorTestCase
{ {
$message = $this->createTestMessage(); $message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver); $message->to($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->from('someuser@somedomain.com');
$message->setSubject('Yii Swift Alternative Body Test'); $message->subject('Yii Swift Alternative Body Test');
$message->setHtml('<b>Yii Swift</b> test HTML body'); $message->html('<b>Yii Swift</b> test HTML body');
$message->setText('Yii Swift test plain text body'); $message->text('Yii Swift test plain text body');
$this->assertTrue($message->send()); $this->assertTrue($message->send());
@ -291,10 +297,10 @@ class MessageTest extends VendorTestCase
{ {
$message = $this->createTestMessage(); $message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver); $message->to($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->from('someuser@somedomain.com');
$message->setSubject('Yii Swift Alternative Body Test'); $message->subject('Yii Swift Alternative Body Test');
$message->setText('Yii Swift test plain text body'); $message->text('Yii Swift test plain text body');
$serializedMessage = serialize($message); $serializedMessage = serialize($message);
$this->assertNotEmpty($serializedMessage, 'Unable to serialize message!'); $this->assertNotEmpty($serializedMessage, 'Unable to serialize message!');

71
tests/unit/framework/mail/BaseMailerTest.php

@ -84,10 +84,10 @@ class BaseMailerTest extends TestCase
$this->assertTrue(is_object($view), 'Unable to get default view!'); $this->assertTrue(is_object($view), 'Unable to get default view!');
} }
public function testCreateMessage() public function testComposeMessage()
{ {
$mailer = new Mailer(); $mailer = new Mailer();
$message = $mailer->createMessage(); $message = $mailer->compose();
$this->assertTrue(is_object($message), 'Unable to create message instance!'); $this->assertTrue(is_object($message), 'Unable to create message instance!');
$this->assertEquals($mailer->messageClass, get_class($message), 'Invalid message class!'); $this->assertEquals($mailer->messageClass, get_class($message), 'Invalid message class!');
@ -95,7 +95,7 @@ class BaseMailerTest extends TestCase
'id' => 'test-id', 'id' => 'test-id',
'encoding' => 'test-encoding', 'encoding' => 'test-encoding',
); );
$message = $mailer->createMessage($messageConfig); $message = $mailer->compose($messageConfig);
foreach ($messageConfig as $name => $value) { foreach ($messageConfig as $name => $value) {
$this->assertEquals($value, $message->$name, 'Unable to apply message config!'); $this->assertEquals($value, $message->$name, 'Unable to apply message config!');
@ -103,21 +103,34 @@ class BaseMailerTest extends TestCase
} }
/** /**
* @depends testCreateMessage * @depends testComposeMessage
*/ */
public function testDefaultMessageConfig() public function testDefaultMessageConfig()
{ {
$mailer = new Mailer(); $mailer = new Mailer();
$messageConfig = array( $notPropertyConfig = [
'from' => 'from@domain.com',
'to' => 'to@domain.com',
'cc' => 'cc@domain.com',
'bcc' => 'bcc@domain.com',
'subject' => 'Test subject',
'text' => 'Test text body',
'html' => 'Test HTML body',
];
$propertyConfig = [
'id' => 'test-id', 'id' => 'test-id',
'encoding' => 'test-encoding', 'encoding' => 'test-encoding',
); ];
$messageConfig = array_merge($notPropertyConfig, $propertyConfig);
$mailer->messageConfig = $messageConfig; $mailer->messageConfig = $messageConfig;
$message = $mailer->createMessage(); $message = $mailer->compose();
foreach ($messageConfig as $name => $value) { foreach ($notPropertyConfig as $name => $value) {
$this->assertEquals($value, $message->{'_' . $name});
}
foreach ($propertyConfig as $name => $value) {
$this->assertEquals($value, $message->$name); $this->assertEquals($value, $message->$name);
} }
} }
@ -190,22 +203,50 @@ class Message extends BaseMessage
{ {
public $id; public $id;
public $encoding; public $encoding;
public $_from;
public $_to;
public $_cc;
public $_bcc;
public $_subject;
public $_text;
public $_html;
public function setCharset($charset) {} public function setCharset($charset) {}
public function setFrom($from) {} public function from($from) {
$this->_from = $from;
return $this;
}
public function setTo($to) {} public function to($to) {
$this->_to = $to;
return $this;
}
public function setCc($cc) {} public function cc($cc) {
$this->_cc = $cc;
return $this;
}
public function setBcc($bcc) {} public function bcc($bcc) {
$this->_bcc = $bcc;
return $this;
}
public function setSubject($subject) {} public function subject($subject) {
$this->_subject = $subject;
return $this;
}
public function setText($text) {} public function text($text) {
$this->_text = $text;
return $this;
}
public function setHtml($html) {} public function html($html) {
$this->_html = $html;
return $this;
}
public function attachContent($content, array $options = []) {} public function attachContent($content, array $options = []) {}

18
tests/unit/framework/mail/BaseMessageTest.php

@ -43,7 +43,7 @@ class BaseMessageTest extends TestCase
public function testRender() public function testRender()
{ {
$mailer = $this->getMailer(); $mailer = $this->getMailer();
$message = $mailer->createMessage(); $message = $mailer->compose();
$viewName = 'test/text/view'; $viewName = 'test/text/view';
$message->renderText($viewName); $message->renderText($viewName);
@ -59,7 +59,7 @@ class BaseMessageTest extends TestCase
public function testSend() public function testSend()
{ {
$mailer = $this->getMailer(); $mailer = $this->getMailer();
$message = $mailer->createMessage(); $message = $mailer->compose();
$message->send(); $message->send();
$this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!'); $this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!');
} }
@ -94,21 +94,21 @@ class TestMessage extends BaseMessage
public function setCharset($charset) {} public function setCharset($charset) {}
public function setFrom($from) {} public function from($from) {}
public function setTo($to) {} public function to($to) {}
public function setCc($cc) {} public function cc($cc) {}
public function setBcc($bcc) {} public function bcc($bcc) {}
public function setSubject($subject) {} public function subject($subject) {}
public function setText($text) { public function text($text) {
$this->text = $text; $this->text = $text;
} }
public function setHtml($html) { public function html($html) {
$this->html = $html; $this->html = $html;
} }

Loading…
Cancel
Save