‪TYPO3CMS  10.4
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 Prophecy\Argument;
21 use Psr\Log\LoggerInterface;
22 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
23 use Symfony\Component\Mailer\Transport\NullTransport;
24 use Symfony\Component\Mailer\Transport\TransportInterface;
25 use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
37 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
38 
42 class ‪TransportFactoryTest extends UnitTestCase
43 {
47  protected ‪$resetSingletonInstances = true;
48 
49  protected function ‪getSubject(&$eventDispatcher): ‪TransportFactory
50  {
51  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
52  $eventDispatcher->dispatch(Argument::any())->willReturn(Argument::any());
53 
54  $logger = $this->prophesize(LoggerInterface::class);
55 
56  $logManager = $this->prophesize(LogManagerInterface::class);
57  $logManager->getLogger(Argument::any())->willReturn($logger->reveal());
58  $logManager->getLogger()->willReturn($logger->reveal());
59 
60  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $logManager->reveal());
61  $transportFactory->setLogger($logger->reveal());
62 
63  return $transportFactory;
64  }
65 
69  public function ‪getReturnsSpoolTransportUsingFileSpool(): void
70  {
71  $mailSettings = [
72  'transport' => 'sendmail',
73  'transport_smtp_server' => 'localhost:25',
74  'transport_smtp_encrypt' => '',
75  'transport_smtp_username' => '',
76  'transport_smtp_password' => '',
77  'transport_sendmail_command' => '',
78  'transport_mbox_file' => '',
79  'defaultMailFromAddress' => '',
80  'defaultMailFromName' => '',
81  'transport_spool_type' => 'file',
82  'transport_spool_filepath' => '.',
83  ];
84 
85  // Register fixture class
86  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][FileSpool::class]['className'] = FakeFileSpoolFixture::class;
87 
88  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
89  self::assertInstanceOf(DelayedTransportInterface::class, $transport);
90  self::assertInstanceOf(FakeFileSpoolFixture::class, $transport);
91 
92  $path = $transport->getPath();
93  self::assertStringContainsString($mailSettings['transport_spool_filepath'], $path);
94  }
95 
99  public function ‪getReturnsSpoolTransportUsingMemorySpool(): void
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' => 'memory',
112  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
113  ];
114 
115  // Register fixture class
116  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][MemorySpool::class]['className'] = FakeMemorySpoolFixture::class;
117 
118  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
119  self::assertInstanceOf(DelayedTransportInterface::class, $transport);
120  self::assertInstanceOf(MemorySpool::class, $transport);
121  }
122 
126  public function ‪getReturnsSpoolTransportUsingCustomSpool(): void
127  {
128  $mailSettings = [
129  'transport' => 'sendmail',
130  'transport_smtp_server' => 'localhost:25',
131  'transport_smtp_encrypt' => '',
132  'transport_smtp_username' => '',
133  'transport_smtp_password' => '',
134  'transport_sendmail_command' => '',
135  'transport_mbox_file' => '',
136  'defaultMailFromAddress' => '',
137  'defaultMailFromName' => '',
138  'transport_spool_type' => FakeValidSpoolFixture::class,
139  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
140  ];
141 
142  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
143  self::assertInstanceOf(DelayedTransportInterface::class, $transport);
144  self::assertInstanceOf(FakeValidSpoolFixture::class, $transport);
145 
146  self::assertSame($mailSettings, $transport->getSettings());
147  }
148 
153  {
154  $this->expectExceptionCode(1466799482);
155 
156  $mailSettings = [
157  'transport' => 'mail',
158  'transport_smtp_server' => 'localhost:25',
159  'transport_smtp_encrypt' => '',
160  'transport_smtp_username' => '',
161  'transport_smtp_password' => '',
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 
176  public function ‪getReturnsMailerTransportInterface(): void
177  {
178  $mailSettings = [
179  'transport' => 'smtp',
180  'transport_smtp_server' => 'localhost:25',
181  'transport_smtp_encrypt' => '',
182  'transport_smtp_username' => '',
183  'transport_smtp_password' => '',
184  'transport_sendmail_command' => '',
185  'transport_mbox_file' => '',
186  'defaultMailFromAddress' => '',
187  'defaultMailFromName' => '',
188  'transport_spool_type' => '',
189  'transport_spool_filepath' => ‪Environment::getVarPath() . '/messages/',
190  ];
191 
192  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
193  self::assertInstanceOf(TransportInterface::class, $transport);
194  }
195 
199  public function ‪smtpTransportCallsDispatchOfDispatcher(): void
200  {
201  $mailSettings = [
202  'transport' => 'smtp',
203  'transport_smtp_server' => 'localhost:25',
204  'transport_smtp_encrypt' => '',
205  'transport_smtp_username' => '',
206  'transport_smtp_password' => '',
207  'transport_sendmail_command' => '',
208  'transport_mbox_file' => '',
209  'defaultMailFromAddress' => '',
210  'defaultMailFromName' => '',
211  ];
212 
213  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
214 
215  $message = new MailMessage();
216  $message->setTo(['foo@bar.com'])
217  ->text('foo')
218  ->from('bar@foo.com')
219  ;
220  try {
221  $transport->send($message);
222  } catch (TransportExceptionInterface $exception) {
223  // connection is not valid in tests, so we just catch the exception here.
224  }
225 
226  $eventDispatcher->dispatch(Argument::any())->shouldHaveBeenCalledOnce();
227  }
228 
232  public function ‪sendmailTransportCallsDispatchOfDispatcher(): void
233  {
234  $mailSettings = [
235  'transport' => 'sendmail',
236  'transport_smtp_server' => 'localhost:25',
237  'transport_smtp_encrypt' => '',
238  'transport_smtp_username' => '',
239  'transport_smtp_password' => '',
240  'transport_sendmail_command' => '',
241  'transport_mbox_file' => '',
242  'defaultMailFromAddress' => '',
243  'defaultMailFromName' => '',
244  ];
245 
246  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
247  $message = new MailMessage();
248  $message->setTo(['foo@bar.com'])
249  ->text('foo')
250  ->from('bar@foo.com')
251  ;
252  try {
253  $transport->send($message);
254  } catch (TransportExceptionInterface $exception) {
255  // connection is not valid in tests, so we just catch the exception here.
256  }
257 
258  $eventDispatcher->dispatch(Argument::any())->shouldHaveBeenCalledOnce();
259  }
260 
264  public function ‪nullTransportCallsDispatchOfDispatcher(): void
265  {
266  $mailSettings = [
267  'transport' => NullTransport::class,
268  'transport_smtp_server' => 'localhost:25',
269  'transport_smtp_encrypt' => '',
270  'transport_smtp_username' => '',
271  'transport_smtp_password' => '',
272  'transport_sendmail_command' => '',
273  'transport_mbox_file' => '',
274  'defaultMailFromAddress' => '',
275  'defaultMailFromName' => '',
276  ];
277 
278  $transport = $this->‪getSubject($eventDispatcher)->get($mailSettings);
279  $message = new MailMessage();
280  $message->setTo(['foo@bar.com'])
281  ->text('foo')
282  ->from('bar@foo.com')
283  ;
284  $transport->send($message);
285 
286  $eventDispatcher->dispatch(Argument::any())->shouldHaveBeenCalledOnce();
287  }
288 }
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\smtpTransportCallsDispatchOfDispatcher
‪smtpTransportCallsDispatchOfDispatcher()
Definition: TransportFactoryTest.php:198
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: TransportFactoryTest.php:46
‪TYPO3\CMS\Core\Mail\MemorySpool
Definition: MemorySpool.php:41
‪TYPO3\CMS\Core\Log\LogManagerInterface
Definition: LogManagerInterface.php:22
‪TYPO3\CMS\Core\Tests\Unit\Mail
Definition: FileSpoolTest.php:18
‪TYPO3\CMS\Core\Mail\MailMessage
Definition: MailMessage.php:28
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsSpoolTransportUsingMemorySpool
‪getReturnsSpoolTransportUsingMemorySpool()
Definition: TransportFactoryTest.php:98
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\sendmailTransportCallsDispatchOfDispatcher
‪sendmailTransportCallsDispatchOfDispatcher()
Definition: TransportFactoryTest.php:231
‪TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeFileSpoolFixture
Definition: FakeFileSpoolFixture.php:24
‪TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeMemorySpoolFixture
Definition: FakeMemorySpoolFixture.php:24
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getThrowsRuntimeExceptionForInvalidCustomSpool
‪getThrowsRuntimeExceptionForInvalidCustomSpool()
Definition: TransportFactoryTest.php:151
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsMailerTransportInterface
‪getReturnsMailerTransportInterface()
Definition: TransportFactoryTest.php:175
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsSpoolTransportUsingCustomSpool
‪getReturnsSpoolTransportUsingCustomSpool()
Definition: TransportFactoryTest.php:125
‪TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeInvalidSpoolFixture
Definition: FakeInvalidSpoolFixture.php:24
‪TYPO3\CMS\Core\Mail\FileSpool
Definition: FileSpool.php:40
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getReturnsSpoolTransportUsingFileSpool
‪getReturnsSpoolTransportUsingFileSpool()
Definition: TransportFactoryTest.php:68
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\getSubject
‪getSubject(&$eventDispatcher)
Definition: TransportFactoryTest.php:48
‪TYPO3\CMS\Core\Mail\DelayedTransportInterface
Definition: DelayedTransportInterface.php:26
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest\nullTransportCallsDispatchOfDispatcher
‪nullTransportCallsDispatchOfDispatcher()
Definition: TransportFactoryTest.php:263
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeValidSpoolFixture
Definition: FakeValidSpoolFixture.php:28
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192
‪TYPO3\CMS\Core\Tests\Unit\Mail\TransportFactoryTest
Definition: TransportFactoryTest.php:43
‪TYPO3\CMS\Core\Mail\TransportFactory
Definition: TransportFactory.php:37