From 2a326c7d7b6ef44889613fbdc09cb1e5fe64f6d1 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Fri, 25 Oct 2013 13:15:36 +0300 Subject: [PATCH] 'email' renamed to 'mail'. 'SwiftMailer' recomposed into extension. --- extensions/swiftmailer/LICENSE.md | 32 ++++ extensions/swiftmailer/README.md | 58 +++++++ extensions/swiftmailer/composer.json | 28 +++ extensions/swiftmailer/yii/swiftmailer/Mailer.php | 143 +++++++++++++++ extensions/swiftmailer/yii/swiftmailer/Message.php | 126 ++++++++++++++ framework/yii/email/BaseMailer.php | 176 ------------------- framework/yii/email/BaseMessage.php | 88 ---------- framework/yii/email/Message.php | 45 ----- framework/yii/email/MessageInterface.php | 116 ------------- framework/yii/email/ViewResolver.php | 57 ------ framework/yii/email/swift/Mailer.php | 180 ------------------- framework/yii/email/swift/Message.php | 126 -------------- framework/yii/email/swift/autoload.php | 9 - framework/yii/mail/BaseMailer.php | 176 +++++++++++++++++++ framework/yii/mail/BaseMessage.php | 88 ++++++++++ framework/yii/mail/Message.php | 45 +++++ framework/yii/mail/MessageInterface.php | 116 +++++++++++++ framework/yii/mail/ViewResolver.php | 57 ++++++ tests/unit/extensions/swiftmailer/MailerTest.php | 74 ++++++++ tests/unit/extensions/swiftmailer/MessageTest.php | 100 +++++++++++ tests/unit/framework/email/BaseMailerTest.php | 191 --------------------- tests/unit/framework/email/ViewResolverTest.php | 57 ------ tests/unit/framework/email/swift/MailerTest.php | 74 -------- tests/unit/framework/email/swift/MessageTest.php | 100 ----------- tests/unit/framework/mail/BaseMailerTest.php | 191 +++++++++++++++++++++ tests/unit/framework/mail/ViewResolverTest.php | 61 +++++++ 26 files changed, 1295 insertions(+), 1219 deletions(-) create mode 100644 extensions/swiftmailer/LICENSE.md create mode 100644 extensions/swiftmailer/README.md create mode 100644 extensions/swiftmailer/composer.json create mode 100644 extensions/swiftmailer/yii/swiftmailer/Mailer.php create mode 100644 extensions/swiftmailer/yii/swiftmailer/Message.php delete mode 100644 framework/yii/email/BaseMailer.php delete mode 100644 framework/yii/email/BaseMessage.php delete mode 100644 framework/yii/email/Message.php delete mode 100644 framework/yii/email/MessageInterface.php delete mode 100644 framework/yii/email/ViewResolver.php delete mode 100644 framework/yii/email/swift/Mailer.php delete mode 100644 framework/yii/email/swift/Message.php delete mode 100644 framework/yii/email/swift/autoload.php create mode 100644 framework/yii/mail/BaseMailer.php create mode 100644 framework/yii/mail/BaseMessage.php create mode 100644 framework/yii/mail/Message.php create mode 100644 framework/yii/mail/MessageInterface.php create mode 100644 framework/yii/mail/ViewResolver.php create mode 100644 tests/unit/extensions/swiftmailer/MailerTest.php create mode 100644 tests/unit/extensions/swiftmailer/MessageTest.php delete mode 100644 tests/unit/framework/email/BaseMailerTest.php delete mode 100644 tests/unit/framework/email/ViewResolverTest.php delete mode 100644 tests/unit/framework/email/swift/MailerTest.php delete mode 100644 tests/unit/framework/email/swift/MessageTest.php create mode 100644 tests/unit/framework/mail/BaseMailerTest.php create mode 100644 tests/unit/framework/mail/ViewResolverTest.php diff --git a/extensions/swiftmailer/LICENSE.md b/extensions/swiftmailer/LICENSE.md new file mode 100644 index 0000000..0bb1a8d --- /dev/null +++ b/extensions/swiftmailer/LICENSE.md @@ -0,0 +1,32 @@ +The Yii framework is free software. It is released under the terms of +the following BSD License. + +Copyright © 2008-2013 by Yii Software LLC (http://www.yiisoft.com) +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + * Neither the name of Yii Software LLC nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/extensions/swiftmailer/README.md b/extensions/swiftmailer/README.md new file mode 100644 index 0000000..bb04a83 --- /dev/null +++ b/extensions/swiftmailer/README.md @@ -0,0 +1,58 @@ +Yii 2.0 Public Preview - SwiftMailer Mail Solution +================================================== + +Thank you for choosing Yii - a high-performance component-based PHP framework. + +If you are looking for a production-ready PHP framework, please use +[Yii v1.1](https://github.com/yiisoft/yii). + +Yii 2.0 is still under heavy development. We may make significant changes +without prior notices. **Yii 2.0 is not ready for production use yet.** + +[![Build Status](https://secure.travis-ci.org/yiisoft/yii2.png)](http://travis-ci.org/yiisoft/yii2) + +This is the yii2-swiftmailer extension. + + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run +``` +php composer.phar require yiisoft/yii2-swiftmailer "*" +``` + +or add +```json +"yiisoft/yii2-swiftmailer": "*" +``` +to the require section of your composer.json. + + +*Note: You might have to run `php composer.phar selfupdate`* + + +Usage & Documentation +--------------------- + +This extension has to be registered prior to usage. +To enable this view renderer add it to the $rendereres property of your view object. + +Example: + +```php + [ + 'mail' => [ + 'class' => 'yii\swiftmailer\Mailer', + ], + ], +]; +``` + +For further instructions refer to the related section in the yii guide. diff --git a/extensions/swiftmailer/composer.json b/extensions/swiftmailer/composer.json new file mode 100644 index 0000000..2aa47ae --- /dev/null +++ b/extensions/swiftmailer/composer.json @@ -0,0 +1,28 @@ +{ + "name": "yiisoft/yii2-swiftmailer", + "description": "The SwiftMailer integration for the Yii framework", + "keywords": ["yii", "swift", "swiftmailer", "mail", "email", "mailer"], + "type": "yii2-extension", + "license": "BSD-3-Clause", + "support": { + "issues": "https://github.com/yiisoft/yii2/issues?state=open", + "forum": "http://www.yiiframework.com/forum/", + "wiki": "http://www.yiiframework.com/wiki/", + "irc": "irc://irc.freenode.net/yii", + "source": "https://github.com/yiisoft/yii2" + }, + "authors": [ + { + "name": "Paul Klimov", + "email": "klimov.paul@gmail.com" + } + ], + "minimum-stability": "dev", + "require": { + "yiisoft/yii2": "*", + "swiftmailer/swiftmailer": ">=v5.0" + }, + "autoload": { + "psr-0": { "yii\\swiftmailer\\": "" } + } +} diff --git a/extensions/swiftmailer/yii/swiftmailer/Mailer.php b/extensions/swiftmailer/yii/swiftmailer/Mailer.php new file mode 100644 index 0000000..2f8bbe7 --- /dev/null +++ b/extensions/swiftmailer/yii/swiftmailer/Mailer.php @@ -0,0 +1,143 @@ + array( + * ... + * 'email' => array( + * 'class' => 'yii\email\swift\Mailer', + * 'transport' => [ + * 'class' => 'Swift_SmtpTransport', + * 'host' => 'localhost', + * 'username' => 'username', + * 'password' => 'password', + * 'port' => '587', + * 'encryption' => 'tls', + * ], + * ), + * ... + * ), + * ~~~ + * + * @see http://swiftmailer.org + * + * @author Paul Klimov + * @since 2.0 + */ +class Mailer extends BaseMailer +{ + /** + * @var \Swift_Mailer Swift mailer instance. + */ + private $_swiftMailer; + /** + * @var \Swift_Transport|array Swift transport instance or its array configuration. + */ + private $_transport = []; + + /** + * @return array|\Swift_Mailer Swift mailer instance or array configuration. + */ + public function getSwiftMailer() + { + if (!is_object($this->_swiftMailer)) { + $this->_swiftMailer = $this->createSwiftMailer(); + } + return $this->_swiftMailer; + } + + /** + * @param array|\Swift_Transport $transport + * @throws \yii\base\InvalidConfigException on invalid argument. + */ + public function setTransport($transport) + { + if (!is_array($transport) && !is_object($transport)) { + throw new InvalidConfigException('"' . get_class($this) . '::transport" should be either object or array, "' . gettype($transport) . '" given.'); + } + $this->_transport = $transport; + } + + /** + * @return array|\Swift_Transport + */ + public function getTransport() + { + if (!is_object($this->_transport)) { + $this->_transport = $this->createTransport($this->_transport); + } + return $this->_transport; + } + + /** + * @inheritdoc + */ + public function send($message) + { + return ($this->getSwiftMailer()->send($message->getSwiftMessage()) > 0); + } + + /** + * Creates Swift mailer instance. + * @return \Swift_Mailer mailer instance. + */ + protected function createSwiftMailer() + { + return \Swift_Mailer::newInstance($this->getTransport()); + } + + /** + * Creates email transport instance by its array configuration. + * @param array $config transport configuration. + * @throws \yii\base\InvalidConfigException on invalid transport configuration. + * @return \Swift_Transport transport instance. + */ + protected function createTransport(array $config) + { + if (array_key_exists('class', $config)) { + $className = $config['class']; + unset($config['class']); + } else { + $className = 'Swift_MailTransport'; + } + $transport = call_user_func([$className, 'newInstance']); + if (!empty($config)) { + foreach ($config as $name => $value) { + if (property_exists($transport, $name)) { + $transport->$name = $value; + } else { + $setter = 'set' . $name; + if (method_exists($transport, $setter)) { + $transport->$setter($value); + } else { + throw new InvalidConfigException('Setting unknown property: ' . get_class($transport) . '::' . $name); + } + } + } + } + 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 diff --git a/extensions/swiftmailer/yii/swiftmailer/Message.php b/extensions/swiftmailer/yii/swiftmailer/Message.php new file mode 100644 index 0000000..e850208 --- /dev/null +++ b/extensions/swiftmailer/yii/swiftmailer/Message.php @@ -0,0 +1,126 @@ + + * @since 2.0 + */ +class Message extends BaseMessage +{ + /** + * @var \Swift_Message Swift message instance. + */ + private $_swiftMessage; + + /** + * @return \Swift_Message Swift message instance. + */ + public function getSwiftMessage() + { + if (!is_object($this->_swiftMessage)) { + $this->_swiftMessage = $this->getMailer()->createSwiftMessage(); + } + return $this->_swiftMessage; + } + + /** + * @inheritdoc + */ + public function setFrom($from) + { + $this->getSwiftMessage()->setFrom($from); + $this->getSwiftMessage()->setReplyTo($from); + } + + /** + * @inheritdoc + */ + public function setTo($to) + { + $this->getSwiftMessage()->setTo($to); + } + + /** + * @inheritdoc + */ + public function setCc($cc) + { + $this->getSwiftMessage()->setCc($cc); + } + + /** + * @inheritdoc + */ + public function setBcc($bcc) + { + $this->getSwiftMessage()->setBcc($bcc); + } + + /** + * @inheritdoc + */ + public function setSubject($subject) + { + $this->getSwiftMessage()->setSubject($subject); + } + + /** + * @inheritdoc + */ + public function setText($text) + { + $this->getSwiftMessage()->setBody($text, 'text/plain'); + } + + /** + * @inheritdoc + */ + public function setHtml($html) + { + $this->getSwiftMessage()->setBody($html, 'text/html'); + } + + /** + * @inheritdoc + */ + public function addText($text) + { + $this->getSwiftMessage()->addPart($text, 'text/plain'); + } + + /** + * @inheritdoc + */ + public function addHtml($html) + { + $this->getSwiftMessage()->addPart($html, 'text/html'); + } + + /** + * @inheritdoc + */ + public function createAttachment($content, $fileName, $contentType = 'application/octet-stream') + { + if (empty($contentType)) { + $contentType = 'application/octet-stream'; + } + $attachment = \Swift_Attachment::newInstance($content, $fileName, $contentType); + $this->getSwiftMessage()->attach($attachment); + } +} \ No newline at end of file diff --git a/framework/yii/email/BaseMailer.php b/framework/yii/email/BaseMailer.php deleted file mode 100644 index b3b40b3..0000000 --- a/framework/yii/email/BaseMailer.php +++ /dev/null @@ -1,176 +0,0 @@ - - * @since 2.0 - */ -abstract class BaseMailer extends Component -{ - /** - * @var \yii\base\View|array view instance or its array configuration. - */ - private $_view = []; - /** - * @var \yii\email\ViewResolver|array view resolver instance or its array configuration. - */ - private $_viewResolver = []; - /** - * @var array configuration, which should be applied by default to any new created - * email message instance. - * For example: - * ~~~ - * array( - * 'encoding' => 'UTF-8', - * 'from' => 'noreply@mydomain.com', - * 'bcc' => 'email.test@mydomain.com', - * ) - * ~~~ - */ - private $_defaultMessageConfig = []; - - /** - * @param array|\yii\base\View $view view instance or its array configuration. - * @throws \yii\base\InvalidConfigException on invalid argument. - */ - public function setView($view) - { - if (!is_array($view) && !is_object($view)) { - throw new InvalidConfigException('"' . get_class($this) . '::view" should be either object or array, "' . gettype($view) . '" given.'); - } - $this->_view = $view; - } - - /** - * @return \yii\base\View view instance. - */ - public function getView() - { - if (!is_object($this->_view)) { - $this->_view = $this->createView($this->_view); - } - return $this->_view; - } - - /** - * @param array|\yii\email\ViewResolver $viewResolver view resolver instance or its array configuration. - * @throws \yii\base\InvalidConfigException on invalid argument. - */ - public function setViewResolver($viewResolver) - { - if (!is_array($viewResolver) && !is_object($viewResolver)) { - throw new InvalidConfigException('"' . get_class($this) . '::viewResolver" should be either object or array, "' . gettype($viewResolver) . '" given.'); - } - $this->_viewResolver = $viewResolver; - } - - /** - * @return \yii\email\ViewResolver view resolver. - */ - public function getViewResolver() - { - if (!is_object($this->_viewResolver)) { - $this->_viewResolver = $this->createViewResolver($this->_viewResolver); - } - return $this->_viewResolver; - } - - /** - * @param array $defaultMessageConfig default message config - */ - public function setDefaultMessageConfig(array $defaultMessageConfig) - { - $this->_defaultMessageConfig = $defaultMessageConfig; - } - - /** - * @return array default message config - */ - public function getDefaultMessageConfig() - { - return $this->_defaultMessageConfig; - } - - /** - * Creates view instance from given configuration. - * @param array $config view configuration. - * @return \yii\base\View view instance. - */ - protected function createView(array $config) - { - if (!array_key_exists('class', $config)) { - $config['class'] = '\yii\base\View'; - } - return Yii::createObject($config); - } - - /** - * Creates view resolver instance from given configuration. - * @param array $config view resolver configuration. - * @return \yii\email\ViewResolver view resolver instance. - */ - protected function createViewResolver(array $config) - { - if (!array_key_exists('class', $config)) { - $config['class'] = '\yii\email\ViewResolver'; - } - return Yii::createObject($config); - } - - /** - * Sends the given email message. - * @param object $message email message instance - * @return boolean whether the message has been sent. - */ - abstract 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, - * 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 - */ - public function sendMultiple(array $messages) { - $successCount = 0; - foreach ($messages as $message) { - if ($this->send($message)) { - $successCount++; - } - } - return $successCount; - } - - /** - * Renders a view. - * @param string $view the view name or the path alias of the view file. - * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. - * @return string string the rendering result - */ - public function render($view, $params = []) - { - return $this->getView()->renderFile($this->getViewResolver()->findViewFile($view), $params, $this); - } -} \ No newline at end of file diff --git a/framework/yii/email/BaseMessage.php b/framework/yii/email/BaseMessage.php deleted file mode 100644 index aca71ee..0000000 --- a/framework/yii/email/BaseMessage.php +++ /dev/null @@ -1,88 +0,0 @@ - - * @since 2.0 - */ -abstract class BaseMessage extends Object implements MessageInterface -{ - /** - * @return \yii\email\BaseMailer - */ - 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()->getDefaultMessageConfig()); - } - - /** - * @inheritdoc - */ - public function send() - { - return $this->getMailer()->send($this); - } - - /** - * @inheritdoc - */ - public function attachFile($fileName, $contentType = null, $attachFileName = null) - { - if (!file_exists($fileName)) { - throw new InvalidParamException('Unable to attach file "' . $fileName . '": file does not exists!'); - } - if (empty($contentType)) { - $contentType = FileHelper::getMimeType($fileName); - } - if (empty($attachFileName)) { - $attachFileName = basename($fileName); - } - $content = file_get_contents($fileName); - $this->createAttachment($content, $attachFileName, $contentType); - } - - /** - * @inheritdoc - */ - public function render($view, $params = []) - { - return $this->getMailer()->render($view, $params); - } -} \ No newline at end of file diff --git a/framework/yii/email/Message.php b/framework/yii/email/Message.php deleted file mode 100644 index e8a14ab..0000000 --- a/framework/yii/email/Message.php +++ /dev/null @@ -1,45 +0,0 @@ -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\email\Message'] = '/path/to/my/email/Message.php' - * ~~~ - * - * @author Paul Klimov - * @since 2.0 - */ -class Message extends SwiftMessage {} \ No newline at end of file diff --git a/framework/yii/email/MessageInterface.php b/framework/yii/email/MessageInterface.php deleted file mode 100644 index c94d2ab..0000000 --- a/framework/yii/email/MessageInterface.php +++ /dev/null @@ -1,116 +0,0 @@ - - * @since 2.0 - */ -interface MessageInterface -{ - /** - * Sets message sender. - * @param string|array $from sender email address. - * You may pass an array of addresses if this message is from multiple people. - * You may also specify sender name in addition to email address using format: - * [email => name]. - */ - public function setFrom($from); - - /** - * Sets message receiver. - * @param string|array $to receiver email address. - * You may pass an array of addresses if multiple recipients should receive this message. - * You may also specify receiver name in addition to email address using format: - * [email => name]. - */ - public function setTo($to); - - /** - * Set the Cc (additional copy receiver) addresses of this message. - * @param string|array $cc copy receiver email address. - * You may pass an array of addresses if multiple recipients should receive this message. - * You may also specify receiver name in addition to email address using format: - * [email => name]. - */ - public function setCc($cc); - - /** - * Set the Bcc (hidden copy receiver) addresses of this message. - * @param string|array $bcc hidden copy receiver email address. - * You may pass an array of addresses if multiple recipients should receive this message. - * You may also specify receiver name in addition to email address using format: - * [email => name]. - */ - public function setBcc($bcc); - - /** - * Sets message subject. - * @param string $subject message subject - */ - public function setSubject($subject); - - /** - * Sets message plain text content. - * @param string $text message plain text content. - */ - public function setText($text); - - /** - * Sets message HTML content. - * @param string $html message HTML content. - */ - public function setHtml($html); - - /** - * Add message plain text content part. - * @param string $text message plain text content. - */ - public function addText($text); - - /** - * Add message HTML content part. - * @param string $html message HTML content. - */ - public function addHtml($html); - - /** - * Create file attachment for the email message. - * @param string $content attachment file content. - * @param string $fileName attachment file name. - * @param string $contentType MIME type of the attachment file, by default 'application/octet-stream' will be used. - */ - public function createAttachment($content, $fileName, $contentType = 'application/octet-stream'); - - /** - * Attaches existing file to the email message. - * @param string $fileName full file name - * @param string $contentType MIME type of the attachment file, if empty it will be suggested automatically. - * @param string $attachFileName name, which should be used for attachment, if empty file base name will be used. - */ - public function attachFile($fileName, $contentType = null, $attachFileName = null); - - /** - * Sends this email message. - * @return boolean success. - */ - public function send(); - - /** - * Renders a view. - * The view to be rendered can be specified in one of the following formats: - * - path alias (e.g. "@app/emails/contact/body"); - * - relative path (e.g. "contact"): the actual view file will be resolved by [[resolveView]]. - * @param string $view the view name or the path alias of the view file. - * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. - * @return string string the rendering result - */ - public function render($view, $params = []); -} \ No newline at end of file diff --git a/framework/yii/email/ViewResolver.php b/framework/yii/email/ViewResolver.php deleted file mode 100644 index 4f522be..0000000 --- a/framework/yii/email/ViewResolver.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @since 2.0 - */ -class ViewResolver extends Component -{ - /** - * @var string directory containing view files for this email messages. - */ - public $viewPath = '@app/emails'; - - /** - * Finds the view file based on the given view name. - * The view to be rendered can be specified in one of the following formats: - * - path alias (e.g. "@app/emails/contact/body"); - * - relative path (e.g. "contact"): the actual view file will be resolved by [[resolveView]]. - * @param string $view the view name or the path alias of the view file. - * @return string the view file path. Note that the file may not exist. - */ - public function findViewFile($view) - { - if (strncmp($view, '@', 1) === 0) { - // e.g. "@app/views/main" - $file = Yii::getAlias($view); - } else { - $file = $this->resolveView($view); - } - return pathinfo($file, PATHINFO_EXTENSION) === '' ? $file . '.php' : $file; - } - - /** - * Composes file name for the view name, appending view name to [[viewPath]]. - * Child classes may override this method to provide more sophisticated - * search of the view files or even composition of the view files "on the fly". - * @param string $view the view name. - * @return string the view file path. - */ - protected function resolveView($view) - { - return Yii::getAlias($this->viewPath) . DIRECTORY_SEPARATOR . $view; - } -} \ No newline at end of file diff --git a/framework/yii/email/swift/Mailer.php b/framework/yii/email/swift/Mailer.php deleted file mode 100644 index 1f589a2..0000000 --- a/framework/yii/email/swift/Mailer.php +++ /dev/null @@ -1,180 +0,0 @@ - array( - * ... - * 'email' => array( - * 'class' => 'yii\email\swift\Mailer', - * 'transport' => [ - * 'class' => 'Swift_SmtpTransport', - * 'host' => 'localhost', - * 'username' => 'username', - * 'password' => 'password', - * 'port' => '587', - * 'encryption' => 'tls', - * ], - * ), - * ... - * ), - * ~~~ - * - * @see http://swiftmailer.org - * - * @author Paul Klimov - * @since 2.0 - */ -class Mailer extends BaseMailer -{ - /** - * @var string|callback SwiftMailer autoloader callback or path to autoloader file. - * If the SwiftMailer classes autoloading is already managed in some other place, - * for example via Composer, you should leave this field blank. - */ - public $autoload; - /** - * @var \Swift_Mailer Swift mailer instance. - */ - private $_swiftMailer; - /** - * @var \Swift_Transport|array Swift transport instance or its array configuration. - */ - private $_transport = []; - - /** - * @return array|\Swift_Mailer Swift mailer instance or array configuration. - */ - public function getSwiftMailer() - { - if (!is_object($this->_swiftMailer)) { - $this->_swiftMailer = $this->createSwiftMailer(); - } - return $this->_swiftMailer; - } - - /** - * @param array|\Swift_Transport $transport - * @throws \yii\base\InvalidConfigException on invalid argument. - */ - public function setTransport($transport) - { - if (!is_array($transport) && !is_object($transport)) { - throw new InvalidConfigException('"' . get_class($this) . '::transport" should be either object or array, "' . gettype($transport) . '" given.'); - } - $this->_transport = $transport; - } - - /** - * @return array|\Swift_Transport - */ - public function getTransport() - { - if (!is_object($this->_transport)) { - $this->_transport = $this->createTransport($this->_transport); - } - return $this->_transport; - } - - /** - * @inheritdoc - */ - public function init() - { - $this->setupSwiftMailerAutoload(); - } - - /** - * Sets up the SwiftMailer autoloader, if it is specified. - */ - protected function setupSwiftMailerAutoload() - { - if (!class_exists('Swift', false)) { - if (empty($this->autoload)) { - $this->autoload = __DIR__ . '/autoload.php'; - } - if (is_string($this->autoload)) { - if (file_exists($this->autoload)) { - require_once($this->autoload); - } elseif (function_exists($this->autoload)) { - spl_autoload_register($this->autoload); - } else { - throw new InvalidConfigException('"' . get_class($this) . '::autoload" value "' . $this->autoload . '" is invalid: no such function or file exists.'); - } - } else { - spl_autoload_register($this->autoload); - } - } - } - - /** - * @inheritdoc - */ - public function send($message) - { - return ($this->getSwiftMailer()->send($message->getSwiftMessage()) > 0); - } - - /** - * Creates Swift mailer instance. - * @return \Swift_Mailer mailer instance. - */ - protected function createSwiftMailer() - { - return \Swift_Mailer::newInstance($this->getTransport()); - } - - /** - * Creates email transport instance by its array configuration. - * @param array $config transport configuration. - * @throws \yii\base\InvalidConfigException on invalid transport configuration. - * @return \Swift_Transport transport instance. - */ - protected function createTransport(array $config) - { - if (array_key_exists('class', $config)) { - $className = $config['class']; - unset($config['class']); - } else { - $className = 'Swift_MailTransport'; - } - $transport = call_user_func([$className, 'newInstance']); - if (!empty($config)) { - foreach ($config as $name => $value) { - if (property_exists($transport, $name)) { - $transport->$name = $value; - } else { - $setter = 'set' . $name; - if (method_exists($transport, $setter)) { - $transport->$setter($value); - } else { - throw new InvalidConfigException('Setting unknown property: ' . get_class($transport) . '::' . $name); - } - } - } - } - 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 diff --git a/framework/yii/email/swift/Message.php b/framework/yii/email/swift/Message.php deleted file mode 100644 index 04ef7b5..0000000 --- a/framework/yii/email/swift/Message.php +++ /dev/null @@ -1,126 +0,0 @@ - - * @since 2.0 - */ -class Message extends BaseMessage -{ - /** - * @var \Swift_Message Swift message instance. - */ - private $_swiftMessage; - - /** - * @return \Swift_Message Swift message instance. - */ - public function getSwiftMessage() - { - if (!is_object($this->_swiftMessage)) { - $this->_swiftMessage = $this->getMailer()->createSwiftMessage(); - } - return $this->_swiftMessage; - } - - /** - * @inheritdoc - */ - public function setFrom($from) - { - $this->getSwiftMessage()->setFrom($from); - $this->getSwiftMessage()->setReplyTo($from); - } - - /** - * @inheritdoc - */ - public function setTo($to) - { - $this->getSwiftMessage()->setTo($to); - } - - /** - * @inheritdoc - */ - public function setCc($cc) - { - $this->getSwiftMessage()->setCc($cc); - } - - /** - * @inheritdoc - */ - public function setBcc($bcc) - { - $this->getSwiftMessage()->setBcc($bcc); - } - - /** - * @inheritdoc - */ - public function setSubject($subject) - { - $this->getSwiftMessage()->setSubject($subject); - } - - /** - * @inheritdoc - */ - public function setText($text) - { - $this->getSwiftMessage()->setBody($text, 'text/plain'); - } - - /** - * @inheritdoc - */ - public function setHtml($html) - { - $this->getSwiftMessage()->setBody($html, 'text/html'); - } - - /** - * @inheritdoc - */ - public function addText($text) - { - $this->getSwiftMessage()->addPart($text, 'text/plain'); - } - - /** - * @inheritdoc - */ - public function addHtml($html) - { - $this->getSwiftMessage()->addPart($html, 'text/html'); - } - - /** - * @inheritdoc - */ - public function createAttachment($content, $fileName, $contentType = 'application/octet-stream') - { - if (empty($contentType)) { - $contentType = 'application/octet-stream'; - } - $attachment = \Swift_Attachment::newInstance($content, $fileName, $contentType); - $this->getSwiftMessage()->attach($attachment); - } -} \ No newline at end of file diff --git a/framework/yii/email/swift/autoload.php b/framework/yii/email/swift/autoload.php deleted file mode 100644 index 494150f..0000000 --- a/framework/yii/email/swift/autoload.php +++ /dev/null @@ -1,9 +0,0 @@ - + * @since 2.0 + */ +abstract class BaseMailer extends Component +{ + /** + * @var \yii\base\View|array view instance or its array configuration. + */ + private $_view = []; + /** + * @var \yii\mail\ViewResolver|array view resolver instance or its array configuration. + */ + private $_viewResolver = []; + /** + * @var array configuration, which should be applied by default to any new created + * email message instance. + * For example: + * ~~~ + * array( + * 'encoding' => 'UTF-8', + * 'from' => 'noreply@mydomain.com', + * 'bcc' => 'email.test@mydomain.com', + * ) + * ~~~ + */ + private $_defaultMessageConfig = []; + + /** + * @param array|\yii\base\View $view view instance or its array configuration. + * @throws \yii\base\InvalidConfigException on invalid argument. + */ + public function setView($view) + { + if (!is_array($view) && !is_object($view)) { + throw new InvalidConfigException('"' . get_class($this) . '::view" should be either object or array, "' . gettype($view) . '" given.'); + } + $this->_view = $view; + } + + /** + * @return \yii\base\View view instance. + */ + public function getView() + { + if (!is_object($this->_view)) { + $this->_view = $this->createView($this->_view); + } + return $this->_view; + } + + /** + * @param array|\yii\mail\ViewResolver $viewResolver view resolver instance or its array configuration. + * @throws \yii\base\InvalidConfigException on invalid argument. + */ + public function setViewResolver($viewResolver) + { + if (!is_array($viewResolver) && !is_object($viewResolver)) { + throw new InvalidConfigException('"' . get_class($this) . '::viewResolver" should be either object or array, "' . gettype($viewResolver) . '" given.'); + } + $this->_viewResolver = $viewResolver; + } + + /** + * @return \yii\mail\ViewResolver view resolver. + */ + public function getViewResolver() + { + if (!is_object($this->_viewResolver)) { + $this->_viewResolver = $this->createViewResolver($this->_viewResolver); + } + return $this->_viewResolver; + } + + /** + * @param array $defaultMessageConfig default message config + */ + public function setDefaultMessageConfig(array $defaultMessageConfig) + { + $this->_defaultMessageConfig = $defaultMessageConfig; + } + + /** + * @return array default message config + */ + public function getDefaultMessageConfig() + { + return $this->_defaultMessageConfig; + } + + /** + * Creates view instance from given configuration. + * @param array $config view configuration. + * @return \yii\base\View view instance. + */ + protected function createView(array $config) + { + if (!array_key_exists('class', $config)) { + $config['class'] = '\yii\base\View'; + } + return Yii::createObject($config); + } + + /** + * Creates view resolver instance from given configuration. + * @param array $config view resolver configuration. + * @return \yii\mail\ViewResolver view resolver instance. + */ + protected function createViewResolver(array $config) + { + if (!array_key_exists('class', $config)) { + $config['class'] = '\yii\mail\ViewResolver'; + } + return Yii::createObject($config); + } + + /** + * Sends the given email message. + * @param object $message email message instance + * @return boolean whether the message has been sent. + */ + abstract 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, + * 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 + */ + public function sendMultiple(array $messages) { + $successCount = 0; + foreach ($messages as $message) { + if ($this->send($message)) { + $successCount++; + } + } + return $successCount; + } + + /** + * Renders a view. + * @param string $view the view name or the path alias of the view file. + * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. + * @return string string the rendering result + */ + public function render($view, $params = []) + { + return $this->getView()->renderFile($this->getViewResolver()->findViewFile($view), $params, $this); + } +} \ No newline at end of file diff --git a/framework/yii/mail/BaseMessage.php b/framework/yii/mail/BaseMessage.php new file mode 100644 index 0000000..17658f4 --- /dev/null +++ b/framework/yii/mail/BaseMessage.php @@ -0,0 +1,88 @@ + + * @since 2.0 + */ +abstract class BaseMessage extends Object implements MessageInterface +{ + /** + * @return \yii\mail\BaseMailer + */ + 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()->getDefaultMessageConfig()); + } + + /** + * @inheritdoc + */ + public function send() + { + return $this->getMailer()->send($this); + } + + /** + * @inheritdoc + */ + public function attachFile($fileName, $contentType = null, $attachFileName = null) + { + if (!file_exists($fileName)) { + throw new InvalidParamException('Unable to attach file "' . $fileName . '": file does not exists!'); + } + if (empty($contentType)) { + $contentType = FileHelper::getMimeType($fileName); + } + if (empty($attachFileName)) { + $attachFileName = basename($fileName); + } + $content = file_get_contents($fileName); + $this->createAttachment($content, $attachFileName, $contentType); + } + + /** + * @inheritdoc + */ + public function render($view, $params = []) + { + return $this->getMailer()->render($view, $params); + } +} \ No newline at end of file diff --git a/framework/yii/mail/Message.php b/framework/yii/mail/Message.php new file mode 100644 index 0000000..fb34e24 --- /dev/null +++ b/framework/yii/mail/Message.php @@ -0,0 +1,45 @@ +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 + * @since 2.0 + */ +class Message extends SwiftMessage {} \ No newline at end of file diff --git a/framework/yii/mail/MessageInterface.php b/framework/yii/mail/MessageInterface.php new file mode 100644 index 0000000..c592edb --- /dev/null +++ b/framework/yii/mail/MessageInterface.php @@ -0,0 +1,116 @@ + + * @since 2.0 + */ +interface MessageInterface +{ + /** + * Sets message sender. + * @param string|array $from sender email address. + * You may pass an array of addresses if this message is from multiple people. + * You may also specify sender name in addition to email address using format: + * [email => name]. + */ + public function setFrom($from); + + /** + * Sets message receiver. + * @param string|array $to receiver email address. + * You may pass an array of addresses if multiple recipients should receive this message. + * You may also specify receiver name in addition to email address using format: + * [email => name]. + */ + public function setTo($to); + + /** + * Set the Cc (additional copy receiver) addresses of this message. + * @param string|array $cc copy receiver email address. + * You may pass an array of addresses if multiple recipients should receive this message. + * You may also specify receiver name in addition to email address using format: + * [email => name]. + */ + public function setCc($cc); + + /** + * Set the Bcc (hidden copy receiver) addresses of this message. + * @param string|array $bcc hidden copy receiver email address. + * You may pass an array of addresses if multiple recipients should receive this message. + * You may also specify receiver name in addition to email address using format: + * [email => name]. + */ + public function setBcc($bcc); + + /** + * Sets message subject. + * @param string $subject message subject + */ + public function setSubject($subject); + + /** + * Sets message plain text content. + * @param string $text message plain text content. + */ + public function setText($text); + + /** + * Sets message HTML content. + * @param string $html message HTML content. + */ + public function setHtml($html); + + /** + * Add message plain text content part. + * @param string $text message plain text content. + */ + public function addText($text); + + /** + * Add message HTML content part. + * @param string $html message HTML content. + */ + public function addHtml($html); + + /** + * Create file attachment for the email message. + * @param string $content attachment file content. + * @param string $fileName attachment file name. + * @param string $contentType MIME type of the attachment file, by default 'application/octet-stream' will be used. + */ + public function createAttachment($content, $fileName, $contentType = 'application/octet-stream'); + + /** + * Attaches existing file to the email message. + * @param string $fileName full file name + * @param string $contentType MIME type of the attachment file, if empty it will be suggested automatically. + * @param string $attachFileName name, which should be used for attachment, if empty file base name will be used. + */ + public function attachFile($fileName, $contentType = null, $attachFileName = null); + + /** + * Sends this email message. + * @return boolean success. + */ + public function send(); + + /** + * Renders a view. + * The view to be rendered can be specified in one of the following formats: + * - path alias (e.g. "@app/emails/contact/body"); + * - relative path (e.g. "contact"): the actual view file will be resolved by [[resolveView]]. + * @param string $view the view name or the path alias of the view file. + * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. + * @return string string the rendering result + */ + public function render($view, $params = []); +} \ No newline at end of file diff --git a/framework/yii/mail/ViewResolver.php b/framework/yii/mail/ViewResolver.php new file mode 100644 index 0000000..31f57cb --- /dev/null +++ b/framework/yii/mail/ViewResolver.php @@ -0,0 +1,57 @@ + + * @since 2.0 + */ +class ViewResolver extends Component +{ + /** + * @var string directory containing view files for this email messages. + */ + public $viewPath = '@app/emails'; + + /** + * Finds the view file based on the given view name. + * The view to be rendered can be specified in one of the following formats: + * - path alias (e.g. "@app/emails/contact/body"); + * - relative path (e.g. "contact"): the actual view file will be resolved by [[resolveView]]. + * @param string $view the view name or the path alias of the view file. + * @return string the view file path. Note that the file may not exist. + */ + public function findViewFile($view) + { + if (strncmp($view, '@', 1) === 0) { + // e.g. "@app/views/main" + $file = Yii::getAlias($view); + } else { + $file = $this->resolveView($view); + } + return pathinfo($file, PATHINFO_EXTENSION) === '' ? $file . '.php' : $file; + } + + /** + * Composes file name for the view name, appending view name to [[viewPath]]. + * Child classes may override this method to provide more sophisticated + * search of the view files or even composition of the view files "on the fly". + * @param string $view the view name. + * @return string the view file path. + */ + protected function resolveView($view) + { + return Yii::getAlias($this->viewPath) . DIRECTORY_SEPARATOR . $view; + } +} \ No newline at end of file diff --git a/tests/unit/extensions/swiftmailer/MailerTest.php b/tests/unit/extensions/swiftmailer/MailerTest.php new file mode 100644 index 0000000..db5f298 --- /dev/null +++ b/tests/unit/extensions/swiftmailer/MailerTest.php @@ -0,0 +1,74 @@ +mockApplication(array( + 'vendorPath' => Yii::getAlias('@yiiunit/vendor') + )); + Yii::$app->setComponent('email', $this->createTestEmailComponent()); + } + + /** + * @return Mailer test email component instance. + */ + protected function createTestEmailComponent() + { + $component = new Mailer(); + return $component; + } + + // Tests : + + public function testSetupTransport() + { + $mailer = new Mailer(); + + $transport = \Swift_MailTransport::newInstance(); + $mailer->setTransport($transport); + $this->assertEquals($transport, $mailer->getTransport(), 'Unable to setup transport!'); + } + + /** + * @depends testSetupTransport + */ + public function testConfigureTransport() + { + $mailer = new Mailer(); + + $transportConfig = [ + 'class' => 'Swift_SmtpTransport', + 'host' => 'localhost', + ]; + $mailer->setTransport($transportConfig); + $transport = $mailer->getTransport(); + $this->assertTrue(is_object($transport), 'Unable to setup transport via config!'); + $this->assertEquals($transportConfig['class'], get_class($transport), 'Invalid transport class!'); + $this->assertEquals($transportConfig['host'], $transport->getHost(), 'Invalid transport host!'); + } + + public function testGetSwiftMailer() + { + $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 diff --git a/tests/unit/extensions/swiftmailer/MessageTest.php b/tests/unit/extensions/swiftmailer/MessageTest.php new file mode 100644 index 0000000..cabf22a --- /dev/null +++ b/tests/unit/extensions/swiftmailer/MessageTest.php @@ -0,0 +1,100 @@ +mockApplication(array( + 'vendorPath' => Yii::getAlias('@yiiunit/vendor') + )); + Yii::$app->setComponent('email', $this->createTestEmailComponent()); + } + + /** + * @return Mailer test email component instance. + */ + protected function createTestEmailComponent() + { + $component = new Mailer(); + return $component; + } + + // Tests : + + public function testGetSwiftMessage() + { + $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 diff --git a/tests/unit/framework/email/BaseMailerTest.php b/tests/unit/framework/email/BaseMailerTest.php deleted file mode 100644 index b75d628..0000000 --- a/tests/unit/framework/email/BaseMailerTest.php +++ /dev/null @@ -1,191 +0,0 @@ -mockApplication(); - Yii::$app->setComponent('email', $this->createTestEmailComponent()); - $filePath = $this->getTestFilePath(); - if (!file_exists($filePath)) { - FileHelper::createDirectory($filePath); - } - } - - public function tearDown() - { - $filePath = $this->getTestFilePath(); - if (file_exists($filePath)) { - FileHelper::removeDirectory($filePath); - } - } - - /** - * @return string test file path. - */ - protected function getTestFilePath() - { - return Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . basename(get_class($this)) . '_' . getmypid(); - } - - /** - * @return Mailer test email component instance. - */ - protected function createTestEmailComponent() - { - $component = new Mailer(); - return $component; - } - - // Tests : - - public function testSetupView() - { - $mailer = new Mailer(); - - $view = new View(); - $mailer->setView($view); - $this->assertEquals($view, $mailer->getView(), 'Unable to setup view!'); - - $viewConfig = [ - 'params' => [ - 'param1' => 'value1', - 'param2' => 'value2', - ] - ]; - $mailer->setView($viewConfig); - $view = $mailer->getView(); - $this->assertTrue(is_object($view), 'Unable to setup view via config!'); - $this->assertEquals($viewConfig['params'], $view->params, 'Unable to configure view via config array!'); - } - - /** - * @depends testSetupView - */ - public function testGetDefaultView() - { - $mailer = new Mailer(); - $view = $mailer->getView(); - $this->assertTrue(is_object($view), 'Unable to get default view!'); - } - - public function testSetupViewResolver() - { - $mailer = new Mailer(); - - $viewResolver = new ViewResolver(); - $mailer->setViewResolver($viewResolver); - $this->assertEquals($viewResolver, $mailer->getViewResolver(), 'Unable to setup view resolver!'); - - $viewResolverConfig = [ - 'viewPath' => '/test/view/path', - ]; - $mailer->setViewResolver($viewResolverConfig); - $viewResolver = $mailer->getViewResolver(); - $this->assertTrue(is_object($viewResolver), 'Unable to setup view resolver via config!'); - $this->assertEquals($viewResolverConfig['viewPath'], $viewResolver->viewPath, 'Unable to configure view resolver via config array!'); - } - - /** - * @depends testSetupViewResolver - */ - public function testGetDefaultViewResolver() - { - $mailer = new Mailer(); - $viewResolver = $mailer->getViewResolver(); - $this->assertTrue(is_object($viewResolver), 'Unable to get default view resolver!'); - } - - public function testDefaultMessageConfig() - { - $defaultMessageConfig = array( - 'id' => 'test-id', - 'encoding' => 'test-encoding', - ); - Yii::$app->getComponent('email')->setDefaultMessageConfig($defaultMessageConfig); - - $message = new Message(); - - foreach ($defaultMessageConfig as $name => $value) { - $this->assertEquals($value, $message->$name); - } - } - - /** - * @depends testGetDefaultView - * @depends testGetDefaultViewResolver - */ - public function testRender() - { - $mailer = new Mailer(); - - $filePath = $this->getTestFilePath(); - $mailer->getViewResolver()->viewPath = $filePath; - - $viewName = 'test_view'; - $fileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; - $fileContent = ''; - file_put_contents($fileName, $fileContent); - - $params = [ - 'testParam' => 'test output' - ]; - $renderResult = $mailer->render($viewName, $params); - $this->assertEquals($params['testParam'], $renderResult); - } -} - -/** - * Test Mailer class - */ -class Mailer extends BaseMailer -{ - public $sentMessages = array(); - - public function send($message) - { - $this->sentMessages[] = $message; - } -} - -/** - * Test Message class - */ -class Message extends BaseMessage -{ - public $id; - public $encoding; - - public function setFrom($from) {} - - public function setTo($to) {} - - public function setCc($cc) {} - - public function setBcc($bcc) {} - - public function setSubject($subject) {} - - public function setText($text) {} - - 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/ViewResolverTest.php b/tests/unit/framework/email/ViewResolverTest.php deleted file mode 100644 index 9185f72..0000000 --- a/tests/unit/framework/email/ViewResolverTest.php +++ /dev/null @@ -1,57 +0,0 @@ -testViewPath); - return [ - [ - $alias . '/test', - $aliasPath . '/test.php', - ], - [ - $alias . '/test.tpl', - $aliasPath . '/test.tpl', - ], - [ - 'contact/html', - $viewPath . '/contact/html.php', - ], - [ - 'contact/html.tpl', - $viewPath . '/contact/html.tpl', - ], - ]; - } - - /** - * @dataProvider dataProviderFindViewFile - * - * @param string $view - * @param string $expectedFileName - */ - public function testFindViewFile($view, $expectedFileName) - { - $viewResolver = new ViewResolver(); - $viewResolver->viewPath = $this->testViewPath; - $fileName = $viewResolver->findViewFile($view); - $this->assertEquals($expectedFileName, $fileName); - } -} \ No newline at end of file diff --git a/tests/unit/framework/email/swift/MailerTest.php b/tests/unit/framework/email/swift/MailerTest.php deleted file mode 100644 index f9cee72..0000000 --- a/tests/unit/framework/email/swift/MailerTest.php +++ /dev/null @@ -1,74 +0,0 @@ -mockApplication(array( - 'vendorPath' => Yii::getAlias('@yiiunit/vendor') - )); - Yii::$app->setComponent('email', $this->createTestEmailComponent()); - } - - /** - * @return Mailer test email component instance. - */ - protected function createTestEmailComponent() - { - $component = new Mailer(); - return $component; - } - - // Tests : - - public function testSetupTransport() - { - $mailer = new Mailer(); - - $transport = \Swift_MailTransport::newInstance(); - $mailer->setTransport($transport); - $this->assertEquals($transport, $mailer->getTransport(), 'Unable to setup transport!'); - } - - /** - * @depends testSetupTransport - */ - public function testConfigureTransport() - { - $mailer = new Mailer(); - - $transportConfig = [ - 'class' => 'Swift_SmtpTransport', - 'host' => 'localhost', - ]; - $mailer->setTransport($transportConfig); - $transport = $mailer->getTransport(); - $this->assertTrue(is_object($transport), 'Unable to setup transport via config!'); - $this->assertEquals($transportConfig['class'], get_class($transport), 'Invalid transport class!'); - $this->assertEquals($transportConfig['host'], $transport->getHost(), 'Invalid transport host!'); - } - - public function testGetSwiftMailer() - { - $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 diff --git a/tests/unit/framework/email/swift/MessageTest.php b/tests/unit/framework/email/swift/MessageTest.php deleted file mode 100644 index 112f4fc..0000000 --- a/tests/unit/framework/email/swift/MessageTest.php +++ /dev/null @@ -1,100 +0,0 @@ -mockApplication(array( - 'vendorPath' => Yii::getAlias('@yiiunit/vendor') - )); - Yii::$app->setComponent('email', $this->createTestEmailComponent()); - } - - /** - * @return Mailer test email component instance. - */ - protected function createTestEmailComponent() - { - $component = new Mailer(); - return $component; - } - - // Tests : - - public function testGetSwiftMessage() - { - $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 diff --git a/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php new file mode 100644 index 0000000..88d6a4e --- /dev/null +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -0,0 +1,191 @@ +mockApplication(); + Yii::$app->setComponent('email', $this->createTestEmailComponent()); + $filePath = $this->getTestFilePath(); + if (!file_exists($filePath)) { + FileHelper::createDirectory($filePath); + } + } + + public function tearDown() + { + $filePath = $this->getTestFilePath(); + if (file_exists($filePath)) { + FileHelper::removeDirectory($filePath); + } + } + + /** + * @return string test file path. + */ + protected function getTestFilePath() + { + return Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . basename(get_class($this)) . '_' . getmypid(); + } + + /** + * @return Mailer test email component instance. + */ + protected function createTestEmailComponent() + { + $component = new Mailer(); + return $component; + } + + // Tests : + + public function testSetupView() + { + $mailer = new Mailer(); + + $view = new View(); + $mailer->setView($view); + $this->assertEquals($view, $mailer->getView(), 'Unable to setup view!'); + + $viewConfig = [ + 'params' => [ + 'param1' => 'value1', + 'param2' => 'value2', + ] + ]; + $mailer->setView($viewConfig); + $view = $mailer->getView(); + $this->assertTrue(is_object($view), 'Unable to setup view via config!'); + $this->assertEquals($viewConfig['params'], $view->params, 'Unable to configure view via config array!'); + } + + /** + * @depends testSetupView + */ + public function testGetDefaultView() + { + $mailer = new Mailer(); + $view = $mailer->getView(); + $this->assertTrue(is_object($view), 'Unable to get default view!'); + } + + public function testSetupViewResolver() + { + $mailer = new Mailer(); + + $viewResolver = new ViewResolver(); + $mailer->setViewResolver($viewResolver); + $this->assertEquals($viewResolver, $mailer->getViewResolver(), 'Unable to setup view resolver!'); + + $viewResolverConfig = [ + 'viewPath' => '/test/view/path', + ]; + $mailer->setViewResolver($viewResolverConfig); + $viewResolver = $mailer->getViewResolver(); + $this->assertTrue(is_object($viewResolver), 'Unable to setup view resolver via config!'); + $this->assertEquals($viewResolverConfig['viewPath'], $viewResolver->viewPath, 'Unable to configure view resolver via config array!'); + } + + /** + * @depends testSetupViewResolver + */ + public function testGetDefaultViewResolver() + { + $mailer = new Mailer(); + $viewResolver = $mailer->getViewResolver(); + $this->assertTrue(is_object($viewResolver), 'Unable to get default view resolver!'); + } + + public function testDefaultMessageConfig() + { + $defaultMessageConfig = array( + 'id' => 'test-id', + 'encoding' => 'test-encoding', + ); + Yii::$app->getComponent('email')->setDefaultMessageConfig($defaultMessageConfig); + + $message = new Message(); + + foreach ($defaultMessageConfig as $name => $value) { + $this->assertEquals($value, $message->$name); + } + } + + /** + * @depends testGetDefaultView + * @depends testGetDefaultViewResolver + */ + public function testRender() + { + $mailer = new Mailer(); + + $filePath = $this->getTestFilePath(); + $mailer->getViewResolver()->viewPath = $filePath; + + $viewName = 'test_view'; + $fileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; + $fileContent = ''; + file_put_contents($fileName, $fileContent); + + $params = [ + 'testParam' => 'test output' + ]; + $renderResult = $mailer->render($viewName, $params); + $this->assertEquals($params['testParam'], $renderResult); + } +} + +/** + * Test Mailer class + */ +class Mailer extends BaseMailer +{ + public $sentMessages = array(); + + public function send($message) + { + $this->sentMessages[] = $message; + } +} + +/** + * Test Message class + */ +class Message extends BaseMessage +{ + public $id; + public $encoding; + + public function setFrom($from) {} + + public function setTo($to) {} + + public function setCc($cc) {} + + public function setBcc($bcc) {} + + public function setSubject($subject) {} + + public function setText($text) {} + + 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/mail/ViewResolverTest.php b/tests/unit/framework/mail/ViewResolverTest.php new file mode 100644 index 0000000..4ff4dbd --- /dev/null +++ b/tests/unit/framework/mail/ViewResolverTest.php @@ -0,0 +1,61 @@ +testViewPath); + return [ + [ + $alias . '/test', + $aliasPath . '/test.php', + ], + [ + $alias . '/test.tpl', + $aliasPath . '/test.tpl', + ], + [ + 'contact/html', + $viewPath . '/contact/html.php', + ], + [ + 'contact/html.tpl', + $viewPath . '/contact/html.tpl', + ], + ]; + } + + /** + * @dataProvider dataProviderFindViewFile + * + * @param string $view + * @param string $expectedFileName + */ + public function testFindViewFile($view, $expectedFileName) + { + $viewResolver = new ViewResolver(); + $viewResolver->viewPath = $this->testViewPath; + $fileName = $viewResolver->findViewFile($view); + $this->assertEquals($expectedFileName, $fileName); + } +} \ No newline at end of file