From 87af95f7124b34e93291f78f048c12e242bc8dea Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Tue, 5 Nov 2013 14:57:13 +0200 Subject: [PATCH] 'yii\mail\MessageInterface' updated: - setter methods renamed to have pure name - method 'createMessage' renamed to 'compose' --- extensions/swiftmailer/yii/swiftmailer/Mailer.php | 2 +- extensions/swiftmailer/yii/swiftmailer/Message.php | 54 +++------------- framework/yii/mail/BaseMailer.php | 37 +++++++++-- framework/yii/mail/BaseMessage.php | 11 +--- framework/yii/mail/MailerInterface.php | 2 +- framework/yii/mail/MessageInterface.php | 22 +++---- tests/unit/extensions/swiftmailer/MessageTest.php | 72 ++++++++++++---------- tests/unit/framework/mail/BaseMailerTest.php | 71 ++++++++++++++++----- tests/unit/framework/mail/BaseMessageTest.php | 18 +++--- 9 files changed, 159 insertions(+), 130 deletions(-) diff --git a/extensions/swiftmailer/yii/swiftmailer/Mailer.php b/extensions/swiftmailer/yii/swiftmailer/Mailer.php index 60e9abb..9cde0e6 100644 --- a/extensions/swiftmailer/yii/swiftmailer/Mailer.php +++ b/extensions/swiftmailer/yii/swiftmailer/Mailer.php @@ -36,7 +36,7 @@ use Yii; * * @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 * @since 2.0 diff --git a/extensions/swiftmailer/yii/swiftmailer/Message.php b/extensions/swiftmailer/yii/swiftmailer/Message.php index cecb61f..6008dd9 100644 --- a/extensions/swiftmailer/yii/swiftmailer/Message.php +++ b/extensions/swiftmailer/yii/swiftmailer/Message.php @@ -59,7 +59,7 @@ class Message extends BaseMessage /** * @inheritdoc */ - public function setFrom($from) + public function from($from) { $this->getSwiftMessage()->setFrom($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 */ - public function setTo($to) + public function to($to) { $this->getSwiftMessage()->setTo($to); return $this; } /** - * @return array To addresses of this message. - */ - public function getTo() - { - return $this->getSwiftMessage()->getTo(); - } - - /** * @inheritdoc */ - public function setCc($cc) + public function cc($cc) { $this->getSwiftMessage()->setCc($cc); return $this; } /** - * @return array Cc address of this message. - */ - public function getCc() - { - return $this->getSwiftMessage()->getCc(); - } - - /** * @inheritdoc */ - public function setBcc($bcc) + public function bcc($bcc) { $this->getSwiftMessage()->setBcc($bcc); return $this; } /** - * @return array Bcc addresses of this message. - */ - public function getBcc() - { - return $this->getSwiftMessage()->getBcc(); - } - - /** * @inheritdoc */ - public function setSubject($subject) + public function subject($subject) { $this->getSwiftMessage()->setSubject($subject); return $this; } /** - * @return string the subject of this message. - */ - public function getSubject() - { - return $this->getSwiftMessage()->getSubject(); - } - - /** * @inheritdoc */ - public function setText($text) + public function text($text) { $this->setBody($text, 'text/plain'); return $this; @@ -154,7 +114,7 @@ class Message extends BaseMessage /** * @inheritdoc */ - public function setHtml($html) + public function html($html) { $this->setBody($html, 'text/html'); return $this; diff --git a/framework/yii/mail/BaseMailer.php b/framework/yii/mail/BaseMailer.php index 4d61fd6..f131161 100644 --- a/framework/yii/mail/BaseMailer.php +++ b/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 * 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: * ~~~ * array( - * 'encoding' => 'UTF-8', + * 'charset' => 'UTF-8', * 'from' => 'noreply@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. * Message configuration will be merged with [[messageConfig]]. * 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. */ - public function createMessage(array $config = []) + public function compose(array $config = []) { $config = array_merge($this->messageConfig, $config); if (!array_key_exists('class', $config)) { $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; } /** diff --git a/framework/yii/mail/BaseMessage.php b/framework/yii/mail/BaseMessage.php index 20bc487..b3dade7 100644 --- a/framework/yii/mail/BaseMessage.php +++ b/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 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 * @since 2.0 @@ -56,7 +49,7 @@ abstract class BaseMessage extends Object implements MessageInterface */ 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; } @@ -65,7 +58,7 @@ abstract class BaseMessage extends Object implements MessageInterface */ 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; } } \ No newline at end of file diff --git a/framework/yii/mail/MailerInterface.php b/framework/yii/mail/MailerInterface.php index 1ed9256..2541d32 100644 --- a/framework/yii/mail/MailerInterface.php +++ b/framework/yii/mail/MailerInterface.php @@ -22,7 +22,7 @@ interface MailerInterface * @param array $config message configuration. * @return MessageInterface message instance. */ - public function createMessage(array $config = []); + public function compose(array $config = []); /** * Sends the given email message. diff --git a/framework/yii/mail/MessageInterface.php b/framework/yii/mail/MessageInterface.php index e2418d3..d0e4180 100644 --- a/framework/yii/mail/MessageInterface.php +++ b/framework/yii/mail/MessageInterface.php @@ -12,10 +12,10 @@ namespace yii\mail; * Together with application component, which matches the [[MailerInterface]], * it introduces following mail sending syntax: * ~~~php - * Yii::$app->mail->createMessage() - * ->setFrom('from@domain.com') - * ->setTo('to@domain.com') - * ->setSubject('Message Subject') + * Yii::$app->mail->compose() + * ->from('from@domain.com') + * ->to('to@domain.com') + * ->subject('Message Subject') * ->renderText('text/view') * ->renderHtml('html/view') * ->send(); @@ -43,7 +43,7 @@ interface MessageInterface * [email => name]. * @return static self reference. */ - public function setFrom($from); + public function from($from); /** * Sets message receiver. @@ -53,7 +53,7 @@ interface MessageInterface * [email => name]. * @return static self reference. */ - public function setTo($to); + public function to($to); /** * Set the Cc (additional copy receiver) addresses of this message. @@ -63,7 +63,7 @@ interface MessageInterface * [email => name]. * @return static self reference. */ - public function setCc($cc); + public function cc($cc); /** * Set the Bcc (hidden copy receiver) addresses of this message. @@ -73,28 +73,28 @@ interface MessageInterface * [email => name]. * @return static self reference. */ - public function setBcc($bcc); + public function bcc($bcc); /** * Sets message subject. * @param string $subject message subject * @return static self reference. */ - public function setSubject($subject); + public function subject($subject); /** * Sets message plain text content. * @param string $text message plain text content. * @return static self reference. */ - public function setText($text); + public function text($text); /** * Sets message HTML content. * @param string $html message HTML content. * @return static self reference. */ - public function setHtml($html); + public function html($html); /** * Attach specified content as file for the email message. diff --git a/tests/unit/extensions/swiftmailer/MessageTest.php b/tests/unit/extensions/swiftmailer/MessageTest.php index f0077ec..dbb7ce0 100644 --- a/tests/unit/extensions/swiftmailer/MessageTest.php +++ b/tests/unit/extensions/swiftmailer/MessageTest.php @@ -63,7 +63,7 @@ class MessageTest extends VendorTestCase */ 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'; $subject = 'Test Subject'; $to = 'someuser@somedomain.com'; + $cc = 'ccuser@somedomain.com'; + $bcc = 'bccuser@somedomain.com'; $messageString = $this->createTestMessage() ->setCharset($charset) - ->setSubject($subject) - ->setTo($to) + ->subject($subject) + ->to($to) + ->cc($cc) + ->bcc($bcc) ->__toString(); $this->assertContains('charset=' . $charset, $messageString, 'Incorrect charset!'); $this->assertContains('Subject: ' . $subject, $messageString, 'Incorrect "Subject" 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'; $messageString = $this->createTestMessage() - ->setFrom($from) + ->from($from) ->__toString(); $this->assertContains('From: ' . $from, $messageString, 'Incorrect "From" header!'); $this->assertContains('Reply-To: ' . $from, $messageString, 'Incorrect "Reply-To" header!'); @@ -151,10 +157,10 @@ class MessageTest extends VendorTestCase public function testSend() { $message = $this->createTestMessage(); - $message->setTo($this->testEmailReceiver); - $message->setFrom('someuser@somedomain.com'); - $message->setSubject('Yii Swift Test'); - $message->setText('Yii Swift Test body'); + $message->to($this->testEmailReceiver); + $message->from('someuser@somedomain.com'); + $message->subject('Yii Swift Test'); + $message->text('Yii Swift Test body'); $this->assertTrue($message->send()); } @@ -165,10 +171,10 @@ class MessageTest extends VendorTestCase { $message = $this->createTestMessage(); - $message->setTo($this->testEmailReceiver); - $message->setFrom('someuser@somedomain.com'); - $message->setSubject('Yii Swift Attach File Test'); - $message->setText('Yii Swift Attach File Test body'); + $message->to($this->testEmailReceiver); + $message->from('someuser@somedomain.com'); + $message->subject('Yii Swift Attach File Test'); + $message->text('Yii Swift Attach File Test body'); $fileName = __FILE__; $message->attachFile($fileName); @@ -186,10 +192,10 @@ class MessageTest extends VendorTestCase { $message = $this->createTestMessage(); - $message->setTo($this->testEmailReceiver); - $message->setFrom('someuser@somedomain.com'); - $message->setSubject('Yii Swift Create Attachment Test'); - $message->setText('Yii Swift Create Attachment Test body'); + $message->to($this->testEmailReceiver); + $message->from('someuser@somedomain.com'); + $message->subject('Yii Swift Create Attachment Test'); + $message->text('Yii Swift Create Attachment Test body'); $fileName = 'test.txt'; $fileContent = 'Test attachment content'; $message->attachContent($fileContent, ['fileName' => $fileName]); @@ -212,10 +218,10 @@ class MessageTest extends VendorTestCase $cid = $message->embedFile($fileName); - $message->setTo($this->testEmailReceiver); - $message->setFrom('someuser@somedomain.com'); - $message->setSubject('Yii Swift Embed File Test'); - $message->setHtml('Embed image: pic'); + $message->to($this->testEmailReceiver); + $message->from('someuser@somedomain.com'); + $message->subject('Yii Swift Embed File Test'); + $message->html('Embed image: pic'); $this->assertTrue($message->send()); @@ -238,10 +244,10 @@ class MessageTest extends VendorTestCase $cid = $message->embedContent($fileContent, ['fileName' => $fileName, 'contentType' => $contentType]); - $message->setTo($this->testEmailReceiver); - $message->setFrom('someuser@somedomain.com'); - $message->setSubject('Yii Swift Embed File Test'); - $message->setHtml('Embed image: pic'); + $message->to($this->testEmailReceiver); + $message->from('someuser@somedomain.com'); + $message->subject('Yii Swift Embed File Test'); + $message->html('Embed image: pic'); $this->assertTrue($message->send()); @@ -258,11 +264,11 @@ class MessageTest extends VendorTestCase { $message = $this->createTestMessage(); - $message->setTo($this->testEmailReceiver); - $message->setFrom('someuser@somedomain.com'); - $message->setSubject('Yii Swift Alternative Body Test'); - $message->setHtml('Yii Swift test HTML body'); - $message->setText('Yii Swift test plain text body'); + $message->to($this->testEmailReceiver); + $message->from('someuser@somedomain.com'); + $message->subject('Yii Swift Alternative Body Test'); + $message->html('Yii Swift test HTML body'); + $message->text('Yii Swift test plain text body'); $this->assertTrue($message->send()); @@ -291,10 +297,10 @@ class MessageTest extends VendorTestCase { $message = $this->createTestMessage(); - $message->setTo($this->testEmailReceiver); - $message->setFrom('someuser@somedomain.com'); - $message->setSubject('Yii Swift Alternative Body Test'); - $message->setText('Yii Swift test plain text body'); + $message->to($this->testEmailReceiver); + $message->from('someuser@somedomain.com'); + $message->subject('Yii Swift Alternative Body Test'); + $message->text('Yii Swift test plain text body'); $serializedMessage = serialize($message); $this->assertNotEmpty($serializedMessage, 'Unable to serialize message!'); diff --git a/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php index 08a099e..625aab9 100644 --- a/tests/unit/framework/mail/BaseMailerTest.php +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -84,10 +84,10 @@ class BaseMailerTest extends TestCase $this->assertTrue(is_object($view), 'Unable to get default view!'); } - public function testCreateMessage() + public function testComposeMessage() { $mailer = new Mailer(); - $message = $mailer->createMessage(); + $message = $mailer->compose(); $this->assertTrue(is_object($message), 'Unable to create message instance!'); $this->assertEquals($mailer->messageClass, get_class($message), 'Invalid message class!'); @@ -95,7 +95,7 @@ class BaseMailerTest extends TestCase 'id' => 'test-id', 'encoding' => 'test-encoding', ); - $message = $mailer->createMessage($messageConfig); + $message = $mailer->compose($messageConfig); foreach ($messageConfig as $name => $value) { $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() { $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', 'encoding' => 'test-encoding', - ); + ]; + $messageConfig = array_merge($notPropertyConfig, $propertyConfig); $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); } } @@ -190,22 +203,50 @@ class Message extends BaseMessage { public $id; public $encoding; + public $_from; + public $_to; + public $_cc; + public $_bcc; + public $_subject; + public $_text; + public $_html; 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 = []) {} diff --git a/tests/unit/framework/mail/BaseMessageTest.php b/tests/unit/framework/mail/BaseMessageTest.php index 4b934da..9409ed2 100644 --- a/tests/unit/framework/mail/BaseMessageTest.php +++ b/tests/unit/framework/mail/BaseMessageTest.php @@ -43,7 +43,7 @@ class BaseMessageTest extends TestCase public function testRender() { $mailer = $this->getMailer(); - $message = $mailer->createMessage(); + $message = $mailer->compose(); $viewName = 'test/text/view'; $message->renderText($viewName); @@ -59,7 +59,7 @@ class BaseMessageTest extends TestCase public function testSend() { $mailer = $this->getMailer(); - $message = $mailer->createMessage(); + $message = $mailer->compose(); $message->send(); $this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!'); } @@ -94,21 +94,21 @@ class TestMessage extends BaseMessage 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; } - public function setHtml($html) { + public function html($html) { $this->html = $html; }