From 88126bc4d3147a4bef44c17b55935616b57af041 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Wed, 23 Oct 2013 12:19:01 +0300 Subject: [PATCH] Unit tests for \yii\email\swift have been improved. Methods 'addText' and 'addHtml' added to \yii\email\BaseMessage. --- framework/yii/email/BaseMessage.php | 12 +++++ framework/yii/email/swift/Message.php | 18 +++++++ tests/unit/framework/email/BaseMailerTest.php | 4 ++ tests/unit/framework/email/swift/MailerTest.php | 16 ------- tests/unit/framework/email/swift/MessageTest.php | 60 ++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 16 deletions(-) diff --git a/framework/yii/email/BaseMessage.php b/framework/yii/email/BaseMessage.php index 8127c9c..506563b 100644 --- a/framework/yii/email/BaseMessage.php +++ b/framework/yii/email/BaseMessage.php @@ -94,6 +94,18 @@ abstract class BaseMessage extends Object abstract public function setHtml($html); /** + * Add message plain text content part. + * @param string $text message plain text content. + */ + abstract public function addText($text); + + /** + * Add message HTML content part. + * @param string $html message HTML content. + */ + abstract public function addHtml($html); + + /** * Create file attachment for the email message. * @param string $content attachment file content. * @param string $fileName attachment file name. diff --git a/framework/yii/email/swift/Message.php b/framework/yii/email/swift/Message.php index 49a4b75..5b8c575 100644 --- a/framework/yii/email/swift/Message.php +++ b/framework/yii/email/swift/Message.php @@ -100,6 +100,24 @@ class Message extends BaseMessage } /** + * Add message plain text content part. + * @param string $text message plain text content. + */ + public function addText($text) + { + $this->getSwiftMessage()->addPart($text, 'text/plain'); + } + + /** + * Add message HTML content part. + * @param string $html message HTML content. + */ + public function addHtml($html) + { + $this->getSwiftMessage()->addPart($html, 'text/html'); + } + + /** * Create file attachment for the email message. * @param string $content - attachment file content. * @param string $fileName - attachment file name. diff --git a/tests/unit/framework/email/BaseMailerTest.php b/tests/unit/framework/email/BaseMailerTest.php index 8418d2b..178028b 100644 --- a/tests/unit/framework/email/BaseMailerTest.php +++ b/tests/unit/framework/email/BaseMailerTest.php @@ -93,5 +93,9 @@ class Message extends BaseMessage public function setHtml($html) {} + public function addText($text) {} + + public function addHtml($html) {} + public function createAttachment($content, $fileName, $contentType = 'application/octet-stream') {} } \ No newline at end of file diff --git a/tests/unit/framework/email/swift/MailerTest.php b/tests/unit/framework/email/swift/MailerTest.php index f984139..f9cee72 100644 --- a/tests/unit/framework/email/swift/MailerTest.php +++ b/tests/unit/framework/email/swift/MailerTest.php @@ -71,20 +71,4 @@ class MailerTest extends TestCase $message = $mailer->createSwiftMessage(); $this->assertTrue(is_object($message), 'Unable to create Swift message instance!'); } - - /** - * @depends testGetSwiftMailer - * @depends testCreateSwiftMessage - */ - public function testSend() - { - $emailAddress = 'someuser@somedomain.com'; - $message = new Message(); - $message->setTo($emailAddress); - $message->setFrom($emailAddress); - $message->setSubject('Yii Swift Test'); - $message->setText('Yii Swift Test body'); - $message->send(); - $this->assertTrue(true); - } } \ No newline at end of file diff --git a/tests/unit/framework/email/swift/MessageTest.php b/tests/unit/framework/email/swift/MessageTest.php index 50742c9..112f4fc 100644 --- a/tests/unit/framework/email/swift/MessageTest.php +++ b/tests/unit/framework/email/swift/MessageTest.php @@ -13,6 +13,11 @@ use yiiunit\TestCase; */ class MessageTest extends TestCase { + /** + * @var string test email address, which will be used as receiver for the messages. + */ + protected $testEmailReceiver = 'someuser@somedomain.com'; + public function setUp() { $this->mockApplication(array( @@ -37,4 +42,59 @@ class MessageTest extends TestCase $message = new Message(); $this->assertTrue(is_object($message->getSwiftMessage()), 'Unable to get Swift message!'); } + + /** + * @depends testGetSwiftMessage + */ + public function testSend() + { + $message = new Message(); + $message->setTo($this->testEmailReceiver); + $message->setFrom('someuser@somedomain.com'); + $message->setSubject('Yii Swift Test'); + $message->setText('Yii Swift Test body'); + $this->assertTrue($message->send()); + } + + /** + * @depends testSend + */ + public function testAttachFile() + { + $message = new Message(); + $message->setTo($this->testEmailReceiver); + $message->setFrom('someuser@somedomain.com'); + $message->setSubject('Yii Swift Attach File Test'); + $message->setText('Yii Swift Attach File Test body'); + $message->attachFile(__FILE__); + $this->assertTrue($message->send()); + } + + /** + * @depends testSend + */ + public function testCreateAttachment() + { + $message = new Message(); + $message->setTo($this->testEmailReceiver); + $message->setFrom('someuser@somedomain.com'); + $message->setSubject('Yii Swift Create Attachment Test'); + $message->setText('Yii Swift Create Attachment Test body'); + $message->createAttachment('Test attachment content', 'test.txt'); + $this->assertTrue($message->send()); + } + + /** + * @depends testSend + */ + public function testSendAlternativeBody() + { + $message = new Message(); + $message->setTo($this->testEmailReceiver); + $message->setFrom('someuser@somedomain.com'); + $message->setSubject('Yii Swift Alternative Body Test'); + $message->addHtml('Yii Swift test HTML body'); + $message->addText('Yii Swift test plain text body'); + $this->assertTrue($message->send()); + } } \ No newline at end of file