‪TYPO3CMS  10.4
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 Symfony\Component\Mailer\Transport\NullTransport;
22 use Symfony\Component\Mailer\Transport\SendmailTransport;
23 use Symfony\Component\Mailer\Transport\TransportInterface;
24 use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
37 class ‪MailerTest extends UnitTestCase
38 {
42  protected ‪$resetSingletonInstances = true;
43 
47  protected ‪$subject;
48 
52  protected function ‪setUp(): void
53  {
54  parent::setUp();
55  $this->subject = $this->getMockBuilder(Mailer::class)
56  ->setMethods(null)
57  ->disableOriginalConstructor()
58  ->getMock();
59  }
60 
65  {
66  $settings = ['transport' => 'mbox', 'transport_mbox_file' => '/path/to/file'];
67  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'] = ['transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail -bs'];
68 
69  $transportFactory = $this->prophesize(TransportFactory::class);
70  $transportFactory->get(Argument::any())->willReturn($this->prophesize(SendmailTransport::class));
71  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory->reveal());
72  $this->subject->injectMailSettings($settings);
73  $this->subject->__construct();
74 
75  $transportFactory->get($settings)->shouldHaveBeenCalled();
76  }
77 
82  {
83  $settings = (‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'] = ['transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail -bs']);
84 
85  $transportFactory = $this->prophesize(TransportFactory::class);
86  $transportFactory->get(Argument::any())->willReturn($this->prophesize(SendmailTransport::class));
87  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory->reveal());
88  $this->subject->injectMailSettings($settings);
89  $this->subject->__construct();
90 
91  $transportFactory->get($settings)->shouldHaveBeenCalled();
92  }
93 
99  public static function ‪wrongConfigurationProvider()
100  {
101  return [
102  'smtp but no host' => [['transport' => 'smtp']],
103  'mbox but no file' => [['transport' => 'mbox']],
104  'no instance of TransportInterface' => [['transport' => ErrorPageController::class]]
105  ];
106  }
107 
113  public function ‪wrongConfigurationThrowsException($settings)
114  {
115  $this->expectException(Exception::class);
116  $this->expectExceptionCode(1291068569);
117 
118  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
119  $logManager = $this->prophesize(LogManagerInterface::class);
120  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $logManager->reveal());
121  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
122  $this->subject->injectMailSettings($settings);
123  $this->subject->__construct();
124  }
125 
130  {
131  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
132  $logManager = $this->prophesize(LogManagerInterface::class);
133  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $logManager->reveal());
134  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
135  $this->subject->injectMailSettings(['transport' => NullTransport::class]);
136  $this->subject->__construct();
137  }
138 
142  public function ‪noPortSettingSetsPortTo25()
143  {
144  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
145  $logManager = $this->prophesize(LogManagerInterface::class);
146  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $logManager->reveal());
147  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
148  $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost']);
149  $this->subject->__construct();
150  $port = $this->subject->getTransport()->getStream()->getPort();
151  self::assertEquals(25, $port);
152  }
153 
157  public function ‪emptyPortSettingSetsPortTo25()
158  {
159  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
160  $logManager = $this->prophesize(LogManagerInterface::class);
161  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $logManager->reveal());
162  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
163  $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost:']);
164  $this->subject->__construct();
165  $port = $this->subject->getTransport()->getStream()->getPort();
166  self::assertEquals(25, $port);
167  }
168 
172  public function ‪givenPortSettingIsRespected()
173  {
174  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
175  $logManager = $this->prophesize(LogManagerInterface::class);
176  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $logManager->reveal());
177  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
178  $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost:12345']);
179  $this->subject->__construct();
180  $port = $this->subject->getTransport()->getStream()->getPort();
181  self::assertEquals(12345, $port);
182  }
183 
188  public function ‪getRealTransportReturnsNoSpoolTransport($settings)
189  {
190  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
191  $logManager = $this->prophesize(LogManagerInterface::class);
192  $transportFactory = new ‪TransportFactory($eventDispatcher->reveal(), $logManager->reveal());
193  GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory);
194  $this->subject->injectMailSettings($settings);
195  $transport = $this->subject->getRealTransport();
196 
197  self::assertInstanceOf(TransportInterface::class, $transport);
198  self::assertNotInstanceOf(DelayedTransportInterface::class, $transport);
199  }
200 
207  {
208  return [
209  'without spool' => [[
210  'transport' => 'sendmail',
211  'spool' => '',
212  ]],
213  'with spool' => [[
214  'transport' => 'sendmail',
215  'spool' => 'memory',
216  ]],
217  ];
218  }
219 }
‪TYPO3\CMS\Core\Controller\ErrorPageController
Definition: ErrorPageController.php:32
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\wrongConfigurationProvider
‪static array wrongConfigurationProvider()
Definition: MailerTest.php:97
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\globalSettingsAreUsedIfNoSettingsAreInjected
‪globalSettingsAreUsedIfNoSettingsAreInjected()
Definition: MailerTest.php:79
‪TYPO3\CMS\Core\Log\LogManagerInterface
Definition: LogManagerInterface.php:22
‪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:45
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\noPortSettingSetsPortTo25
‪noPortSettingSetsPortTo25()
Definition: MailerTest.php:140
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest
Definition: MailerTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\wrongConfigurationThrowsException
‪wrongConfigurationThrowsException($settings)
Definition: MailerTest.php:111
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\emptyPortSettingSetsPortTo25
‪emptyPortSettingSetsPortTo25()
Definition: MailerTest.php:155
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\setUp
‪setUp()
Definition: MailerTest.php:50
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\injectedSettingsAreNotReplacedByGlobalSettings
‪injectedSettingsAreNotReplacedByGlobalSettings()
Definition: MailerTest.php:62
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\getRealTransportReturnsNoSpoolTransport
‪getRealTransportReturnsNoSpoolTransport($settings)
Definition: MailerTest.php:186
‪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:5
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\providingCorrectClassnameDoesNotThrowException
‪providingCorrectClassnameDoesNotThrowException()
Definition: MailerTest.php:127
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: MailerTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\getRealTransportReturnsNoSpoolTransportProvider
‪static array getRealTransportReturnsNoSpoolTransportProvider()
Definition: MailerTest.php:204
‪TYPO3\CMS\Core\Mail\TransportFactory
Definition: TransportFactory.php:37
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailerTest\givenPortSettingIsRespected
‪givenPortSettingIsRespected()
Definition: MailerTest.php:170