mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 06:48:59 +08:00
Render methods removed from 'yii\mail\MessageInterface'.
Method 'yii\mail\MailerInterface::compose()' reworked allowing rendering message body.
This commit is contained in:
@@ -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 <klimov.paul@gmail.com>
|
||||
* @since 2.0
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,54 +43,6 @@ abstract class BaseMessage extends Object implements MessageInterface
|
||||
return $this->getMailer()->send($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -96,16 +96,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
|
||||
@@ -154,39 +144,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.
|
||||
|
||||
@@ -63,7 +63,7 @@ class MessageTest extends VendorTestCase
|
||||
*/
|
||||
protected function createTestMessage()
|
||||
{
|
||||
return Yii::$app->getComponent('mail')->message();
|
||||
return Yii::$app->getComponent('mail')->compose();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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});
|
||||
@@ -164,28 +154,6 @@ class BaseMailerTest extends TestCase
|
||||
$this->assertEquals($params['testParam'], $renderResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
@@ -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 <b>view file</b> 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!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,53 +40,27 @@ 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!');
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testRender
|
||||
*/
|
||||
public function testComposeBody()
|
||||
{
|
||||
$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();
|
||||
$this->assertEquals($mailer, $message->getMailer());
|
||||
}
|
||||
|
||||
public function testSend()
|
||||
{
|
||||
$mailer = $this->getMailer();
|
||||
$message = $mailer->message();
|
||||
$message = $mailer->compose();
|
||||
$message->send();
|
||||
$this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!');
|
||||
}
|
||||
|
||||
public function testToString()
|
||||
{
|
||||
$mailer = $this->getMailer();
|
||||
$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;
|
||||
|
||||
Reference in New Issue
Block a user