[TASK] Refactor t3lib_mail_Mailer and add unit tests
authorHelmut Hummel <helmut.hummel@typo3.org>
Tue, 8 May 2012 07:40:38 +0000 (09:40 +0200)
committerHelmut Hummel <helmut.hummel@typo3.org>
Tue, 8 May 2012 17:54:54 +0000 (19:54 +0200)
This change also cleans up some comments and the existing unit test.

Change-Id: Ifb80b7b6f70d06f6531656c579e673a0b0e5ad0b
Resolves: #36942
Releases: 6.0
Reviewed-on: http://review.typo3.org/11068
Reviewed-by: Tolleiv Nietsch
Tested-by: Tolleiv Nietsch
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
t3lib/mail/class.t3lib_mail_mailer.php
t3lib/mail/class.t3lib_mail_mboxtransport.php
tests/Unit/t3lib/mail/class.t3lib_mail_mailerTest.php [new file with mode: 0644]
tests/Unit/t3lib/mail/class.t3lib_mail_swiftmaileradapterTest.php

index d591ec6..4545e33 100644 (file)
@@ -47,15 +47,23 @@ class t3lib_mail_Mailer extends Swift_Mailer {
        protected $transport;
 
        /**
+        * @var array
+        */
+       protected $mailSettings = array();
+
+       /**
         * When constructing, also initializes the Swift_Transport like configured
         *
-        * @param Swift_Transport optionally pass a transport to the constructor. By default the configured transport from $TYPO3_CONF_VARS is used
+        * @param null|Swift_Transport $transport optionally pass a transport to the constructor. By default the configured transport from $TYPO3_CONF_VARS is used
         * @throws t3lib_exception
         */
        public function __construct(Swift_Transport $transport = NULL) {
                if ($transport !== NULL) {
                        $this->transport = $transport;
                } else {
+                       if (empty($this->mailSettings)) {
+                               $this->injectMailSettings();
+                       }
                        try {
                                $this->initializeTransport();
                        } catch (Exception $e) {
@@ -80,14 +88,13 @@ class t3lib_mail_Mailer extends Swift_Mailer {
         * $TYPO3_CONF_VARS['MAIL']['transport_sendmail_command'] = '/usr/sbin/sendmail -bs'
         *
         * @throws t3lib_exception
+        * @throws RuntimeException
         */
        private function initializeTransport() {
-               $mailSettings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
-               switch ($mailSettings['transport']) {
-
+               switch ($this->mailSettings['transport']) {
                        case 'smtp':
                                        // Get settings to be used when constructing the transport object
-                               list($host, $port) = preg_split('/:/', $mailSettings['transport_smtp_server']);
+                               list($host, $port) = preg_split('/:/', $this->mailSettings['transport_smtp_server']);
                                if ($host === '') {
                                        throw new t3lib_exception(
                                                '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"',
@@ -97,25 +104,25 @@ class t3lib_mail_Mailer extends Swift_Mailer {
                                if ($port === '') {
                                        $port = '25';
                                }
-                               $useEncryption = ($mailSettings['transport_smtp_encrypt'] ? $mailSettings['transport_smtp_encrypt'] : NULL);
+                               $useEncryption = ($this->mailSettings['transport_smtp_encrypt'] ? $this->mailSettings['transport_smtp_encrypt'] : NULL);
 
                                        // Create our transport
                                $this->transport = Swift_SmtpTransport::newInstance($host, $port, $useEncryption);
 
                                        // Need authentication?
-                               $username = $mailSettings['transport_smtp_username'];
+                               $username = $this->mailSettings['transport_smtp_username'];
                                if ($username !== '') {
                                        $this->transport->setUsername($username);
                                }
-                               $password = $mailSettings['transport_smtp_password'];
+                               $password = $this->mailSettings['transport_smtp_password'];
                                if ($password !== '') {
                                        $this->transport->setPassword($password);
                                }
                                break;
 
                        case 'sendmail':
-                               $sendmailCommand = $mailSettings['transport_sendmail_command'];
-                               if ($sendmailCommand === '') {
+                               $sendmailCommand = $this->mailSettings['transport_sendmail_command'];
+                               if (empty($sendmailCommand)) {
                                        throw new t3lib_exception(
                                                '$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"',
                                                1291068620
@@ -126,7 +133,7 @@ class t3lib_mail_Mailer extends Swift_Mailer {
                                break;
 
                        case 'mbox':
-                               $mboxFile = $mailSettings['transport_mbox_file'];
+                               $mboxFile = $this->mailSettings['transport_mbox_file'];
                                if ($mboxFile == '') {
                                        throw new t3lib_exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
                                }
@@ -140,12 +147,12 @@ class t3lib_mail_Mailer extends Swift_Mailer {
                                break;
                        default:
                                        // Custom mail transport
-                               $customTransport = t3lib_div::makeInstance($mailSettings['transport'], $mailSettings);
+                               $customTransport = t3lib_div::makeInstance($this->mailSettings['transport'], $this->mailSettings);
                                if ($customTransport instanceof Swift_Transport) {
                                        $this->transport = $customTransport;
                                } else {
                                        throw new RuntimeException(
-                                               $mailSettings['transport'] . ' is not an implementation of Swift_Transport,
+                                               $this->mailSettings['transport'] . ' is not an implementation of Swift_Transport,
                                                but must implement that interface to be used as a mail transport.',
                                                1323006478
                                        );
@@ -153,6 +160,20 @@ class t3lib_mail_Mailer extends Swift_Mailer {
                }
                return;
        }
+
+       /**
+        * This method is only used in unit tests
+        *
+        * @param array $mailSettings
+        * @access private
+        */
+       public function injectMailSettings(array $mailSettings = NULL) {
+               if (is_array($mailSettings)) {
+                       $this->mailSettings = $mailSettings;
+               } else {
+                       $this->mailSettings = (array)$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
+               }
+       }
 }
 
 ?>
\ No newline at end of file
index 3fd6a70..8da105f 100644 (file)
@@ -44,7 +44,7 @@ class t3lib_mail_MboxTransport implements Swift_Transport {
 
        /**
         * Create a new MailTransport
-        * @param Swift_Transport_Log $log
+        * @param string $debugFile
         */
        public function __construct($debugFile) {
                $this->debugFile = $debugFile;
diff --git a/tests/Unit/t3lib/mail/class.t3lib_mail_mailerTest.php b/tests/Unit/t3lib/mail/class.t3lib_mail_mailerTest.php
new file mode 100644 (file)
index 0000000..be24153
--- /dev/null
@@ -0,0 +1,122 @@
+<?php
+/***************************************************************
+* Copyright notice
+*
+* (c) 2011 Ernesto Baschny (ernst@cron-it.de)
+* All rights reserved
+*
+* This script is part of the TYPO3 project. The TYPO3 project is
+* free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation; either version 2 of the License, or
+* (at your option) any later version.
+*
+* The GNU General Public License can be found at
+* http://www.gnu.org/copyleft/gpl.html.
+*
+* This script is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+/**
+ * Testcase for the t3lib_mail_Mailer class.
+ *
+ * @package TYPO3
+ * @subpackage t3lib
+ *
+ * @author Helmut Hummel <helmut.hummel@typo3.org>
+ */
+class t3lib_mail_MailerTest extends tx_phpunit_testcase {
+
+       /**
+        * @var t3lib_mail_Mailer
+        */
+       protected $fixture;
+
+       /**
+        * @var array
+        */
+       protected $globalMailSettings;
+
+       public function setUp() {
+               $this->globalMailSettings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
+               $this->fixture = $this->getMock('t3lib_mail_Mailer', array('noMethodMocked'), array(), '', FALSE);
+       }
+
+       public function tearDown() {
+               unset($this->fixture);
+               $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $this->globalMailSettings;
+       }
+
+       //////////////////////////
+       // Tests concerning t3lib_mail_Mailer
+       //////////////////////////
+
+       /**
+        * @test
+        */
+       public function injectedSettingsAreNotReplacedByGlobalSettings() {
+               $settings = array('transport' => 'mbox', 'transport_mbox_file' => '/path/to/file');
+               $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail');
+               $this->fixture->injectMailSettings($settings);
+               $this->fixture->__construct();
+
+               $this->assertAttributeSame($settings, 'mailSettings', $this->fixture);
+       }
+
+       /**
+        * @test
+        */
+       public function globalSettingsAreUsedIfNoSettingsAreInjected() {
+               $settings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail');
+               $this->fixture->__construct();
+
+               $this->assertAttributeSame($settings, 'mailSettings', $this->fixture);
+       }
+
+       /**
+        * Data provider for wrongConfigigurationThrowsException
+        *
+        * @return array Data sets
+        */
+       public static function wrongConfigigurationProvider() {
+               return array(
+                       'smtp but no host' => array(array('transport' => 'smtp')),
+                       'sendmail but no command' => array(array('transport' => 'sendmail')),
+                       'mbox but no file' => array(array('transport' => 'mbox')),
+                       'no instance of Swift_Transport' => array(array('transport' => 't3lib_message_ErrorpageMessage')),
+               );
+       }
+
+       /**
+        * @test
+        * @param $settings
+        * @dataProvider wrongConfigigurationProvider
+        * @expectedException t3lib_exception
+        */
+       public function wrongConfigigurationThrowsException($settings) {
+               $this->fixture->injectMailSettings($settings);
+               $this->fixture->__construct();
+       }
+
+
+       /**
+        * @test
+        */
+       public function providingCorrectClassnameDoesNotThrowException() {
+               if (!class_exists('t3lib_mail_SwiftMailerFakeTransport')) {
+                               // Create fake custom transport class
+                       eval('class t3lib_mail_SwiftMailerFakeTransport extends t3lib_mail_mboxtransport {
+                               public function __construct($settings) {}
+                       }');
+               }
+
+               $this->fixture->injectMailSettings(array('transport'=>'t3lib_mail_SwiftMailerFakeTransport'));
+               $this->fixture->__construct();
+       }
+}
+?>
\ No newline at end of file
index 3915fb6..5cdca6c 100644 (file)
  */
 class t3lib_mail_SwiftMailerAdapterTest extends tx_phpunit_testcase {
 
+       /**
+        * @var t3lib_mail_SwiftMailerAdapter
+        */
+       protected $fixture;
+
        public function setUp() {
                if (!class_exists('t3lib_mail_SwiftMailerAdapterExposed')) {
                        // Make protected methods accessible so that they can be tested: