‪TYPO3CMS  11.5
MailerTest.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 Prophecy\PhpUnit\ProphecyTrait;
22 use Psr\Log\LoggerInterface;
23 use Psr\Log\NullLogger;
24 use Symfony\Component\Mailer\Transport\NullTransport;
25 use Symfony\Component\Mailer\Transport\SendmailTransport;
26 use Symfony\Component\Mailer\Transport\TransportInterface;
27 use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
35 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
36 
40 class ‪MailerTest extends UnitTestCase
41 {
42  use ProphecyTrait;
43 
47  protected ‪$resetSingletonInstances = true;
48 
52  protected ‪$subject;
53 
55 
59  protected function ‪setUp(): void
60  {
61  parent::setUp();
62  $this->subject = $this->getMockBuilder(Mailer::class)
63  ->onlyMethods([])
64  ->disableOriginalConstructor()
65  ->getMock();
66  $this->logManager = new class () implements ‪LogManagerInterface {
67  public function getLogger(string $name = ''): LoggerInterface
68  {
69  return new NullLogger();
70  }
71  };
72  }
73 
78  {
79  $settings = ['transport' => 'mbox', 'transport_mbox_file' => '/path/to/file'];
80  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'] = ['transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail -bs'];
81 
82  $transportFactory = $this->prophesize(TransportFactory::class);
83  $transportFactory->get(Argument::any())->willReturn($this->prophesize(SendmailTransport::class));
84  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory->reveal());
85  $this->subject->injectMailSettings($settings);
86  $this->subject->__construct();
87 
88  $transportFactory->get($settings)->shouldHaveBeenCalled();
89  }
90 
95  {
96  $settings = (‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'] = ['transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail -bs']);
97 
98  $transportFactory = $this->prophesize(TransportFactory::class);
99  $transportFactory->get(Argument::any())->willReturn($this->prophesize(SendmailTransport::class));
100  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory->reveal());
101  $this->subject->injectMailSettings($settings);
102  $this->subject->__construct();
103 
104  $transportFactory->get($settings)->shouldHaveBeenCalled();
105  }
106 
112  public static function ‪wrongConfigurationProvider(): array
113  {
114  return [
115  'smtp but no host' => [['transport' => 'smtp']],
116  'mbox but no file' => [['transport' => 'mbox']],
117  'no instance of TransportInterface' => [['transport' => ErrorPageController::class]],
118  ];
119  }
120 
125  public function ‪wrongConfigurationThrowsException(array $settings): void
126  {
127  $this->expectException(Exception::class);
128  $this->expectExceptionCode(1291068569);
129 
130  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
131  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $this->logManager);
132  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
133  $this->subject->injectMailSettings($settings);
134  $this->subject->__construct();
135  }
136 
141  {
142  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
143  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $this->logManager);
144  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
145  $this->subject->injectMailSettings(['transport' => NullTransport::class]);
146  $this->subject->__construct();
147  }
148 
152  public function ‪noPortSettingSetsPortTo25(): void
153  {
154  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
155  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $this->logManager);
156  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
157  $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost']);
158  $this->subject->__construct();
159  $port = $this->subject->getTransport()->getStream()->getPort();
160  self::assertEquals(25, $port);
161  }
162 
166  public function ‪emptyPortSettingSetsPortTo25(): void
167  {
168  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
169  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $this->logManager);
170  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
171  $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost:']);
172  $this->subject->__construct();
173  $port = $this->subject->getTransport()->getStream()->getPort();
174  self::assertEquals(25, $port);
175  }
176 
180  public function ‪givenPortSettingIsRespected(): void
181  {
182  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
183  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $this->logManager);
184  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
185  $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost:12345']);
186  $this->subject->__construct();
187  $port = $this->subject->getTransport()->getStream()->getPort();
188  self::assertEquals(12345, $port);
189  }
190 
195  public function ‪getRealTransportReturnsNoSpoolTransport($settings): void
196  {
197  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
198  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $this->logManager);
199  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
200  $this->subject->injectMailSettings($settings);
201  $transport = $this->subject->getRealTransport();
202 
203  self::assertInstanceOf(TransportInterface::class, $transport);
204  self::assertNotInstanceOf(DelayedTransportInterface::class, $transport);
205  }
206 
212  public static function ‪getRealTransportReturnsNoSpoolTransportProvider(): array
213  {
214  return [
215  'without spool' => [[
216  'transport' => 'sendmail',
217  'spool' => '',
218  ]],
219  'with spool' => [[
220  'transport' => 'sendmail',
221  'spool' => 'memory',
222  ]],
223  ];
224  }
225 }
‪TYPO3\CMS\Core\Controller\ErrorPageController
Definition: ErrorPageController.php:30
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\wrongConfigurationProvider
‪static array wrongConfigurationProvider()
Definition: MailerTest.php:109
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\globalSettingsAreUsedIfNoSettingsAreInjected
‪globalSettingsAreUsedIfNoSettingsAreInjected()
Definition: MailerTest.php:91
‪TYPO3\CMS\Core\Log\LogManagerInterface
Definition: LogManagerInterface.php:24
‪TYPO3\CMS\Core\Tests\Unit\Mail
Definition: FileSpoolTest.php:18
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\$subject
‪Mailer $subject
Definition: MailerTest.php:49
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\noPortSettingSetsPortTo25
‪noPortSettingSetsPortTo25()
Definition: MailerTest.php:149
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest
Definition: MailerTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\$logManager
‪LogManagerInterface $logManager
Definition: MailerTest.php:51
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\emptyPortSettingSetsPortTo25
‪emptyPortSettingSetsPortTo25()
Definition: MailerTest.php:163
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\setUp
‪setUp()
Definition: MailerTest.php:56
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\injectedSettingsAreNotReplacedByGlobalSettings
‪injectedSettingsAreNotReplacedByGlobalSettings()
Definition: MailerTest.php:74
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\getRealTransportReturnsNoSpoolTransport
‪getRealTransportReturnsNoSpoolTransport($settings)
Definition: MailerTest.php:192
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:38
‪TYPO3\CMS\Core\Mail\DelayedTransportInterface
Definition: DelayedTransportInterface.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\providingCorrectClassnameDoesNotThrowException
‪providingCorrectClassnameDoesNotThrowException()
Definition: MailerTest.php:137
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\wrongConfigurationThrowsException
‪wrongConfigurationThrowsException(array $settings)
Definition: MailerTest.php:122
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: MailerTest.php:45
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\getRealTransportReturnsNoSpoolTransportProvider
‪static array getRealTransportReturnsNoSpoolTransportProvider()
Definition: MailerTest.php:209
‪TYPO3\CMS\Core\Mail\TransportFactory
Definition: TransportFactory.php:38
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\givenPortSettingIsRespected
‪givenPortSettingIsRespected()
Definition: MailerTest.php:177