[FEATURE] Integrate Swift Mailer's spool transport
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Mail / TransportFactoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Mail;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeValidSpoolFixture;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Testcase for the TYPO3\CMS\Core\Mail\TransportFactory class.
22 */
23 class TransportFactoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
24 {
25 /**
26 * @var \TYPO3\CMS\Core\Mail\TransportFactory
27 */
28 protected $subject;
29
30 protected function setUp()
31 {
32 $this->subject = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\TransportFactory::class);
33 }
34
35 /**
36 * @test
37 */
38 public function getReturnsSwiftSpoolTransportUsingSwiftFileSpool()
39 {
40 $mailSettings = [
41 'transport' => 'mail',
42 'transport_smtp_server' => 'localhost:25',
43 'transport_smtp_encrypt' => '',
44 'transport_smtp_username' => '',
45 'transport_smtp_password' => '',
46 'transport_sendmail_command' => '',
47 'transport_mbox_file' => '',
48 'defaultMailFromAddress' => '',
49 'defaultMailFromName' => '',
50 'transport_spool_type' => 'file',
51 'transport_spool_filepath' => 'typo3temp/var/messages/',
52 ];
53
54 // Register fixture class
55 $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\Swift_FileSpool::class]['className'] = \TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeFileSpoolFixture::class;
56
57 $transport = $this->subject->get($mailSettings);
58 $this->assertInstanceOf(\Swift_SpoolTransport::class, $transport);
59
60 $spool = $transport->getSpool();
61 $this->assertInstanceOf(\Swift_FileSpool::class, $spool);
62
63 $path = $spool->getPath();
64 $this->assertContains($mailSettings['transport_spool_filepath'], $path);
65 }
66
67 /**
68 * @test
69 */
70 public function getReturnsSwiftSpoolTransportUsingSwiftMemorySpool()
71 {
72 $mailSettings = [
73 'transport' => 'mail',
74 'transport_smtp_server' => 'localhost:25',
75 'transport_smtp_encrypt' => '',
76 'transport_smtp_username' => '',
77 'transport_smtp_password' => '',
78 'transport_sendmail_command' => '',
79 'transport_mbox_file' => '',
80 'defaultMailFromAddress' => '',
81 'defaultMailFromName' => '',
82 'transport_spool_type' => 'memory',
83 'transport_spool_filepath' => 'typo3temp/var/messages/',
84 ];
85
86 // Register fixture class
87 $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Core\Mail\MemorySpool::class]['className'] = \TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeMemorySpoolFixture::class;
88
89 $transport = $this->subject->get($mailSettings);
90 $this->assertInstanceOf(\Swift_SpoolTransport::class, $transport);
91
92 $spool = $transport->getSpool();
93 $this->assertInstanceOf(\Swift_MemorySpool::class, $spool);
94 }
95
96 /**
97 * @test
98 */
99 public function getReturnsSwiftSpoolTransportUsingCustomSpool()
100 {
101 $mailSettings = [
102 'transport' => 'mail',
103 'transport_smtp_server' => 'localhost:25',
104 'transport_smtp_encrypt' => '',
105 'transport_smtp_username' => '',
106 'transport_smtp_password' => '',
107 'transport_sendmail_command' => '',
108 'transport_mbox_file' => '',
109 'defaultMailFromAddress' => '',
110 'defaultMailFromName' => '',
111 'transport_spool_type' => 'TYPO3\\CMS\\Core\\Tests\\Unit\\Mail\\Fixtures\\FakeValidSpoolFixture',
112 'transport_spool_filepath' => 'typo3temp/var/messages/',
113 ];
114
115 $transport = $this->subject->get($mailSettings);
116 $this->assertInstanceOf(\Swift_SpoolTransport::class, $transport);
117
118 $spool = $transport->getSpool();
119 $this->assertInstanceOf(FakeValidSpoolFixture::class, $spool);
120
121 $this->assertSame($mailSettings, $spool->getSettings());
122 }
123
124 /**
125 * @test
126 */
127 public function getThrowsRuntimeExceptionForInvalidCustomSpool()
128 {
129 $this->expectException(\RuntimeException::class);
130 $this->expectExceptionCode(1466799482);
131
132 $mailSettings = [
133 'transport' => 'mail',
134 'transport_smtp_server' => 'localhost:25',
135 'transport_smtp_encrypt' => '',
136 'transport_smtp_username' => '',
137 'transport_smtp_password' => '',
138 'transport_sendmail_command' => '',
139 'transport_mbox_file' => '',
140 'defaultMailFromAddress' => '',
141 'defaultMailFromName' => '',
142 'transport_spool_type' => 'TYPO3\\CMS\\Core\\Tests\\Unit\\Mail\\Fixtures\\FakeInvalidSpoolFixture',
143 'transport_spool_filepath' => 'typo3temp/var/messages/',
144 ];
145
146 $this->subject->get($mailSettings);
147 }
148
149 /**
150 * @test
151 */
152 public function getReturnsSwiftMailTransport()
153 {
154 $mailSettings = [
155 'transport' => 'mail',
156 'transport_smtp_server' => 'localhost:25',
157 'transport_smtp_encrypt' => '',
158 'transport_smtp_username' => '',
159 'transport_smtp_password' => '',
160 'transport_sendmail_command' => '',
161 'transport_mbox_file' => '',
162 'defaultMailFromAddress' => '',
163 'defaultMailFromName' => '',
164 'transport_spool_type' => '',
165 'transport_spool_filepath' => 'typo3temp/var/messages/',
166 ];
167
168 $transport = $this->subject->get($mailSettings);
169 $this->assertInstanceOf(\Swift_MailTransport::class, $transport);
170 }
171 }