‪TYPO3CMS  ‪main
TransportFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
21 use Psr\Log\NullLogger;
22 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
23 use Symfony\Component\Mailer\Transport\NullTransport;
24 use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
25 use Symfony\Component\Mailer\Transport\TransportInterface;
26 use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
38 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
39 
40 final class ‪TransportFactoryTest extends UnitTestCase
41 {
42  protected bool ‪$resetSingletonInstances = true;
43 
44  protected function ‪getSubject(&$eventDispatcher): ‪TransportFactory
45  {
46  $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
47  $logger = new NullLogger();
48 
49  $logManager = $this->createMock(LogManagerInterface::class);
50  $logManager->method('getLogger')->willReturn($logger);
51 
52  $transportFactory = new ‪TransportFactory($eventDispatcher, $logManager);
53  $transportFactory->setLogger($logger);
54 
55  return $transportFactory;
56  }
57 
58  #[Test]
60  {
61  $mailSettings = [
62  'transport' => 'sendmail',
63  'transport_smtp_server' => 'localhost:25',
64  'transport_smtp_encrypt' => '',
65  'transport_smtp_username' => '',
66  'transport_smtp_password' => '',
67  'transport_smtp_restart_threshold' => 0,
68  'transport_smtp_restart_threshold_sleep' => 0,
69  'transport_smtp_ping_threshold' => 0,
70  'transport_smtp_stream_options' => [],
71  'transport_sendmail_command' => '',
72  'transport_mbox_file' => '',
73  'defaultMailFromAddress' => '',
74  'defaultMailFromName' => '',
75  'transport_spool_type' => 'file',
76  'transport_spool_filepath' => '.',
77  ];
78 
79  // Register fixture class
80  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][FileSpool::class]['className'] = FakeFileSpoolFixture::class;
81 
82  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
83  self::assertInstanceOf(DelayedTransportInterface::class, $transport);
84  self::assertInstanceOf(FakeFileSpoolFixture::class, $transport);
85 
86  $path = $transport->getPath();
87  self::assertStringContainsString($mailSettings['transport_spool_filepath'], $path);
88  }
89 
90  #[Test]
92  {
93  $mailSettings = [
94  'transport' => 'mail',
95  'transport_smtp_server' => 'localhost:25',
96  'transport_smtp_encrypt' => '',
97  'transport_smtp_username' => '',
98  'transport_smtp_password' => '',
99  'transport_smtp_restart_threshold' => 0,
100  'transport_smtp_restart_threshold_sleep' => 0,
101  'transport_smtp_ping_threshold' => 0,
102  'transport_smtp_stream_options' => [],
103  'transport_sendmail_command' => '',
104  'transport_mbox_file' => '',
105  'defaultMailFromAddress' => '',
106  'defaultMailFromName' => '',
107  'transport_spool_type' => 'memory',
108  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
109  ];
110 
111  // Register fixture class
112  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][MemorySpool::class]['className'] = FakeMemorySpoolFixture::class;
113 
114  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
115  self::assertInstanceOf(DelayedTransportInterface::class, $transport);
116  self::assertInstanceOf(MemorySpool::class, $transport);
117  }
118 
119  #[Test]
121  {
122  $mailSettings = [
123  'transport' => 'sendmail',
124  'transport_smtp_server' => 'localhost:25',
125  'transport_smtp_encrypt' => '',
126  'transport_smtp_username' => '',
127  'transport_smtp_password' => '',
128  'transport_smtp_restart_threshold' => 0,
129  'transport_smtp_restart_threshold_sleep' => 0,
130  'transport_smtp_ping_threshold' => 0,
131  'transport_smtp_stream_options' => [],
132  'transport_sendmail_command' => '',
133  'transport_mbox_file' => '',
134  'defaultMailFromAddress' => '',
135  'defaultMailFromName' => '',
136  'transport_spool_type' => FakeValidSpoolFixture::class,
137  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
138  ];
139 
140  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
141  self::assertInstanceOf(DelayedTransportInterface::class, $transport);
142  self::assertInstanceOf(FakeValidSpoolFixture::class, $transport);
143 
144  self::assertSame($mailSettings, $transport->getSettings());
145  }
146 
147  #[Test]
149  {
150  $this->expectExceptionCode(1466799482);
151 
152  $mailSettings = [
153  'transport' => 'mail',
154  'transport_smtp_server' => 'localhost:25',
155  'transport_smtp_encrypt' => '',
156  'transport_smtp_username' => '',
157  'transport_smtp_password' => '',
158  'transport_smtp_restart_threshold' => 0,
159  'transport_smtp_restart_threshold_sleep' => 0,
160  'transport_smtp_ping_threshold' => 0,
161  'transport_smtp_stream_options' => [],
162  'transport_sendmail_command' => '',
163  'transport_mbox_file' => '',
164  'defaultMailFromAddress' => '',
165  'defaultMailFromName' => '',
166  'transport_spool_type' => FakeInvalidSpoolFixture::class,
167  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
168  ];
169 
170  $this->‪getSubject($eventDispatcher)->get($mailSettings);
171  }
172 
173  #[Test]
175  {
176  $this->expectExceptionCode(1615021869);
177 
178  $mailSettings = [
179  'transport' => 'dsn',
180  'dsn' => '',
181  ];
182 
183  $this->‪getSubject($eventDispatcher)->get($mailSettings);
184  }
185 
186  #[Test]
188  {
189  $mailSettings = [
190  'transport' => 'dsn',
191  'dsn' => 'smtp://user:pass@smtp.example.com:25',
192  ];
193 
194  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
195  $eventDispatcher->expects(self::atLeastOnce())->method('dispatch')->with(self::anything());
196 
197  $message = new ‪MailMessage();
198  $message->setTo(['foo@bar.com'])
199  ->text('foo')
200  ->from('bar@foo.com')
201  ;
202  try {
203  $transport->send($message);
204  } catch (TransportExceptionInterface $exception) {
205  // connection is not valid in tests, so we just catch the exception here.
206  }
207  }
208 
209  #[Test]
211  {
212  $mailSettings = [
213  'transport' => 'smtp',
214  'transport_smtp_server' => 'localhost:25',
215  'transport_smtp_encrypt' => '',
216  'transport_smtp_username' => '',
217  'transport_smtp_password' => '',
218  'transport_smtp_restart_threshold' => 0,
219  'transport_smtp_restart_threshold_sleep' => 0,
220  'transport_smtp_ping_threshold' => 0,
221  'transport_smtp_stream_options' => [],
222  'transport_sendmail_command' => '',
223  'transport_mbox_file' => '',
224  'defaultMailFromAddress' => '',
225  'defaultMailFromName' => '',
226  'transport_spool_type' => '',
227  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
228  ];
229 
230  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
231  self::assertInstanceOf(TransportInterface::class, $transport);
232  }
233 
234  #[Test]
236  {
237  $mailSettings = [
238  'transport' => 'smtp',
239  'transport_smtp_server' => 'localhost:25',
240  'transport_smtp_encrypt' => '',
241  'transport_smtp_username' => '',
242  'transport_smtp_password' => '',
243  'transport_smtp_restart_threshold' => 0,
244  'transport_smtp_restart_threshold_sleep' => 0,
245  'transport_smtp_ping_threshold' => 0,
246  'transport_smtp_stream_options' => [],
247  'transport_sendmail_command' => '',
248  'transport_mbox_file' => '',
249  'defaultMailFromAddress' => '',
250  'defaultMailFromName' => '',
251  ];
252 
253  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
254  $eventDispatcher->expects(self::atLeastOnce())->method('dispatch')->with(self::anything());
255 
256  $message = new ‪MailMessage();
257  $message->setTo(['foo@bar.com'])
258  ->text('foo')
259  ->from('bar@foo.com')
260  ;
261  try {
262  $transport->send($message);
263  } catch (TransportExceptionInterface $exception) {
264  // connection is not valid in tests, so we just catch the exception here.
265  }
266  }
267 
268  #[Test]
270  {
271  $mailSettings = [
272  'transport' => 'sendmail',
273  'transport_smtp_server' => 'localhost:25',
274  'transport_smtp_encrypt' => '',
275  'transport_smtp_username' => '',
276  'transport_smtp_password' => '',
277  'transport_smtp_restart_threshold' => 0,
278  'transport_smtp_restart_threshold_sleep' => 0,
279  'transport_smtp_ping_threshold' => 0,
280  'transport_smtp_stream_options' => [],
281  'transport_sendmail_command' => '',
282  'transport_mbox_file' => '',
283  'defaultMailFromAddress' => '',
284  'defaultMailFromName' => '',
285  ];
286 
287  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
288  $eventDispatcher->expects(self::atLeastOnce())->method('dispatch')->with(self::anything());
289 
290  $message = new ‪MailMessage();
291  $message->setTo(['foo@bar.com'])
292  ->text('foo')
293  ->from('bar@foo.com')
294  ;
295  try {
296  $transport->send($message);
297  } catch (TransportExceptionInterface $exception) {
298  // connection is not valid in tests, so we just catch the exception here.
299  }
300  }
301 
302  #[Test]
304  {
305  $mailSettings = [
306  'transport' => NullTransport::class,
307  'transport_smtp_server' => 'localhost:25',
308  'transport_smtp_encrypt' => '',
309  'transport_smtp_username' => '',
310  'transport_smtp_password' => '',
311  'transport_sendmail_command' => '',
312  'transport_smtp_restart_threshold' => 0,
313  'transport_smtp_restart_threshold_sleep' => 0,
314  'transport_smtp_ping_threshold' => 0,
315  'transport_smtp_stream_options' => [],
316  'transport_mbox_file' => '',
317  'defaultMailFromAddress' => '',
318  'defaultMailFromName' => '',
319  ];
320 
321  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
322  $eventDispatcher->expects(self::atLeastOnce())->method('dispatch')->with(self::anything());
323 
324  $message = new ‪MailMessage();
325  $message->setTo(['foo@bar.com'])
326  ->text('foo')
327  ->from('bar@foo.com')
328  ;
329  $transport->send($message);
330  }
331 
332  #[Test]
334  {
335  $mailSettings = [
336  'transport' => 'smtp',
337  'transport_smtp_server' => 'localhost:25',
338  'transport_smtp_username' => 'username',
339  'transport_smtp_password' => 'password',
340  'transport_smtp_domain' => 'example.com',
341  ];
342 
343  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
344 
345  self::assertInstanceOf(EsmtpTransport::class, $transport);
346  self::assertSame(explode(':', $mailSettings['transport_smtp_server'], 2)[0], $transport->getStream()->getHost());
347  self::assertSame((int)explode(':', $mailSettings['transport_smtp_server'], 2)[1], $transport->getStream()->getPort());
348  self::assertSame($mailSettings['transport_smtp_username'], $transport->getUsername());
349  self::assertSame($mailSettings['transport_smtp_password'], $transport->getPassword());
350  self::assertSame($mailSettings['transport_smtp_domain'], $transport->getLocalDomain());
351  }
352 }
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\smtpTransportCallsDispatchOfDispatcher
‪smtpTransportCallsDispatchOfDispatcher()
Definition: TransportFactoryTest.php:235
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: TransportFactoryTest.php:42
‪TYPO3\CMS\Core\Mail\MemorySpool
Definition: MemorySpool.php:41
‪TYPO3\CMS\Core\Log\LogManagerInterface
Definition: LogManagerInterface.php:24
‪TYPO3\CMS\Core\Tests\Unit\Mail
‪TYPO3\CMS\Core\Mail\MailMessage
Definition: MailMessage.php:28
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsSpoolTransportUsingMemorySpool
‪getReturnsSpoolTransportUsingMemorySpool()
Definition: TransportFactoryTest.php:91
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\sendmailTransportCallsDispatchOfDispatcher
‪sendmailTransportCallsDispatchOfDispatcher()
Definition: TransportFactoryTest.php:269
‪TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeFileSpoolFixture
Definition: FakeFileSpoolFixture.php:26
‪TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeMemorySpoolFixture
Definition: FakeMemorySpoolFixture.php:26
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getThrowsRuntimeExceptionForInvalidCustomSpool
‪getThrowsRuntimeExceptionForInvalidCustomSpool()
Definition: TransportFactoryTest.php:148
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsMailerTransportInterface
‪getReturnsMailerTransportInterface()
Definition: TransportFactoryTest.php:210
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsSpoolTransportUsingCustomSpool
‪getReturnsSpoolTransportUsingCustomSpool()
Definition: TransportFactoryTest.php:120
‪TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeInvalidSpoolFixture
Definition: FakeInvalidSpoolFixture.php:26
‪TYPO3\CMS\Core\Mail\FileSpool
Definition: FileSpool.php:39
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\dsnTransportCallsDispatchOfDispatcher
‪dsnTransportCallsDispatchOfDispatcher()
Definition: TransportFactoryTest.php:187
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsSpoolTransportUsingFileSpool
‪getReturnsSpoolTransportUsingFileSpool()
Definition: TransportFactoryTest.php:59
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getSubject
‪getSubject(&$eventDispatcher)
Definition: TransportFactoryTest.php:44
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\smtpTransportIsCorrectlyConfigured
‪smtpTransportIsCorrectlyConfigured()
Definition: TransportFactoryTest.php:333
‪TYPO3\CMS\Core\Mail\DelayedTransportInterface
Definition: DelayedTransportInterface.php:26
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\nullTransportCallsDispatchOfDispatcher
‪nullTransportCallsDispatchOfDispatcher()
Definition: TransportFactoryTest.php:303
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeValidSpoolFixture
Definition: FakeValidSpoolFixture.php:30
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getThrowsExceptionForMissingDsnConfig
‪getThrowsExceptionForMissingDsnConfig()
Definition: TransportFactoryTest.php:174
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest
Definition: TransportFactoryTest.php:41
‪TYPO3\CMS\Core\Mail\TransportFactory
Definition: TransportFactory.php:38