‪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 Psr\Log\NullLogger;
21 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
22 use Symfony\Component\Mailer\Transport\NullTransport;
23 use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
24 use Symfony\Component\Mailer\Transport\TransportInterface;
25 use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
37 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
38 
39 final class ‪TransportFactoryTest extends UnitTestCase
40 {
41  protected bool ‪$resetSingletonInstances = true;
42 
43  protected function ‪getSubject(&$eventDispatcher): ‪TransportFactory
44  {
45  $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
46  $logger = new NullLogger();
47 
48  $logManager = $this->createMock(LogManagerInterface::class);
49  $logManager->method('getLogger')->willReturn($logger);
50 
51  $transportFactory = new ‪TransportFactory($eventDispatcher, $logManager);
52  $transportFactory->setLogger($logger);
53 
54  return $transportFactory;
55  }
56 
61  {
62  $mailSettings = [
63  'transport' => 'sendmail',
64  'transport_smtp_server' => 'localhost:25',
65  'transport_smtp_encrypt' => '',
66  'transport_smtp_username' => '',
67  'transport_smtp_password' => '',
68  'transport_smtp_restart_threshold' => 0,
69  'transport_smtp_restart_threshold_sleep' => 0,
70  'transport_smtp_ping_threshold' => 0,
71  'transport_smtp_stream_options' => [],
72  'transport_sendmail_command' => '',
73  'transport_mbox_file' => '',
74  'defaultMailFromAddress' => '',
75  'defaultMailFromName' => '',
76  'transport_spool_type' => 'file',
77  'transport_spool_filepath' => '.',
78  ];
79 
80  // Register fixture class
81  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][FileSpool::class]['className'] = FakeFileSpoolFixture::class;
82 
83  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
84  self::assertInstanceOf(DelayedTransportInterface::class, $transport);
85  self::assertInstanceOf(FakeFileSpoolFixture::class, $transport);
86 
87  $path = $transport->getPath();
88  self::assertStringContainsString($mailSettings['transport_spool_filepath'], $path);
89  }
90 
95  {
96  $mailSettings = [
97  'transport' => 'mail',
98  'transport_smtp_server' => 'localhost:25',
99  'transport_smtp_encrypt' => '',
100  'transport_smtp_username' => '',
101  'transport_smtp_password' => '',
102  'transport_smtp_restart_threshold' => 0,
103  'transport_smtp_restart_threshold_sleep' => 0,
104  'transport_smtp_ping_threshold' => 0,
105  'transport_smtp_stream_options' => [],
106  'transport_sendmail_command' => '',
107  'transport_mbox_file' => '',
108  'defaultMailFromAddress' => '',
109  'defaultMailFromName' => '',
110  'transport_spool_type' => 'memory',
111  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
112  ];
113 
114  // Register fixture class
115  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][MemorySpool::class]['className'] = FakeMemorySpoolFixture::class;
116 
117  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
118  self::assertInstanceOf(DelayedTransportInterface::class, $transport);
119  self::assertInstanceOf(MemorySpool::class, $transport);
120  }
121 
126  {
127  $mailSettings = [
128  'transport' => 'sendmail',
129  'transport_smtp_server' => 'localhost:25',
130  'transport_smtp_encrypt' => '',
131  'transport_smtp_username' => '',
132  'transport_smtp_password' => '',
133  'transport_smtp_restart_threshold' => 0,
134  'transport_smtp_restart_threshold_sleep' => 0,
135  'transport_smtp_ping_threshold' => 0,
136  'transport_smtp_stream_options' => [],
137  'transport_sendmail_command' => '',
138  'transport_mbox_file' => '',
139  'defaultMailFromAddress' => '',
140  'defaultMailFromName' => '',
141  'transport_spool_type' => FakeValidSpoolFixture::class,
142  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
143  ];
144 
145  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
146  self::assertInstanceOf(DelayedTransportInterface::class, $transport);
147  self::assertInstanceOf(FakeValidSpoolFixture::class, $transport);
148 
149  self::assertSame($mailSettings, $transport->getSettings());
150  }
151 
156  {
157  $this->expectExceptionCode(1466799482);
158 
159  $mailSettings = [
160  'transport' => 'mail',
161  'transport_smtp_server' => 'localhost:25',
162  'transport_smtp_encrypt' => '',
163  'transport_smtp_username' => '',
164  'transport_smtp_password' => '',
165  'transport_smtp_restart_threshold' => 0,
166  'transport_smtp_restart_threshold_sleep' => 0,
167  'transport_smtp_ping_threshold' => 0,
168  'transport_smtp_stream_options' => [],
169  'transport_sendmail_command' => '',
170  'transport_mbox_file' => '',
171  'defaultMailFromAddress' => '',
172  'defaultMailFromName' => '',
173  'transport_spool_type' => FakeInvalidSpoolFixture::class,
174  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
175  ];
176 
177  $this->‪getSubject($eventDispatcher)->get($mailSettings);
178  }
179 
184  {
185  $this->expectExceptionCode(1615021869);
186 
187  $mailSettings = [
188  'transport' => 'dsn',
189  'dsn' => '',
190  ];
191 
192  $this->‪getSubject($eventDispatcher)->get($mailSettings);
193  }
194 
199  {
200  $mailSettings = [
201  'transport' => 'dsn',
202  'dsn' => 'smtp://user:pass@smtp.example.com:25',
203  ];
204 
205  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
206  $eventDispatcher->expects(self::atLeastOnce())->method('dispatch')->with(self::anything());
207 
208  $message = new ‪MailMessage();
209  $message->setTo(['foo@bar.com'])
210  ->text('foo')
211  ->from('bar@foo.com')
212  ;
213  try {
214  $transport->send($message);
215  } catch (TransportExceptionInterface $exception) {
216  // connection is not valid in tests, so we just catch the exception here.
217  }
218  }
219 
224  {
225  $mailSettings = [
226  'transport' => 'smtp',
227  'transport_smtp_server' => 'localhost:25',
228  'transport_smtp_encrypt' => '',
229  'transport_smtp_username' => '',
230  'transport_smtp_password' => '',
231  'transport_smtp_restart_threshold' => 0,
232  'transport_smtp_restart_threshold_sleep' => 0,
233  'transport_smtp_ping_threshold' => 0,
234  'transport_smtp_stream_options' => [],
235  'transport_sendmail_command' => '',
236  'transport_mbox_file' => '',
237  'defaultMailFromAddress' => '',
238  'defaultMailFromName' => '',
239  'transport_spool_type' => '',
240  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
241  ];
242 
243  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
244  self::assertInstanceOf(TransportInterface::class, $transport);
245  }
246 
251  {
252  $mailSettings = [
253  'transport' => 'smtp',
254  'transport_smtp_server' => 'localhost:25',
255  'transport_smtp_encrypt' => '',
256  'transport_smtp_username' => '',
257  'transport_smtp_password' => '',
258  'transport_smtp_restart_threshold' => 0,
259  'transport_smtp_restart_threshold_sleep' => 0,
260  'transport_smtp_ping_threshold' => 0,
261  'transport_smtp_stream_options' => [],
262  'transport_sendmail_command' => '',
263  'transport_mbox_file' => '',
264  'defaultMailFromAddress' => '',
265  'defaultMailFromName' => '',
266  ];
267 
268  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
269  $eventDispatcher->expects(self::atLeastOnce())->method('dispatch')->with(self::anything());
270 
271  $message = new ‪MailMessage();
272  $message->setTo(['foo@bar.com'])
273  ->text('foo')
274  ->from('bar@foo.com')
275  ;
276  try {
277  $transport->send($message);
278  } catch (TransportExceptionInterface $exception) {
279  // connection is not valid in tests, so we just catch the exception here.
280  }
281  }
282 
287  {
288  $mailSettings = [
289  'transport' => 'sendmail',
290  'transport_smtp_server' => 'localhost:25',
291  'transport_smtp_encrypt' => '',
292  'transport_smtp_username' => '',
293  'transport_smtp_password' => '',
294  'transport_smtp_restart_threshold' => 0,
295  'transport_smtp_restart_threshold_sleep' => 0,
296  'transport_smtp_ping_threshold' => 0,
297  'transport_smtp_stream_options' => [],
298  'transport_sendmail_command' => '',
299  'transport_mbox_file' => '',
300  'defaultMailFromAddress' => '',
301  'defaultMailFromName' => '',
302  ];
303 
304  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
305  $eventDispatcher->expects(self::atLeastOnce())->method('dispatch')->with(self::anything());
306 
307  $message = new ‪MailMessage();
308  $message->setTo(['foo@bar.com'])
309  ->text('foo')
310  ->from('bar@foo.com')
311  ;
312  try {
313  $transport->send($message);
314  } catch (TransportExceptionInterface $exception) {
315  // connection is not valid in tests, so we just catch the exception here.
316  }
317  }
318 
323  {
324  $mailSettings = [
325  'transport' => NullTransport::class,
326  'transport_smtp_server' => 'localhost:25',
327  'transport_smtp_encrypt' => '',
328  'transport_smtp_username' => '',
329  'transport_smtp_password' => '',
330  'transport_sendmail_command' => '',
331  'transport_smtp_restart_threshold' => 0,
332  'transport_smtp_restart_threshold_sleep' => 0,
333  'transport_smtp_ping_threshold' => 0,
334  'transport_smtp_stream_options' => [],
335  'transport_mbox_file' => '',
336  'defaultMailFromAddress' => '',
337  'defaultMailFromName' => '',
338  ];
339 
340  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
341  $eventDispatcher->expects(self::atLeastOnce())->method('dispatch')->with(self::anything());
342 
343  $message = new ‪MailMessage();
344  $message->setTo(['foo@bar.com'])
345  ->text('foo')
346  ->from('bar@foo.com')
347  ;
348  $transport->send($message);
349  }
350 
355  {
356  $mailSettings = [
357  'transport' => 'smtp',
358  'transport_smtp_server' => 'localhost:25',
359  'transport_smtp_username' => 'username',
360  'transport_smtp_password' => 'password',
361  'transport_smtp_domain' => 'example.com',
362  ];
363 
364  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
365 
366  self::assertInstanceOf(EsmtpTransport::class, $transport);
367  self::assertSame(explode(':', $mailSettings['transport_smtp_server'], 2)[0], $transport->getStream()->getHost());
368  self::assertSame((int)explode(':', $mailSettings['transport_smtp_server'], 2)[1], $transport->getStream()->getPort());
369  self::assertSame($mailSettings['transport_smtp_username'], $transport->getUsername());
370  self::assertSame($mailSettings['transport_smtp_password'], $transport->getPassword());
371  self::assertSame($mailSettings['transport_smtp_domain'], $transport->getLocalDomain());
372  }
373 }
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\smtpTransportCallsDispatchOfDispatcher
‪smtpTransportCallsDispatchOfDispatcher()
Definition: TransportFactoryTest.php:250
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: TransportFactoryTest.php:41
‪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:94
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\sendmailTransportCallsDispatchOfDispatcher
‪sendmailTransportCallsDispatchOfDispatcher()
Definition: TransportFactoryTest.php:286
‪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:155
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsMailerTransportInterface
‪getReturnsMailerTransportInterface()
Definition: TransportFactoryTest.php:223
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsSpoolTransportUsingCustomSpool
‪getReturnsSpoolTransportUsingCustomSpool()
Definition: TransportFactoryTest.php:125
‪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:198
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsSpoolTransportUsingFileSpool
‪getReturnsSpoolTransportUsingFileSpool()
Definition: TransportFactoryTest.php:60
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getSubject
‪getSubject(&$eventDispatcher)
Definition: TransportFactoryTest.php:43
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\smtpTransportIsCorrectlyConfigured
‪smtpTransportIsCorrectlyConfigured()
Definition: TransportFactoryTest.php:354
‪TYPO3\CMS\Core\Mail\DelayedTransportInterface
Definition: DelayedTransportInterface.php:26
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\nullTransportCallsDispatchOfDispatcher
‪nullTransportCallsDispatchOfDispatcher()
Definition: TransportFactoryTest.php:322
‪$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:183
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest
Definition: TransportFactoryTest.php:40
‪TYPO3\CMS\Core\Mail\TransportFactory
Definition: TransportFactory.php:37