‪TYPO3CMS  10.4
MailMessageTest.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 Symfony\Component\Mime\Address;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪MailMessageTest extends UnitTestCase
28 {
32  protected ‪$resetSingletonInstances = true;
33 
37  protected ‪$subject;
38 
42  protected function ‪setUp(): void
43  {
44  parent::setUp();
45  $this->subject = new ‪MailMessage();
46  }
47 
51  public function ‪isSentReturnsFalseIfMailWasNotSent(): void
52  {
53  self::assertFalse($this->subject->isSent());
54  }
55 
59  public function ‪setSubjectWorksAsExpected(): void
60  {
61  $this->subject->setSubject('Test');
62  self::assertSame('Test', $this->subject->getSubject());
63  $this->subject->setSubject('Test2');
64  self::assertSame('Test2', $this->subject->getSubject());
65  }
66 
70  public function ‪setDateWorksAsExpected(): void
71  {
72  $time = time();
73  $this->subject->setDate($time);
74  self::assertSame($time, (int)$this->subject->getDate()->format('U'));
75  $time++;
76  $this->subject->setDate($time);
77  self::assertSame($time, (int)$this->subject->getDate()->format('U'));
78  }
79 
83  public function ‪setReturnPathWorksAsExpected(): void
84  {
85  $this->subject->setReturnPath('noreply@typo3.com');
86  self::assertInstanceOf(Address::class, $this->subject->getReturnPath());
87  self::assertSame('noreply@typo3.com', $this->subject->getReturnPath()->getAddress());
88  $this->subject->setReturnPath('no-reply@typo3.com');
89  self::assertInstanceOf(Address::class, $this->subject->getReturnPath());
90  self::assertSame('no-reply@typo3.com', $this->subject->getReturnPath()->getAddress());
91  }
92 
93  public function ‪setSenderAddressDataProvider(): array
94  {
95  return [
96  'address without name' => [
97  'admin@typo3.com', null, [
98  ['admin@typo3.com']
99  ]
100  ],
101  'address with name' => [
102  'admin@typo3.com', 'Admin', [
103  ['admin@typo3.com', 'Admin', '<admin@typo3.com>']
104  ]
105  ],
106  ];
107  }
108 
116  public function ‪setSenderWorksAsExpected($address, $name, array $expectedAddresses): void
117  {
118  $this->subject->setSender($address, $name);
119  self::assertInstanceOf(Address::class, $this->subject->getSender());
120  self::assertSame($address, $this->subject->getSender()->getAddress());
121  $this->‪assertCorrectAddresses([$this->subject->getSender()], $expectedAddresses);
122  }
123 
124  public function ‪globalSetAddressDataProvider(): array
125  {
126  return [
127  'address without name' => [
128  'admin@typo3.com', null, [
129  ['admin@typo3.com']
130  ]
131  ],
132  'address with name' => [
133  'admin@typo3.com', 'Admin', [
134  ['admin@typo3.com', 'Admin', '<admin@typo3.com>']
135  ]
136  ],
137  'address with name enclosed in quotes' => [
138  'admin@typo3.com', '"Admin"', [
139  ['admin@typo3.com', 'Admin', '<admin@typo3.com>']
140  ]
141  ],
142  'multiple addresses without name' => [
143  [
144  'admin@typo3.com',
145  'system@typo3.com'
146  ], null, [
147  ['admin@typo3.com'],
148  ['system@typo3.com']
149  ]
150  ],
151  'address as array' => [
152  ['admin@typo3.com' => 'Admin'], null, [
153  ['admin@typo3.com', 'Admin', '<admin@typo3.com>']
154  ]
155  ],
156  'multiple addresses as array' => [
157  [
158  'admin@typo3.com' => 'Admin',
159  'system@typo3.com' => 'System'
160  ], null, [
161  ['admin@typo3.com', 'Admin', '<admin@typo3.com>'],
162  ['system@typo3.com', 'System', '<system@typo3.com>']
163  ]
164  ],
165  'multiple addresses as array mixed' => [
166  [
167  'admin@typo3.com' => 'Admin',
168  'it@typo3.com',
169  'system@typo3.com' => 'System'
170  ], null, [
171  ['admin@typo3.com', 'Admin', '<admin@typo3.com>'],
172  ['it@typo3.com'],
173  ['system@typo3.com', 'System', '<system@typo3.com>']
174  ]
175  ],
176  ];
177  }
178 
186  public function ‪setFromWorksAsExpected($address, $name, array $expectedAddresses): void
187  {
188  // We first add one address, because set should override / remove existing addresses
189  $this->subject->addFrom('foo@bar.com', 'Foo');
190  $this->subject->setFrom($address, $name);
191  $this->‪assertCorrectAddresses($this->subject->getFrom(), $expectedAddresses);
192  }
193 
201  public function ‪setReplyToWorksAsExpected($address, $name, array $expectedAddresses): void
202  {
203  // We first add one address, because set should override / remove existing addresses
204  $this->subject->addReplyTo('foo@bar.com', 'Foo');
205  $this->subject->setReplyTo($address, $name);
206  $this->‪assertCorrectAddresses($this->subject->getReplyTo(), $expectedAddresses);
207  }
208 
216  public function ‪setToToWorksAsExpected($address, $name, array $expectedAddresses): void
217  {
218  // We first add one address, because set should override / remove existing addresses
219  $this->subject->addTo('foo@bar.com', 'Foo');
220  $this->subject->setTo($address, $name);
221  $this->‪assertCorrectAddresses($this->subject->getTo(), $expectedAddresses);
222  }
223 
231  public function ‪setCcToWorksAsExpected($address, $name, array $expectedAddresses): void
232  {
233  // We first add one address, because set should override / remove existing addresses
234  $this->subject->addCc('foo@bar.com', 'Foo');
235  $this->subject->setCc($address, $name);
236  $this->‪assertCorrectAddresses($this->subject->getCc(), $expectedAddresses);
237  }
238 
246  public function ‪setBccToWorksAsExpected($address, $name, array $expectedAddresses): void
247  {
248  // We first add one address, because set should override / remove existing addresses
249  $this->subject->addBcc('foo@bar.com', 'Foo');
250  $this->subject->setBcc($address, $name);
251  $this->‪assertCorrectAddresses($this->subject->getBcc(), $expectedAddresses);
252  }
253 
254  public function ‪globalAddAddressDataProvider(): array
255  {
256  return [
257  'address without name' => [
258  'admin@typo3.com', null, [
259  ['admin@typo3.com']
260  ]
261  ],
262  'address with name' => [
263  'admin@typo3.com', 'Admin', [
264  ['admin@typo3.com', 'Admin', '<admin@typo3.com>']
265  ]
266  ],
267  'address as array' => [
268  ['admin@typo3.com' => 'Admin'], null, [
269  ['admin@typo3.com', 'Admin', '<admin@typo3.com>']
270  ]
271  ],
272  ];
273  }
274 
282  public function ‪addFromToWorksAsExpected($address, $name, array $expectedAddresses): void
283  {
284  $this->subject->addFrom($address, $name);
285  $this->‪assertCorrectAddresses($this->subject->getFrom(), $expectedAddresses);
286  }
287 
295  public function ‪addReplyToToWorksAsExpected($address, $name, array $expectedAddresses): void
296  {
297  $this->subject->addReplyTo($address, $name);
298  $this->‪assertCorrectAddresses($this->subject->getReplyTo(), $expectedAddresses);
299  }
300 
308  public function ‪addToToWorksAsExpected($address, $name, array $expectedAddresses): void
309  {
310  $this->subject->addTo($address, $name);
311  $this->‪assertCorrectAddresses($this->subject->getTo(), $expectedAddresses);
312  }
313 
321  public function ‪addCcToWorksAsExpected($address, $name, array $expectedAddresses): void
322  {
323  $this->subject->addCc($address, $name);
324  $this->‪assertCorrectAddresses($this->subject->getCc(), $expectedAddresses);
325  }
326 
334  public function ‪addBccToWorksAsExpected($address, $name, array $expectedAddresses): void
335  {
336  $this->subject->addBcc($address, $name);
337  $this->‪assertCorrectAddresses($this->subject->getBcc(), $expectedAddresses);
338  }
339 
343  public function ‪setReadReceiptToToWorksAsExpected(): void
344  {
345  $this->subject->setReadReceiptTo('foo@example.com');
346  self::assertSame('foo@example.com', $this->subject->getHeaders()->get('Disposition-Notification-To')->getAddress()->getAddress());
347  }
348 
350  {
351  return [
352  'setFrom' => ['setFrom'],
353  'setReplyTo' => ['setReplyTo'],
354  'setTo' => ['setTo'],
355  'setCc' => ['setCc'],
356  'setBcc' => ['setBcc'],
357  ];
358  }
359 
365  public function ‪exceptionIsThrownForInvalidArgumentCombinations(string $method): void
366  {
367  $this->expectException(\InvalidArgumentException::class);
368  $this->expectExceptionCode(1570543657);
369  $this->subject->{$method}(['foo@example.com'], 'A name');
370  }
371 
379  protected function assertCorrectAddresses(array $dataToCheck, array $expectedAddresses): void
380  {
381  self::assertIsArray($dataToCheck);
382  self::assertCount(count($expectedAddresses), $dataToCheck);
383  foreach ($expectedAddresses as $key => $expectedAddress) {
384  self::assertIsArray($expectedAddress);
385  self::assertSame($expectedAddress[0], $dataToCheck[$key]->getAddress());
386  foreach ($expectedAddress as $expectedAddressPart) {
387  self::assertStringContainsString($expectedAddressPart, $dataToCheck[$key]->toString());
388  }
389  }
390  }
391 }
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setFromWorksAsExpected
‪setFromWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:184
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\globalSetAddressDataProvider
‪globalSetAddressDataProvider()
Definition: MailMessageTest.php:122
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: MailMessageTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\addFromToWorksAsExpected
‪addFromToWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:280
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\$subject
‪MailMessage $subject
Definition: MailMessageTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Mail
Definition: FileSpoolTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setSenderAddressDataProvider
‪setSenderAddressDataProvider()
Definition: MailMessageTest.php:91
‪TYPO3\CMS\Core\Mail\MailMessage
Definition: MailMessage.php:28
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\addBccToWorksAsExpected
‪addBccToWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:332
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setSenderWorksAsExpected
‪setSenderWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:114
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setDateWorksAsExpected
‪setDateWorksAsExpected()
Definition: MailMessageTest.php:68
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\addReplyToToWorksAsExpected
‪addReplyToToWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:293
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setUp
‪setUp()
Definition: MailMessageTest.php:40
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setReplyToWorksAsExpected
‪setReplyToWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:199
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\addCcToWorksAsExpected
‪addCcToWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:319
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setSubjectWorksAsExpected
‪setSubjectWorksAsExpected()
Definition: MailMessageTest.php:57
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\addToToWorksAsExpected
‪addToToWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:306
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setReturnPathWorksAsExpected
‪setReturnPathWorksAsExpected()
Definition: MailMessageTest.php:81
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\globalAddAddressDataProvider
‪globalAddAddressDataProvider()
Definition: MailMessageTest.php:252
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setCcToWorksAsExpected
‪setCcToWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:229
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setReadReceiptToToWorksAsExpected
‪setReadReceiptToToWorksAsExpected()
Definition: MailMessageTest.php:341
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\exceptionIsThrownForInvalidArgumentCombinationsDataProvider
‪exceptionIsThrownForInvalidArgumentCombinationsDataProvider()
Definition: MailMessageTest.php:347
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\isSentReturnsFalseIfMailWasNotSent
‪isSentReturnsFalseIfMailWasNotSent()
Definition: MailMessageTest.php:49
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest
Definition: MailMessageTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\assertCorrectAddresses
‪assertCorrectAddresses(array $dataToCheck, array $expectedAddresses)
Definition: MailMessageTest.php:377
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\exceptionIsThrownForInvalidArgumentCombinations
‪exceptionIsThrownForInvalidArgumentCombinations(string $method)
Definition: MailMessageTest.php:363
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setBccToWorksAsExpected
‪setBccToWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:244
‪TYPO3\CMS\Core\Tests\Unit\Mail\MailMessageTest\setToToWorksAsExpected
‪setToToWorksAsExpected($address, $name, array $expectedAddresses)
Definition: MailMessageTest.php:214