forked from fedeisas/laravel-mail-css-inliner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCssInlinerPluginTest.php
371 lines (298 loc) · 13.1 KB
/
CssInlinerPluginTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
namespace Tests;
use Illuminate\Support\Collection;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Fedeisas\LaravelMailCssInliner\CssInlinerPlugin;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Exception\LogicException;
use Symfony\Component\Mime\Part\AbstractMultipartPart;
use Symfony\Component\Mime\Part\AbstractPart;
use Symfony\Component\Mime\Part\TextPart;
class CssInlinerPluginTest extends TestCase
{
protected array $stubs;
protected array $options;
protected static array $stubDefinitions = [
'converted-html',
'converted-html-with-css',
'converted-html-with-link-css',
'converted-html-with-link-external',
'converted-html-with-link-relative-external',
'converted-html-with-links-css',
'converted-html-with-mixed-type-links',
'converted-html-with-non-stylesheet-link',
'original-html',
'original-html-with-css',
'original-html-with-link-css',
'original-html-with-link-external',
'original-html-with-link-relative-external',
'original-html-with-links-css',
'original-html-with-mixed-type-links',
'original-html-with-non-stylesheet-link',
'plain-text',
];
public function setUp(): void
{
foreach (self::$stubDefinitions as $stub) {
$this->stubs[$stub] = $this->cleanupHtmlStringForComparison(
file_get_contents(__DIR__ . "/stubs/{$stub}.stub")
);
}
}
public function test_it_should_convert_html_body(): void
{
$message = $this->fakeSendMessageUsingInlinePlugin(
(new Email)->html($this->stubs['original-html'])
);
$this->assertBodyMatchesStub($message, 'converted-html');
}
public function test_it_should_convert_html_body_with_attachment(): void
{
$originalMessage = $this->createMessageToSend(
(new Email)->html($this->stubs['original-html']),
__DIR__ . '/stubs/original-html.stub'
);
$message = $this->fakeSendMessageUsingInlinePlugin($originalMessage);
$this->assertBodyMatchesStub($message, 'converted-html');
$this->assertSameMessageStructure($originalMessage, $message);
$this->assertAttachmentsAreIdentical($originalMessage, $message);
}
public function test_it_should_convert_html_body_with_given_css(): void
{
$message = $this->fakeSendMessageUsingInlinePlugin(
(new Email)->html($this->stubs['original-html-with-css']),
[__DIR__ . '/css/test.css']
);
$this->assertBodyMatchesStub($message, 'converted-html-with-css');
}
public function test_it_should_convert_html_body_with_given_css_content(): void
{
$message = $this->fakeSendMessageUsingInlinePlugin(
(new Email)->html($this->stubs['original-html-with-css']),
[],
file_get_contents(__DIR__ . '/css/test.css')
);
$this->assertBodyMatchesStub($message, 'converted-html-with-css');
}
public function test_it_should_convert_html_body_with_given_css_and_attachment(): void
{
$originalMessage = $this->createMessageToSend(
(new Email)->html($this->stubs['original-html-with-css']),
__DIR__ . '/stubs/original-html.stub'
);
$message = $this->fakeSendMessageUsingInlinePlugin(
$originalMessage,
[__DIR__ . '/css/test.css']
);
$this->assertBodyMatchesStub($message, 'converted-html-with-css');
$this->assertSameMessageStructure($originalMessage, $message);
$this->assertAttachmentsAreIdentical($originalMessage, $message);
}
public function test_it_should_convert_html_body_and_text_parts(): void
{
$message = $this->fakeSendMessageUsingInlinePlugin(
(new Email)
->html($this->stubs['original-html'])
->text($this->stubs['plain-text'])
);
$this->assertBodyMatchesStub($message, 'converted-html');
$this->assertBodyMatchesStub($message, 'plain-text', 'plain');
}
public function test_it_should_convert_html_body_and_text_parts_with_attachment(): void
{
$originalMessage = $this->createMessageToSend(
(new Email)->html($this->stubs['original-html'])->text($this->stubs['plain-text']),
__DIR__ . '/stubs/original-html.stub'
);
$message = $this->fakeSendMessageUsingInlinePlugin($originalMessage);
$this->assertBodyMatchesStub($message, 'converted-html');
$this->assertBodyMatchesStub($message, 'plain-text', 'plain');
$this->assertSameMessageStructure($originalMessage, $message);
$this->assertAttachmentsAreIdentical($originalMessage, $message);
}
public function test_it_should_leave_plain_text_unmodified(): void
{
$message = $this->fakeSendMessageUsingInlinePlugin(
(new Email)->text($this->stubs['plain-text'])
);
$this->assertBodyMatchesStub($message, 'plain-text', 'plain');
}
public function test_it_should_leave_plain_text_unmodified_with_attachment(): void
{
$originalMessage = $this->createMessageToSend(
(new Email)->text($this->stubs['plain-text']),
__DIR__ . '/stubs/original-html.stub'
);
$message = $this->fakeSendMessageUsingInlinePlugin($originalMessage);
$this->assertBodyMatchesStub($message, 'plain-text', 'plain');
$this->assertSameMessageStructure($originalMessage, $message);
$this->assertAttachmentsAreIdentical($originalMessage, $message);
}
public function test_it_should_convert_html_body_as_a_part(): void
{
$message = $this->fakeSendMessageUsingInlinePlugin(
(new Email)->html($this->stubs['original-html'])
);
$this->assertBodyMatchesStub($message, 'converted-html');
}
public function test_it_should_convert_html_body_as_a_part_with_attachment(): void
{
$originalMessage = $this->createMessageToSend(
(new Email)->html($this->stubs['original-html']),
__DIR__ . '/stubs/original-html.stub'
);
$message = $this->fakeSendMessageUsingInlinePlugin($originalMessage);
$this->assertBodyMatchesStub($message, 'converted-html');
$this->assertSameMessageStructure($originalMessage, $message);
$this->assertAttachmentsAreIdentical($originalMessage, $message);
}
public function test_it_should_convert_html_body_with_link_css(): void
{
$message = $this->fakeSendMessageUsingInlinePlugin(
(new Email)->html($this->stubs['original-html-with-link-css'])
);
$this->assertBodyMatchesStub($message, 'converted-html-with-css');
}
public function test_it_should_convert_html_body_with_link_css_and_attachment(): void
{
$originalMessage = $this->createMessageToSend(
(new Email)->html($this->stubs['original-html-with-link-css']),
__DIR__ . '/stubs/original-html.stub'
);
$message = $this->fakeSendMessageUsingInlinePlugin($originalMessage);
$this->assertBodyMatchesStub($message, 'converted-html-with-css');
$this->assertSameMessageStructure($originalMessage, $message);
$this->assertAttachmentsAreIdentical($originalMessage, $message);
}
public function test_it_should_convert_html_body_with_links_css(): void
{
$message = $this->fakeSendMessageUsingInlinePlugin(
(new Email)->html($this->stubs['original-html-with-links-css'])
);
$this->assertBodyMatchesStub($message, 'converted-html-with-links-css');
}
public function test_it_should_convert_html_body_with_links_css_and_attachment(): void
{
$originalMessage = $this->createMEssageToSend(
(new Email)->html($this->stubs['original-html-with-links-css']),
__DIR__ . '/stubs/original-html.stub'
);
$message = $this->fakeSendMessageUsingInlinePlugin($originalMessage);
$this->assertBodyMatchesStub($message, 'converted-html-with-links-css');
$this->assertSameMessageStructure($originalMessage, $message);
$this->assertAttachmentsAreIdentical($originalMessage, $message);
}
private function assertBodyMatchesStub(Email $message, string $stub, string $mediaSubType = 'html'): void
{
$body = $message->getBody();
if (! $body instanceof AbstractPart) {
throw new RuntimeException('Unknown message body type');
}
$actual = $this->getTextFromPart($body, $mediaSubType);
if (is_null($actual)) {
throw new RuntimeException("No text found in body with media subtype '$mediaSubType'" );
}
$this->assertEquals($this->stubs[$stub], $this->cleanupHtmlStringForComparison($actual));
}
private function assertSameMessageStructure(Email $expected, Email $actual)
{
$expected_structure = $this->getMessagePartStructure($expected->getBody());
$actual_structure = $this->getMessagePartStructure($actual->getBody());
$this->assertEquals($expected_structure, $actual_structure);
}
private function assertAttachmentsAreIdentical(Email $expected, Email $actual)
{
$expected_attachments = $expected->getAttachments();
$actual_attachments = $actual->getAttachments();
$this->assertGreaterThan(0, count($expected_attachments));
$this->assertSameSize($expected_attachments, $actual_attachments);
for ($i = 0; $i < count($expected_attachments); $i++) {
$this->assertEquals($expected_attachments[$i]->getBody(), $actual_attachments[$i]->getBody());
}
}
private function getMessagePartStructure(AbstractPart $part): array|string
{
$structure = [];
$partClass = get_class($part);
if (! $part instanceof AbstractMultipartPart) {
$structure[] = $partClass;
} else {
$structure[$partClass] = [];
foreach ($part->getParts() as $childPart) {
$structure[$partClass][] = $this->getMessagePartStructure($childPart);
}
}
if (count($structure, COUNT_RECURSIVE) === 1) {
$structure = $structure[0];
}
return $structure;
}
private function cleanupHtmlStringForComparison(string $string): string
{
// Strip out all newlines and trim newlines from the start and end
$string = str_replace("\n", '', trim($string));
// Strip out any whitespace between HTML tags
return preg_replace('/(>)\s+(<\/?[a-z]+)/', '$1$2', $string);
}
private function getTextFromPart(AbstractPart $part, string $mediaSubType = 'html'): ?string
{
if ($part instanceof TextPart && $part->getMediaType() === 'text' && $part->getMediaSubtype() === $mediaSubType) {
return $part->getBody();
} elseif ($part instanceof AbstractMultipartPart) {
foreach ($part->getParts() as $childPart) {
$text = $this->getTextFromPart($childPart, $mediaSubType);
if (! is_null($text)) {
return $text;
}
}
}
return null;
}
private function fakeSendMessageUsingInlinePlugin(Email $message, array $inlineCssFiles = [], string $inlineCssContent = null): Email
{
$processedMessage = null;
$dispatcher = new EventDispatcher;
$dispatcher->addListener(MessageEvent::class, static function (MessageEvent $event) use ($inlineCssFiles, $inlineCssContent, &$processedMessage) {
$handler = new CssInlinerPlugin([
'css-files' => $inlineCssFiles,
'css-content' => $inlineCssContent,
]);
$handler->handleSymfonyEvent($event);
$processedMessage = $event->getMessage();
});
$mailer = new Mailer(
Transport::fromDsn('null://default', $dispatcher)
);
// Check if the message is valid (has to, cc and bcc set). If not, we create a valid message.
try {
$message->ensureValidity();
} catch (LogicException) {
$message = $this->createMessageToSend($message);
}
try {
$mailer->send($message);
} catch (TransportExceptionInterface) {
// We are not really expecting anything to happen here considering it's a `NullTransport` we are using :)
}
if (!$processedMessage instanceof Email) {
throw new RuntimeException('No email was processed!');
}
return $processedMessage;
}
private function createMessageToSend(Email $message, string $attachmentPath = null): Email
{
$message = $message->to('[email protected]')
->from('[email protected]')
->subject('Test');
if (! is_null($attachmentPath)) {
$message = $message->attachFromPath($attachmentPath);
}
return $message;
}
}