[TASK] Refactor t3lib_mail_Mailer and add unit tests
[Packages/TYPO3.CMS.git] / tests / Unit / t3lib / mail / class.t3lib_mail_mailerTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Ernesto Baschny (ernst@cron-it.de)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for the t3lib_mail_Mailer class.
27 *
28 * @package TYPO3
29 * @subpackage t3lib
30 *
31 * @author Helmut Hummel <helmut.hummel@typo3.org>
32 */
33 class t3lib_mail_MailerTest extends tx_phpunit_testcase {
34
35 /**
36 * @var t3lib_mail_Mailer
37 */
38 protected $fixture;
39
40 /**
41 * @var array
42 */
43 protected $globalMailSettings;
44
45 public function setUp() {
46 $this->globalMailSettings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
47 $this->fixture = $this->getMock('t3lib_mail_Mailer', array('noMethodMocked'), array(), '', FALSE);
48 }
49
50 public function tearDown() {
51 unset($this->fixture);
52 $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $this->globalMailSettings;
53 }
54
55 //////////////////////////
56 // Tests concerning t3lib_mail_Mailer
57 //////////////////////////
58
59 /**
60 * @test
61 */
62 public function injectedSettingsAreNotReplacedByGlobalSettings() {
63 $settings = array('transport' => 'mbox', 'transport_mbox_file' => '/path/to/file');
64 $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail');
65 $this->fixture->injectMailSettings($settings);
66 $this->fixture->__construct();
67
68 $this->assertAttributeSame($settings, 'mailSettings', $this->fixture);
69 }
70
71 /**
72 * @test
73 */
74 public function globalSettingsAreUsedIfNoSettingsAreInjected() {
75 $settings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail');
76 $this->fixture->__construct();
77
78 $this->assertAttributeSame($settings, 'mailSettings', $this->fixture);
79 }
80
81 /**
82 * Data provider for wrongConfigigurationThrowsException
83 *
84 * @return array Data sets
85 */
86 public static function wrongConfigigurationProvider() {
87 return array(
88 'smtp but no host' => array(array('transport' => 'smtp')),
89 'sendmail but no command' => array(array('transport' => 'sendmail')),
90 'mbox but no file' => array(array('transport' => 'mbox')),
91 'no instance of Swift_Transport' => array(array('transport' => 't3lib_message_ErrorpageMessage')),
92 );
93 }
94
95 /**
96 * @test
97 * @param $settings
98 * @dataProvider wrongConfigigurationProvider
99 * @expectedException t3lib_exception
100 */
101 public function wrongConfigigurationThrowsException($settings) {
102 $this->fixture->injectMailSettings($settings);
103 $this->fixture->__construct();
104 }
105
106
107 /**
108 * @test
109 */
110 public function providingCorrectClassnameDoesNotThrowException() {
111 if (!class_exists('t3lib_mail_SwiftMailerFakeTransport')) {
112 // Create fake custom transport class
113 eval('class t3lib_mail_SwiftMailerFakeTransport extends t3lib_mail_mboxtransport {
114 public function __construct($settings) {}
115 }');
116 }
117
118 $this->fixture->injectMailSettings(array('transport'=>'t3lib_mail_SwiftMailerFakeTransport'));
119 $this->fixture->__construct();
120 }
121 }
122 ?>