Browse Source

Interface 'MailerInterface' extracted.

Method 'MailerInterface::createMessage()' added.
tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
5bc22a077f
  1. 17
      extensions/swiftmailer/yii/swiftmailer/Mailer.php
  2. 11
      extensions/swiftmailer/yii/swiftmailer/Message.php
  3. 30
      framework/yii/mail/BaseMailer.php
  4. 12
      framework/yii/mail/BaseMessage.php
  5. 40
      framework/yii/mail/MailerInterface.php
  6. 45
      framework/yii/mail/Message.php
  7. 2
      framework/yii/mail/MessageInterface.php
  8. 9
      tests/unit/extensions/swiftmailer/MailerTest.php
  9. 20
      tests/unit/extensions/swiftmailer/MessageTest.php
  10. 37
      tests/unit/framework/mail/BaseMailerTest.php
  11. 2
      tests/unit/framework/mail/ViewResolverTest.php

17
extensions/swiftmailer/yii/swiftmailer/Mailer.php

@ -19,7 +19,7 @@ use yii\mail\BaseMailer;
* 'components' => array( * 'components' => array(
* ... * ...
* 'email' => array( * 'email' => array(
* 'class' => 'yii\email\swift\Mailer', * 'class' => 'yii\swiftmailer\Mailer',
* 'transport' => [ * 'transport' => [
* 'class' => 'Swift_SmtpTransport', * 'class' => 'Swift_SmtpTransport',
* 'host' => 'localhost', * 'host' => 'localhost',
@ -35,12 +35,18 @@ use yii\mail\BaseMailer;
* *
* @see http://swiftmailer.org * @see http://swiftmailer.org
* *
* @method Message createMessage(array $config = []) creates new message instance from given configuration.
*
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Mailer extends BaseMailer class Mailer extends BaseMailer
{ {
/** /**
* @var string message default class name.
*/
public $messageClass = 'yii\swiftmailer\Message';
/**
* @var \Swift_Mailer Swift mailer instance. * @var \Swift_Mailer Swift mailer instance.
*/ */
private $_swiftMailer; private $_swiftMailer;
@ -131,13 +137,4 @@ class Mailer extends BaseMailer
} }
return $transport; return $transport;
} }
/**
* Creates the Swift email message instance.
* @return \Swift_Message email message instance.
*/
public function createSwiftMessage()
{
return new \Swift_Message();
}
} }

11
extensions/swiftmailer/yii/swiftmailer/Message.php

@ -34,7 +34,7 @@ class Message extends BaseMessage
public function getSwiftMessage() public function getSwiftMessage()
{ {
if (!is_object($this->_swiftMessage)) { if (!is_object($this->_swiftMessage)) {
$this->_swiftMessage = $this->getMailer()->createSwiftMessage(); $this->_swiftMessage = $this->createSwiftMessage();
} }
return $this->_swiftMessage; return $this->_swiftMessage;
} }
@ -171,4 +171,13 @@ class Message extends BaseMessage
{ {
return $this->getSwiftMessage()->toString(); return $this->getSwiftMessage()->toString();
} }
/**
* Creates the Swift email message instance.
* @return \Swift_Message email message instance.
*/
protected function createSwiftMessage()
{
return new \Swift_Message();
}
} }

30
framework/yii/mail/BaseMailer.php

@ -20,13 +20,11 @@ use Yii;
* *
* @property \yii\base\View|array $view view instance or its array configuration. * @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 \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> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @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. * @var \yii\base\View|array view instance or its array configuration.
@ -49,6 +47,10 @@ abstract class BaseMailer extends Component
* ~~~ * ~~~
*/ */
public $messageConfig = []; 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. * @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. * Creates new message instance from given configuration.
* @param object $message email message instance * Message configuration will be merged with [[messageConfig]].
* @return boolean whether the message has been sent. * 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. * 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, * saving resources, for example on open/close connection operations,
* they may override this method to create their specific implementation. * they may override this method to create their specific implementation.
* @param array $messages list of email messages, which should be sent. * @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; $successCount = 0;
foreach ($messages as $message) { foreach ($messages as $message) {
if ($this->send($message)) { if ($this->send($message)) {

12
framework/yii/mail/BaseMessage.php

@ -39,17 +39,7 @@ abstract class BaseMessage extends Object implements MessageInterface
*/ */
public function getMailer() public function getMailer()
{ {
return Yii::$app->getComponent('email'); return Yii::$app->getComponent('mail');
}
/**
* 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);
} }
/** /**

40
framework/yii/mail/MailerInterface.php

@ -0,0 +1,40 @@
<?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);
}

45
framework/yii/mail/Message.php

@ -1,45 +0,0 @@
<?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 {}

2
framework/yii/mail/MessageInterface.php

@ -8,7 +8,7 @@
namespace yii\mail; namespace yii\mail;
/** /**
* Class MessageInterface * MessageInterface is an interface, which email message should apply.
* *
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0

9
tests/unit/extensions/swiftmailer/MailerTest.php

@ -9,7 +9,7 @@ use yiiunit\VendorTestCase;
/** /**
* @group vendor * @group vendor
* @group email * @group mail
* @group swiftmailer * @group swiftmailer
*/ */
class MailerTest extends VendorTestCase class MailerTest extends VendorTestCase
@ -66,11 +66,4 @@ class MailerTest extends VendorTestCase
$mailer = new Mailer(); $mailer = new Mailer();
$this->assertTrue(is_object($mailer->getSwiftMailer()), 'Unable to get Swift mailer instance!'); $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!');
}
} }

20
tests/unit/extensions/swiftmailer/MessageTest.php

@ -9,7 +9,7 @@ use yiiunit\VendorTestCase;
/** /**
* @group vendor * @group vendor
* @group email * @group mail
* @group swiftmailer * @group swiftmailer
*/ */
class MessageTest extends VendorTestCase class MessageTest extends VendorTestCase
@ -23,7 +23,7 @@ class MessageTest extends VendorTestCase
{ {
$this->mockApplication([ $this->mockApplication([
'components' => [ 'components' => [
'email' => $this->createTestEmailComponent() 'mail' => $this->createTestEmailComponent()
] ]
]); ]);
} }
@ -37,6 +37,14 @@ class MessageTest extends VendorTestCase
return $component; return $component;
} }
/**
* @return Message test message instance.
*/
protected function createTestMessage()
{
return Yii::$app->getComponent('mail')->createMessage();
}
// Tests : // Tests :
public function testGetSwiftMessage() public function testGetSwiftMessage()
@ -50,7 +58,7 @@ class MessageTest extends VendorTestCase
*/ */
public function testSend() public function testSend()
{ {
$message = new Message(); $message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver); $message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Test'); $message->setSubject('Yii Swift Test');
@ -63,7 +71,7 @@ class MessageTest extends VendorTestCase
*/ */
public function testAttachFile() public function testAttachFile()
{ {
$message = new Message(); $message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver); $message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Attach File Test'); $message->setSubject('Yii Swift Attach File Test');
@ -77,7 +85,7 @@ class MessageTest extends VendorTestCase
*/ */
public function testCreateAttachment() public function testCreateAttachment()
{ {
$message = new Message(); $message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver); $message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Create Attachment Test'); $message->setSubject('Yii Swift Create Attachment Test');
@ -91,7 +99,7 @@ class MessageTest extends VendorTestCase
*/ */
public function testSendAlternativeBody() public function testSendAlternativeBody()
{ {
$message = new Message(); $message = $this->createTestMessage();
$message->setTo($this->testEmailReceiver); $message->setTo($this->testEmailReceiver);
$message->setFrom('someuser@somedomain.com'); $message->setFrom('someuser@somedomain.com');
$message->setSubject('Yii Swift Alternative Body Test'); $message->setSubject('Yii Swift Alternative Body Test');

37
tests/unit/framework/mail/BaseMailerTest.php

@ -11,14 +11,17 @@ use yii\helpers\FileHelper;
use yiiunit\TestCase; use yiiunit\TestCase;
/** /**
* @group email * @group mail
*/ */
class BaseMailerTest extends TestCase class BaseMailerTest extends TestCase
{ {
public function setUp() public function setUp()
{ {
$this->mockApplication(); $this->mockApplication([
Yii::$app->setComponent('email', $this->createTestEmailComponent()); 'components' => [
'mail' => $this->createTestEmailComponent()
]
]);
$filePath = $this->getTestFilePath(); $filePath = $this->getTestFilePath();
if (!file_exists($filePath)) { if (!file_exists($filePath)) {
FileHelper::createDirectory($filePath); FileHelper::createDirectory($filePath);
@ -109,15 +112,38 @@ class BaseMailerTest extends TestCase
$this->assertTrue(is_object($viewResolver), 'Unable to get default view resolver!'); $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() public function testDefaultMessageConfig()
{ {
$mailer = new Mailer();
$messageConfig = array( $messageConfig = array(
'id' => 'test-id', 'id' => 'test-id',
'encoding' => 'test-encoding', 'encoding' => 'test-encoding',
); );
Yii::$app->getComponent('email')->messageConfig = $messageConfig; $mailer->messageConfig = $messageConfig;
$message = new Message(); $message = $mailer->createMessage();
foreach ($messageConfig as $name => $value) { foreach ($messageConfig as $name => $value) {
$this->assertEquals($value, $message->$name); $this->assertEquals($value, $message->$name);
@ -153,6 +179,7 @@ class BaseMailerTest extends TestCase
*/ */
class Mailer extends BaseMailer class Mailer extends BaseMailer
{ {
public $messageClass = 'yiiunit\framework\mail\Message';
public $sentMessages = array(); public $sentMessages = array();
public function send($message) public function send($message)

2
tests/unit/framework/mail/ViewResolverTest.php

@ -7,7 +7,7 @@ use Yii;
use yiiunit\TestCase; use yiiunit\TestCase;
/** /**
* @group email * @group mail
*/ */
class ViewResolverTest extends TestCase class ViewResolverTest extends TestCase
{ {

Loading…
Cancel
Save