Commit 28b03273 by Klimov Paul

Method 'yii\mail\BaseMailer::compose()' updated, allowing composition of the…

Method 'yii\mail\BaseMailer::compose()' updated, allowing composition of the complex methods like 'renderText' and 'body'.
parent 45d02d07
...@@ -46,19 +46,23 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont ...@@ -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 * @var array configuration, which should be applied by default to any new created
* email message instance. * email message instance.
* In addition to normal [[Yii::createObject()]] behavior extra config keys are available: * In addition to normal [[Yii::createObject()]] behavior extra config keys are available:
* - 'from' invokes [[MessageInterface::from()]] * - 'from' argument for [[MessageInterface::from()]]
* - 'to' invokes [[MessageInterface::to()]] * - 'to' argument for [[MessageInterface::to()]]
* - 'cc' invokes [[MessageInterface::cc()]] * - 'cc' argument for [[MessageInterface::cc()]]
* - 'bcc' invokes [[MessageInterface::bcc()]] * - 'bcc' argument for [[MessageInterface::bcc()]]
* - 'subject' invokes [[MessageInterface::subject()]] * - 'subject' argument for [[MessageInterface::subject()]]
* - 'text' invokes [[MessageInterface::text()]] * - 'text' argument for [[MessageInterface::text()]]
* - 'html' invokes [[MessageInterface::html()]] * - '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: * For example:
* ~~~ * ~~~
* array( * array(
* 'charset' => 'UTF-8', * 'charset' => 'UTF-8',
* 'from' => 'noreply@mydomain.com', * 'from' => 'noreply@mydomain.com',
* 'bcc' => 'email.test@mydomain.com', * 'bcc' => 'email.test@mydomain.com',
* 'renderText' => ['default/text', ['companyName' => 'YiiApp']],
* ) * )
* ~~~ * ~~~
*/ */
...@@ -118,7 +122,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont ...@@ -118,7 +122,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
if (!array_key_exists('class', $config)) { if (!array_key_exists('class', $config)) {
$config['class'] = $this->messageClass; $config['class'] = $this->messageClass;
} }
$configMethodNames = [ $directSetterNames = [
'from', 'from',
'to', 'to',
'cc', 'cc',
...@@ -127,17 +131,30 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont ...@@ -127,17 +131,30 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
'text', 'text',
'html', 'html',
]; ];
$methodBasedConfig = []; $setupMethodNames = [
'renderText',
'renderHtml',
'body',
];
$directSetterConfig = [];
$setupMethodConfig = [];
foreach ($config as $name => $value) { foreach ($config as $name => $value) {
if (in_array($name, $configMethodNames, true)) { if (in_array($name, $directSetterNames, true)) {
$methodBasedConfig[$name] = $value; $directSetterConfig[$name] = $value;
unset($config[$name]);
}
if (in_array($name, $setupMethodNames, true)) {
$setupMethodConfig[$name] = $value;
unset($config[$name]); unset($config[$name]);
} }
} }
$message = Yii::createObject($config); $message = Yii::createObject($config);
foreach ($methodBasedConfig as $name => $value) { foreach ($directSetterConfig as $name => $value) {
$message->$name($value); $message->$name($value);
} }
foreach ($setupMethodConfig as $method => $arguments) {
call_user_func_array(array($message, $method), $arguments);
}
return $message; return $message;
} }
......
...@@ -18,7 +18,7 @@ class BaseMailerTest extends TestCase ...@@ -18,7 +18,7 @@ class BaseMailerTest extends TestCase
{ {
$this->mockApplication([ $this->mockApplication([
'components' => [ 'components' => [
'mail' => $this->createTestEmailComponent() 'mail' => $this->createTestMailComponent(),
] ]
]); ]);
$filePath = $this->getTestFilePath(); $filePath = $this->getTestFilePath();
...@@ -46,12 +46,21 @@ class BaseMailerTest extends TestCase ...@@ -46,12 +46,21 @@ class BaseMailerTest extends TestCase
/** /**
* @return Mailer test email component instance. * @return Mailer test email component instance.
*/ */
protected function createTestEmailComponent() protected function createTestMailComponent()
{ {
$component = new Mailer(); $component = new Mailer();
$component->viewPath = $this->getTestFilePath();
return $component; return $component;
} }
/**
* @return Mailer mailer instance
*/
protected function getTestMailComponent()
{
return Yii::$app->getComponent('mail');
}
// Tests : // Tests :
public function testSetupView() public function testSetupView()
...@@ -135,18 +144,17 @@ class BaseMailerTest extends TestCase ...@@ -135,18 +144,17 @@ class BaseMailerTest extends TestCase
} }
} }
/** /**
* @depends testGetDefaultView * @depends testGetDefaultView
*/ */
public function testRender() public function testRender()
{ {
$mailer = new Mailer(); $mailer = $this->getTestMailComponent();
$filePath = $this->getTestFilePath();
$mailer->viewPath = $filePath;
$viewName = 'test_view'; $viewName = 'test_view';
$viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; $viewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $viewName . '.php';
$viewFileContent = '<?php echo $testParam; ?>'; $viewFileContent = '<?php echo $testParam; ?>';
file_put_contents($viewFileName, $viewFileContent); file_put_contents($viewFileName, $viewFileContent);
...@@ -158,14 +166,35 @@ class BaseMailerTest extends TestCase ...@@ -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 * @depends testRender
*/ */
public function testRenderLayout() public function testRenderLayout()
{ {
$mailer = new Mailer(); $mailer = $this->getTestMailComponent();
$filePath = $this->getTestFilePath(); $filePath = $this->getTestFilePath();
$mailer->viewPath = $filePath;
$viewName = 'test_view'; $viewName = 'test_view';
$viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; $viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment