diff --git a/framework/yii/mail/BaseMailer.php b/framework/yii/mail/BaseMailer.php index f131161..ed236c8 100644 --- a/framework/yii/mail/BaseMailer.php +++ b/framework/yii/mail/BaseMailer.php @@ -46,19 +46,23 @@ 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()]] + * - 'from' argument for [[MessageInterface::from()]] + * - 'to' argument for [[MessageInterface::to()]] + * - 'cc' argument for [[MessageInterface::cc()]] + * - 'bcc' argument for [[MessageInterface::bcc()]] + * - 'subject' argument for [[MessageInterface::subject()]] + * - 'text' argument for [[MessageInterface::text()]] + * - 'html' argument for [[MessageInterface::html()]] + * - 'renderText' list of arguments for [[MessageInterface::renderText()]] + * - 'renderHtml' list of arguments for [[MessageInterface::renderHtml()]] + * - 'body' list of arguments for [[MessageInterface::body()]] * For example: * ~~~ * array( * 'charset' => 'UTF-8', * 'from' => 'noreply@mydomain.com', * 'bcc' => 'email.test@mydomain.com', + * 'renderText' => ['default/text', ['companyName' => 'YiiApp']], * ) * ~~~ */ @@ -118,7 +122,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont if (!array_key_exists('class', $config)) { $config['class'] = $this->messageClass; } - $configMethodNames = [ + $directSetterNames = [ 'from', 'to', 'cc', @@ -127,17 +131,30 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont 'text', 'html', ]; - $methodBasedConfig = []; + $setupMethodNames = [ + 'renderText', + 'renderHtml', + 'body', + ]; + $directSetterConfig = []; + $setupMethodConfig = []; foreach ($config as $name => $value) { - if (in_array($name, $configMethodNames, true)) { - $methodBasedConfig[$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 ($methodBasedConfig as $name => $value) { + 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/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php index 625aab9..c769e61 100644 --- a/tests/unit/framework/mail/BaseMailerTest.php +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -18,7 +18,7 @@ class BaseMailerTest extends TestCase { $this->mockApplication([ 'components' => [ - 'mail' => $this->createTestEmailComponent() + 'mail' => $this->createTestMailComponent(), ] ]); $filePath = $this->getTestFilePath(); @@ -46,12 +46,21 @@ class BaseMailerTest extends TestCase /** * @return Mailer test email component instance. */ - protected function createTestEmailComponent() + protected function createTestMailComponent() { $component = new Mailer(); + $component->viewPath = $this->getTestFilePath(); return $component; } + /** + * @return Mailer mailer instance + */ + protected function getTestMailComponent() + { + return Yii::$app->getComponent('mail'); + } + // Tests : public function testSetupView() @@ -135,18 +144,17 @@ class BaseMailerTest extends TestCase } } + + /** * @depends testGetDefaultView */ public function testRender() { - $mailer = new Mailer(); - - $filePath = $this->getTestFilePath(); - $mailer->viewPath = $filePath; + $mailer = $this->getTestMailComponent(); $viewName = 'test_view'; - $viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; + $viewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $viewName . '.php'; $viewFileContent = ''; file_put_contents($viewFileName, $viewFileContent); @@ -158,14 +166,35 @@ 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->compose($messageConfig); + + $this->assertEquals($viewFileContent, $message->_text); + } + + /** * @depends testRender */ public function testRenderLayout() { - $mailer = new Mailer(); + $mailer = $this->getTestMailComponent(); $filePath = $this->getTestFilePath(); - $mailer->viewPath = $filePath; $viewName = 'test_view'; $viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php';