From 1aafa73e1576bbb24827cf11266a717f14bb5c7d Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Thu, 7 Nov 2013 12:18:07 +0200 Subject: [PATCH] Render methods removed from 'yii\mail\MessageInterface'. Method 'yii\mail\MailerInterface::compose()' reworked allowing rendering message body. --- extensions/swiftmailer/yii/swiftmailer/Mailer.php | 2 +- framework/yii/mail/BaseMailer.php | 58 +++++++++--------- framework/yii/mail/BaseMessage.php | 48 --------------- framework/yii/mail/MailerInterface.php | 13 +++- framework/yii/mail/MessageInterface.php | 43 -------------- tests/unit/extensions/swiftmailer/MessageTest.php | 2 +- tests/unit/framework/mail/BaseMailerTest.php | 72 +++++++++++------------ tests/unit/framework/mail/BaseMessageTest.php | 51 ++++------------ 8 files changed, 88 insertions(+), 201 deletions(-) diff --git a/extensions/swiftmailer/yii/swiftmailer/Mailer.php b/extensions/swiftmailer/yii/swiftmailer/Mailer.php index eaca3bd..1d89290 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 message(array $config = []) creates new message instance from given configuration. + * @method Message compose($view = null, array $params = []) creates new message optionally filling up its body via view rendering. * * @author Paul Klimov * @since 2.0 diff --git a/framework/yii/mail/BaseMailer.php b/framework/yii/mail/BaseMailer.php index a827f5a..6ee5899 100644 --- a/framework/yii/mail/BaseMailer.php +++ b/framework/yii/mail/BaseMailer.php @@ -33,7 +33,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont /** * @var string directory containing view files for this email messages. */ - public $viewPath = '@app/mailviews'; + public $viewPath = '@app/mails'; /** * @var string HTML layout view name. */ @@ -54,17 +54,12 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont * - 'subject' argument for [[MessageInterface::subject()]] * - 'text' argument for [[MessageInterface::text()]] * - 'html' argument for [[MessageInterface::html()]] - * - 'body' argument for [[MessageInterface::body()]] - * - 'renderText' list of arguments for [[MessageInterface::renderText()]] - * - 'renderHtml' list of arguments for [[MessageInterface::renderHtml()]] - * - 'renderBody' list of arguments for [[MessageInterface::renderBody()]] * For example: * ~~~ * array( * 'charset' => 'UTF-8', * 'from' => 'noreply@mydomain.com', - * 'bcc' => 'email.test@mydomain.com', - * 'renderText' => ['default/text', ['companyName' => 'YiiApp']], + * 'bcc' => 'developer@mydomain.com', * ) * ~~~ */ @@ -111,16 +106,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. See [[messageConfig]] - * for the configuration format details. + * @inheritdoc + */ + public function compose($view = null, array $params = []) + { + $message = $this->createMessage(); + if ($view !== null) { + $params['message'] = $message; + if (is_array($view)) { + if (array_key_exists('html', $view)) { + $message->html($this->render($view['html'], $params, $this->htmlLayout)); + } + if (array_key_exists('text', $view)) { + $message->text($this->render($view['text'], $params, $this->textLayout)); + } + } else { + $html = $this->render($view, $params, $this->htmlLayout); + $message->html($html); + $message->text(strip_tags($html)); + } + } + return $message; + } + + /** + * Creates mew message instance using configuration from [[messageConfig]]. + * If 'class' parameter is omitted, [[messageClass]] will be used. * @return MessageInterface message instance. */ - public function message(array $config = []) + protected function createMessage() { - $config = array_merge($this->messageConfig, $config); + $config = $this->messageConfig; if (!array_key_exists('class', $config)) { $config['class'] = $this->messageClass; } @@ -133,32 +149,18 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont 'subject', 'text', 'html', - 'body', - ]; - $setupMethodNames = [ - 'renderText', - 'renderHtml', - 'renderBody', ]; $directSetterConfig = []; - $setupMethodConfig = []; foreach ($config as $name => $value) { if (in_array($name, $directSetterNames, true)) { $directSetterConfig[$name] = $value; unset($config[$name]); } - if (in_array($name, $setupMethodNames, true)) { - $setupMethodConfig[$name] = $value; - unset($config[$name]); - } } $message = Yii::createObject($config); foreach ($directSetterConfig as $name => $value) { $message->$name($value); } - foreach ($setupMethodConfig as $method => $arguments) { - call_user_func_array(array($message, $method), $arguments); - } return $message; } diff --git a/framework/yii/mail/BaseMessage.php b/framework/yii/mail/BaseMessage.php index ff6a724..9a6fccf 100644 --- a/framework/yii/mail/BaseMessage.php +++ b/framework/yii/mail/BaseMessage.php @@ -44,54 +44,6 @@ abstract class BaseMessage extends Object implements MessageInterface } /** - * @inheritdoc - */ - public function body($body) - { - if (is_array($body)) { - $this->html($body['html']); - $this->text($body['text']); - } else { - $this->html($body); - $this->text(strip_tags($body)); - } - return $this; - } - - /** - * @inheritdoc - */ - public function renderHtml($view, $params = []) - { - $this->html($this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout)); - return $this; - } - - /** - * @inheritdoc - */ - public function renderText($view, $params = []) - { - $this->text($this->getMailer()->render($view, $params, $this->getMailer()->textLayout)); - return $this; - } - - /** - * @inheritdoc - */ - public function renderBody($view, $params = []) - { - if (is_array($view)) { - $this->renderHtml($view['html'], $params); - $this->renderText($view['text'], $params); - } else { - $html = $this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout); - $this->body($html); - } - return $this; - } - - /** * PHP magic method that returns the string representation of this object. * @return string the string representation of this object. */ diff --git a/framework/yii/mail/MailerInterface.php b/framework/yii/mail/MailerInterface.php index a80f49b..d17c65f 100644 --- a/framework/yii/mail/MailerInterface.php +++ b/framework/yii/mail/MailerInterface.php @@ -18,11 +18,18 @@ namespace yii\mail; interface MailerInterface { /** - * Creates new message instance from given configuration. - * @param array $config message configuration. + * Creates new message optionally filling up its body via view rendering. + * The view to be rendered can be specified in one of the following formats: + * - path alias (e.g. "@app/mails/contact/body"); + * - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]]. + * @param string|array $view view, which should be used to render message body + * - if string - the view name or the path alias of the HTML body view file, in this case + * text body will be composed automatically from html one. + * - if array - list of views for each body type in format: ['html' => 'htmlView', 'text' => 'textView'] + * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. * @return MessageInterface message instance. */ - public function message(array $config = []); + public function compose($view = null, array $params = []); /** * Sends the given email message. diff --git a/framework/yii/mail/MessageInterface.php b/framework/yii/mail/MessageInterface.php index 6bfd421..69344a4 100644 --- a/framework/yii/mail/MessageInterface.php +++ b/framework/yii/mail/MessageInterface.php @@ -97,16 +97,6 @@ interface MessageInterface public function html($html); /** - * Sets message HTML and plain text content. - * @param string|array $body varies method behavior depending on type: - * - string - the HTML body content, in this case text body will be composed from - * html one using [[strip_tags()]] function. - * - array - list of body contents for each body type in format: ['html' => 'htmlContent', 'text' => 'textContent'] - * @return static self reference. - */ - public function body($body); - - /** * Attaches existing file to the email message. * @param string $fileName full file name * @param array $options options for embed file. Valid options are: @@ -155,39 +145,6 @@ interface MessageInterface public function send(); /** - * Fills up HTML body rendering a view. - * The view to be rendered can be specified in one of the following formats: - * - path alias (e.g. "@app/mails/contact/body"); - * - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]]. - * @param string $view the view name or the path alias of the view file. - * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. - * @return static self reference. - */ - public function renderHtml($view, $params = []); - - /** - * Fills up plain text body rendering a view. - * The view to be rendered can be specified in one of the following formats: - * - path alias (e.g. "@app/mails/contact/body"); - * - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]]. - * @param string $view the view name or the path alias of the view file. - * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. - * @return static self reference. - */ - public function renderText($view, $params = []); - - /** - * Composes the message HTML and plain text body. - * @param string|array $view varies method behavior depending on type: - * - string - the view name or the path alias of the HTML body view file, in this case - * text body will be composed from html one using [[strip_tags()]] function. - * - array - list of views for each body type in format: ['html' => 'htmlView', 'text' => 'textView'] - * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. - * @return static self reference. - */ - public function renderBody($view, $params = []); - - /** * Returns string representation of this message. * @return string the string representation of this message. */ diff --git a/tests/unit/extensions/swiftmailer/MessageTest.php b/tests/unit/extensions/swiftmailer/MessageTest.php index 79d33f0..7a6fedf 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')->message(); + return Yii::$app->getComponent('mail')->compose(); } /** diff --git a/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php index 0a145d8..505652c 100644 --- a/tests/unit/framework/mail/BaseMailerTest.php +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -93,26 +93,16 @@ class BaseMailerTest extends TestCase $this->assertTrue(is_object($view), 'Unable to get default view!'); } - public function testComposeMessage() + public function testCreateMessage() { $mailer = new Mailer(); - $message = $mailer->message(); + $message = $mailer->compose(); $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->message($messageConfig); - - foreach ($messageConfig as $name => $value) { - $this->assertEquals($value, $message->$name, 'Unable to apply message config!'); - } } /** - * @depends testComposeMessage + * @depends testCreateMessage */ public function testDefaultMessageConfig() { @@ -135,7 +125,7 @@ class BaseMailerTest extends TestCase $messageConfig = array_merge($notPropertyConfig, $propertyConfig); $mailer->messageConfig = $messageConfig; - $message = $mailer->message(); + $message = $mailer->compose(); foreach ($notPropertyConfig as $name => $value) { $this->assertEquals($value, $message->{'_' . $name}); @@ -165,28 +155,6 @@ class BaseMailerTest extends TestCase } /** - * @depends testComposeMessage - * @depends testRender - */ - public function testComposeSetupMethods() - { - $mailer = $this->getTestMailComponent(); - $mailer->textLayout = false; - - $viewName = 'test_view'; - $viewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $viewName . '.php'; - $viewFileContent = 'view file content'; - file_put_contents($viewFileName, $viewFileContent); - - $messageConfig = array( - 'renderText' => [$viewName], - ); - $message = $mailer->message($messageConfig); - - $this->assertEquals($viewFileContent, $message->_text); - } - - /** * @depends testRender */ public function testRenderLayout() @@ -208,6 +176,38 @@ class BaseMailerTest extends TestCase $renderResult = $mailer->render($viewName, [], $layoutName); $this->assertEquals('Begin Layout ' . $viewFileContent . ' End Layout', $renderResult); } + + /** + * @depends testCreateMessage + * @depends testRender + */ + public function testCompose() + { + $mailer = $this->getTestMailComponent(); + $mailer->htmlLayout = false; + $mailer->textLayout = false; + + $htmlViewName = 'test_html_view'; + $htmlViewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $htmlViewName . '.php'; + $htmlViewFileContent = 'HTML view file content'; + file_put_contents($htmlViewFileName, $htmlViewFileContent); + + $textViewName = 'test_text_view'; + $textViewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $textViewName . '.php'; + $textViewFileContent = 'Plain text view file content'; + file_put_contents($textViewFileName, $textViewFileContent); + + $message = $mailer->compose([ + 'html' => $htmlViewName, + 'text' => $textViewName, + ]); + $this->assertEquals($htmlViewFileContent, $message->_html, 'Unable to render html!'); + $this->assertEquals($textViewFileContent, $message->_text, 'Unable to render text!'); + + $message = $mailer->compose($htmlViewName); + $this->assertEquals($htmlViewFileContent, $message->_html, 'Unable to render html by direct view!'); + $this->assertEquals(strip_tags($htmlViewFileContent), $message->_text, 'Unable to render text by direct view!'); + } } /** diff --git a/tests/unit/framework/mail/BaseMessageTest.php b/tests/unit/framework/mail/BaseMessageTest.php index 3eeb362..7b36a93 100644 --- a/tests/unit/framework/mail/BaseMessageTest.php +++ b/tests/unit/framework/mail/BaseMessageTest.php @@ -40,52 +40,26 @@ class BaseMessageTest extends TestCase // Tests : - public function testRender() + public function testGetMailer() { $mailer = $this->getMailer(); - $message = $mailer->message(); - - $viewName = 'test/text/view'; - $message->renderText($viewName); - $expectedText = 'view=' . $viewName . ' layout=' . $mailer->textLayout; - $this->assertEquals($expectedText, $message->text, 'Unable to render text!'); - - $viewName = 'test/html/view'; - $message->renderHtml($viewName); - $expectedHtml = 'view=' . $viewName . ' layout=' . $mailer->htmlLayout; - $this->assertEquals($expectedHtml, $message->html, 'Unable to render html!'); + $message = $mailer->compose(); + $this->assertEquals($mailer, $message->getMailer()); } - /** - * @depends testRender - */ - public function testComposeBody() + public function testSend() { $mailer = $this->getMailer(); - $message = $mailer->message(); - - $viewName = 'test/html/view'; - $message->renderBody($viewName); - $expectedHtml = 'view=' . $viewName . ' layout=' . $mailer->htmlLayout; - $this->assertEquals($expectedHtml, $message->html, 'Unable to compose html!'); - $expectedText = strip_tags($expectedHtml); - $this->assertEquals($expectedText, $message->text, 'Unable to compose text from html!'); - - $textViewName = 'test/text/view'; - $htmlViewName = 'test/html/view'; - $message->renderBody(['text' => $textViewName, 'html' => $htmlViewName]); - $expectedHtml = 'view=' . $htmlViewName . ' layout=' . $mailer->htmlLayout; - $this->assertEquals($expectedHtml, $message->html, 'Unable to compose html from separated view!'); - $expectedText = 'view=' . $textViewName . ' layout=' . $mailer->textLayout; - $this->assertEquals($expectedText, $message->text, 'Unable to compose text from separated view!'); + $message = $mailer->compose(); + $message->send(); + $this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!'); } - public function testSend() + public function testToString() { $mailer = $this->getMailer(); - $message = $mailer->message(); - $message->send(); - $this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!'); + $message = $mailer->compose(); + $this->assertEquals($message->toString(), '' . $message); } } @@ -97,11 +71,6 @@ class TestMailer extends BaseMailer public $messageClass = 'yiiunit\framework\mail\TestMessage'; public $sentMessages = array(); - public function render($view, $params = [], $layout = false) - { - return 'view=' . $view . ' layout=' . $layout; - } - public function send($message) { $this->sentMessages[] = $message;