From 5bc22a077fafa0e8f1100d274dcf407205bdc8c8 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Tue, 29 Oct 2013 12:13:26 +0200 Subject: [PATCH] Interface 'MailerInterface' extracted. Method 'MailerInterface::createMessage()' added. --- extensions/swiftmailer/yii/swiftmailer/Mailer.php | 17 ++++---- extensions/swiftmailer/yii/swiftmailer/Message.php | 11 +++++- framework/yii/mail/BaseMailer.php | 30 ++++++++++----- framework/yii/mail/BaseMessage.php | 12 +----- framework/yii/mail/MailerInterface.php | 40 +++++++++++++++++++ framework/yii/mail/Message.php | 45 ---------------------- framework/yii/mail/MessageInterface.php | 2 +- tests/unit/extensions/swiftmailer/MailerTest.php | 9 +---- tests/unit/extensions/swiftmailer/MessageTest.php | 20 +++++++--- tests/unit/framework/mail/BaseMailerTest.php | 37 +++++++++++++++--- tests/unit/framework/mail/ViewResolverTest.php | 2 +- 11 files changed, 128 insertions(+), 97 deletions(-) create mode 100644 framework/yii/mail/MailerInterface.php delete mode 100644 framework/yii/mail/Message.php diff --git a/extensions/swiftmailer/yii/swiftmailer/Mailer.php b/extensions/swiftmailer/yii/swiftmailer/Mailer.php index 1d5bc5c..f53001b 100644 --- a/extensions/swiftmailer/yii/swiftmailer/Mailer.php +++ b/extensions/swiftmailer/yii/swiftmailer/Mailer.php @@ -19,7 +19,7 @@ use yii\mail\BaseMailer; * 'components' => array( * ... * 'email' => array( - * 'class' => 'yii\email\swift\Mailer', + * 'class' => 'yii\swiftmailer\Mailer', * 'transport' => [ * 'class' => 'Swift_SmtpTransport', * 'host' => 'localhost', @@ -35,12 +35,18 @@ use yii\mail\BaseMailer; * * @see http://swiftmailer.org * + * @method Message createMessage(array $config = []) creates new message instance from given configuration. + * * @author Paul Klimov * @since 2.0 */ class Mailer extends BaseMailer { /** + * @var string message default class name. + */ + public $messageClass = 'yii\swiftmailer\Message'; + /** * @var \Swift_Mailer Swift mailer instance. */ private $_swiftMailer; @@ -131,13 +137,4 @@ class Mailer extends BaseMailer } return $transport; } - - /** - * Creates the Swift email message instance. - * @return \Swift_Message email message instance. - */ - public function createSwiftMessage() - { - return new \Swift_Message(); - } } \ No newline at end of file diff --git a/extensions/swiftmailer/yii/swiftmailer/Message.php b/extensions/swiftmailer/yii/swiftmailer/Message.php index bfefcde..7fc908b 100644 --- a/extensions/swiftmailer/yii/swiftmailer/Message.php +++ b/extensions/swiftmailer/yii/swiftmailer/Message.php @@ -34,7 +34,7 @@ class Message extends BaseMessage public function getSwiftMessage() { if (!is_object($this->_swiftMessage)) { - $this->_swiftMessage = $this->getMailer()->createSwiftMessage(); + $this->_swiftMessage = $this->createSwiftMessage(); } return $this->_swiftMessage; } @@ -171,4 +171,13 @@ class Message extends BaseMessage { return $this->getSwiftMessage()->toString(); } + + /** + * Creates the Swift email message instance. + * @return \Swift_Message email message instance. + */ + protected function createSwiftMessage() + { + return new \Swift_Message(); + } } \ No newline at end of file diff --git a/framework/yii/mail/BaseMailer.php b/framework/yii/mail/BaseMailer.php index a78b6fc..ea7690b 100644 --- a/framework/yii/mail/BaseMailer.php +++ b/framework/yii/mail/BaseMailer.php @@ -20,13 +20,11 @@ use Yii; * * @property \yii\base\View|array $view view instance or its array configuration. * @property \yii\mail\ViewResolver|array $viewResolver view resolver instance or its array configuration. - * @property array $defaultMessageConfig configuration, which should be applied by default to any - * new created email message instance. * * @author Paul Klimov * @since 2.0 */ -abstract class BaseMailer extends Component +abstract class BaseMailer extends Component implements MailerInterface { /** * @var \yii\base\View|array view instance or its array configuration. @@ -49,6 +47,10 @@ abstract class BaseMailer extends Component * ~~~ */ public $messageConfig = []; + /** + * @var string message default class name. + */ + public $messageClass = 'yii\mail\BaseMessage'; /** * @param array|\yii\base\View $view view instance or its array configuration. @@ -123,11 +125,20 @@ abstract class BaseMailer extends Component } /** - * Sends the given email message. - * @param object $message email message instance - * @return boolean whether the message has been sent. + * 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. + * @return MessageInterface message instance. */ - abstract public function send($message); + public function createMessage(array $config = []) + { + $config = array_merge($this->messageConfig, $config); + if (!array_key_exists('class', $config)) { + $config['class'] = $this->messageClass; + } + return Yii::createObject($config); + } /** * Sends a couple of messages at once. @@ -135,9 +146,10 @@ abstract class BaseMailer extends Component * saving resources, for example on open/close connection operations, * they may override this method to create their specific implementation. * @param array $messages list of email messages, which should be sent. - * @return integer number of successfull sends + * @return integer number of successful sends. */ - public function sendMultiple(array $messages) { + public function sendMultiple(array $messages) + { $successCount = 0; foreach ($messages as $message) { if ($this->send($message)) { diff --git a/framework/yii/mail/BaseMessage.php b/framework/yii/mail/BaseMessage.php index f48310c..b573b16 100644 --- a/framework/yii/mail/BaseMessage.php +++ b/framework/yii/mail/BaseMessage.php @@ -39,17 +39,7 @@ abstract class BaseMessage extends Object implements MessageInterface */ public function getMailer() { - return Yii::$app->getComponent('email'); - } - - /** - * Initializes the object. - * This method is invoked at the end of the constructor after the object is initialized with the - * given configuration. - */ - public function init() - { - Yii::configure($this, $this->getMailer()->messageConfig); + return Yii::$app->getComponent('mail'); } /** diff --git a/framework/yii/mail/MailerInterface.php b/framework/yii/mail/MailerInterface.php new file mode 100644 index 0000000..60e231c --- /dev/null +++ b/framework/yii/mail/MailerInterface.php @@ -0,0 +1,40 @@ + + * @since 2.0 + */ +interface MailerInterface +{ + /** + * Creates new message instance from given configuration. + * @param array $config message configuration. + * @return MessageInterface message instance. + */ + public function createMessage(array $config = []); + + /** + * Sends the given email message. + * @param object $message email message instance + * @return boolean whether the message has been sent. + */ + public function send($message); + + /** + * Sends a couple of messages at once. + * Note: some particular mailers may benefit from sending messages as batch, + * saving resources, for example on open/close connection operations. + * @param array $messages list of email messages, which should be sent. + * @return integer number of successful sends. + */ + public function sendMultiple(array $messages); +} \ No newline at end of file diff --git a/framework/yii/mail/Message.php b/framework/yii/mail/Message.php deleted file mode 100644 index fb34e24..0000000 --- a/framework/yii/mail/Message.php +++ /dev/null @@ -1,45 +0,0 @@ -from = 'sender@domain.com'; - * $email->to = 'receiver@domain.com'; - * $email->subject = 'Message Subject'; - * $email->text = 'Message Content'; - * $email->send(); - * ~~~ - * - * You can use message object to render view, which can be used to compose the message content: - * ~~~ - * $email = new Message(); - * $email->from = $contactForm->email; - * $email->to = 'admin@domain.com'; - * $email->subject = $email->render('contact/subject', ['form' => $contactForm]); - * $email->addHtml($email->render('contact/html', ['form' => $contactForm])); - * $email->addText($email->render('contact/text', ['form' => $contactForm])); - * $email->send(); - * ~~~ - * - * This particular class uses 'SwiftMailer' library to perform the message sending. - * Note: you can replace usage of this class by your own one, using [[Yii::$classMap]]: - * ~~~ - * Yii::$classMap['yii\mail\Message'] = '/path/to/my/email/Message.php' - * ~~~ - * - * @author Paul Klimov - * @since 2.0 - */ -class Message extends SwiftMessage {} \ No newline at end of file diff --git a/framework/yii/mail/MessageInterface.php b/framework/yii/mail/MessageInterface.php index 48a4f10..cdf2d3d 100644 --- a/framework/yii/mail/MessageInterface.php +++ b/framework/yii/mail/MessageInterface.php @@ -8,7 +8,7 @@ namespace yii\mail; /** - * Class MessageInterface + * MessageInterface is an interface, which email message should apply. * * @author Paul Klimov * @since 2.0 diff --git a/tests/unit/extensions/swiftmailer/MailerTest.php b/tests/unit/extensions/swiftmailer/MailerTest.php index 14c3719..306e128 100644 --- a/tests/unit/extensions/swiftmailer/MailerTest.php +++ b/tests/unit/extensions/swiftmailer/MailerTest.php @@ -9,7 +9,7 @@ use yiiunit\VendorTestCase; /** * @group vendor - * @group email + * @group mail * @group swiftmailer */ class MailerTest extends VendorTestCase @@ -66,11 +66,4 @@ class MailerTest extends VendorTestCase $mailer = new Mailer(); $this->assertTrue(is_object($mailer->getSwiftMailer()), 'Unable to get Swift mailer instance!'); } - - public function testCreateSwiftMessage() - { - $mailer = new Mailer(); - $message = $mailer->createSwiftMessage(); - $this->assertTrue(is_object($message), 'Unable to create Swift message instance!'); - } } \ No newline at end of file diff --git a/tests/unit/extensions/swiftmailer/MessageTest.php b/tests/unit/extensions/swiftmailer/MessageTest.php index dabe831..d55ac3a 100644 --- a/tests/unit/extensions/swiftmailer/MessageTest.php +++ b/tests/unit/extensions/swiftmailer/MessageTest.php @@ -9,7 +9,7 @@ use yiiunit\VendorTestCase; /** * @group vendor - * @group email + * @group mail * @group swiftmailer */ class MessageTest extends VendorTestCase @@ -23,7 +23,7 @@ class MessageTest extends VendorTestCase { $this->mockApplication([ 'components' => [ - 'email' => $this->createTestEmailComponent() + 'mail' => $this->createTestEmailComponent() ] ]); } @@ -37,6 +37,14 @@ class MessageTest extends VendorTestCase return $component; } + /** + * @return Message test message instance. + */ + protected function createTestMessage() + { + return Yii::$app->getComponent('mail')->createMessage(); + } + // Tests : public function testGetSwiftMessage() @@ -50,7 +58,7 @@ class MessageTest extends VendorTestCase */ public function testSend() { - $message = new Message(); + $message = $this->createTestMessage(); $message->setTo($this->testEmailReceiver); $message->setFrom('someuser@somedomain.com'); $message->setSubject('Yii Swift Test'); @@ -63,7 +71,7 @@ class MessageTest extends VendorTestCase */ public function testAttachFile() { - $message = new Message(); + $message = $this->createTestMessage(); $message->setTo($this->testEmailReceiver); $message->setFrom('someuser@somedomain.com'); $message->setSubject('Yii Swift Attach File Test'); @@ -77,7 +85,7 @@ class MessageTest extends VendorTestCase */ public function testCreateAttachment() { - $message = new Message(); + $message = $this->createTestMessage(); $message->setTo($this->testEmailReceiver); $message->setFrom('someuser@somedomain.com'); $message->setSubject('Yii Swift Create Attachment Test'); @@ -91,7 +99,7 @@ class MessageTest extends VendorTestCase */ public function testSendAlternativeBody() { - $message = new Message(); + $message = $this->createTestMessage(); $message->setTo($this->testEmailReceiver); $message->setFrom('someuser@somedomain.com'); $message->setSubject('Yii Swift Alternative Body Test'); diff --git a/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php index 84bb293..ff37da2 100644 --- a/tests/unit/framework/mail/BaseMailerTest.php +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -11,14 +11,17 @@ use yii\helpers\FileHelper; use yiiunit\TestCase; /** - * @group email + * @group mail */ class BaseMailerTest extends TestCase { public function setUp() { - $this->mockApplication(); - Yii::$app->setComponent('email', $this->createTestEmailComponent()); + $this->mockApplication([ + 'components' => [ + 'mail' => $this->createTestEmailComponent() + ] + ]); $filePath = $this->getTestFilePath(); if (!file_exists($filePath)) { FileHelper::createDirectory($filePath); @@ -109,15 +112,38 @@ class BaseMailerTest extends TestCase $this->assertTrue(is_object($viewResolver), 'Unable to get default view resolver!'); } + public function testCreateMessage() + { + $mailer = new Mailer(); + $message = $mailer->createMessage(); + $this->assertTrue(is_object($message), 'Unable to create message instance!'); + $this->assertEquals($mailer->messageClass, get_class($message), 'Invalid message class!'); + + $messageConfig = array( + 'id' => 'test-id', + 'encoding' => 'test-encoding', + ); + $message = $mailer->createMessage($messageConfig); + + foreach ($messageConfig as $name => $value) { + $this->assertEquals($value, $message->$name, 'Unable to apply message config!'); + } + } + + /** + * @depends testCreateMessage + */ public function testDefaultMessageConfig() { + $mailer = new Mailer(); + $messageConfig = array( 'id' => 'test-id', 'encoding' => 'test-encoding', ); - Yii::$app->getComponent('email')->messageConfig = $messageConfig; + $mailer->messageConfig = $messageConfig; - $message = new Message(); + $message = $mailer->createMessage(); foreach ($messageConfig as $name => $value) { $this->assertEquals($value, $message->$name); @@ -153,6 +179,7 @@ class BaseMailerTest extends TestCase */ class Mailer extends BaseMailer { + public $messageClass = 'yiiunit\framework\mail\Message'; public $sentMessages = array(); public function send($message) diff --git a/tests/unit/framework/mail/ViewResolverTest.php b/tests/unit/framework/mail/ViewResolverTest.php index 4ff4dbd..5caa947 100644 --- a/tests/unit/framework/mail/ViewResolverTest.php +++ b/tests/unit/framework/mail/ViewResolverTest.php @@ -7,7 +7,7 @@ use Yii; use yiiunit\TestCase; /** - * @group email + * @group mail */ class ViewResolverTest extends TestCase {