Commit 5bc22a07 by Paul Klimov

Interface 'MailerInterface' extracted.

Method 'MailerInterface::createMessage()' added.
parent 31ea3f89
......@@ -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 <klimov.paul@gmail.com>
* @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
......@@ -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
......@@ -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 <klimov.paul@gmail.com>
* @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)) {
......
......@@ -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');
}
/**
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mail;
/**
* MailerInterface is an interface, which any mailer should apply.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mail;
use yii\swiftmailer\Message as SwiftMessage;
/**
* Message provides the email message sending functionality.
*
* Usage:
* ~~~
* $email = new Message();
* $email->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 <klimov.paul@gmail.com>
* @since 2.0
*/
class Message extends SwiftMessage {}
\ No newline at end of file
......@@ -8,7 +8,7 @@
namespace yii\mail;
/**
* Class MessageInterface
* MessageInterface is an interface, which email message should apply.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
......
......@@ -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
......@@ -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');
......
......@@ -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)
......
......@@ -7,7 +7,7 @@ use Yii;
use yiiunit\TestCase;
/**
* @group email
* @group mail
*/
class ViewResolverTest extends TestCase
{
......
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