EmailTest.php
13.9 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mime\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\NamedAddress;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Symfony\Component\Mime\Part\Multipart\MixedPart;
use Symfony\Component\Mime\Part\Multipart\RelatedPart;
use Symfony\Component\Mime\Part\TextPart;
class EmailTest extends TestCase
{
public function testSubject()
{
$e = new Email();
$e->subject('Subject');
$this->assertEquals('Subject', $e->getSubject());
}
public function testDate()
{
$e = new Email();
$e->date($d = new \DateTimeImmutable());
$this->assertSame($d, $e->getDate());
}
public function testReturnPath()
{
$e = new Email();
$e->returnPath('fabien@symfony.com');
$this->assertEquals(new Address('fabien@symfony.com'), $e->getReturnPath());
}
public function testSender()
{
$e = new Email();
$e->sender('fabien@symfony.com');
$this->assertEquals(new Address('fabien@symfony.com'), $e->getSender());
$e->sender($fabien = new Address('fabien@symfony.com'));
$this->assertSame($fabien, $e->getSender());
}
public function testFrom()
{
$e = new Email();
$helene = new Address('helene@symfony.com');
$thomas = new NamedAddress('thomas@symfony.com', 'Thomas');
$caramel = new Address('caramel@symfony.com');
$this->assertSame($e, $e->from('fabien@symfony.com', $helene, $thomas));
$v = $e->getFrom();
$this->assertCount(3, $v);
$this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
$this->assertSame($helene, $v[1]);
$this->assertSame($thomas, $v[2]);
$this->assertSame($e, $e->addFrom('lucas@symfony.com', $caramel));
$v = $e->getFrom();
$this->assertCount(5, $v);
$this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
$this->assertSame($helene, $v[1]);
$this->assertSame($thomas, $v[2]);
$this->assertEquals(new Address('lucas@symfony.com'), $v[3]);
$this->assertSame($caramel, $v[4]);
$e = new Email();
$e->addFrom('lucas@symfony.com', $caramel);
$this->assertCount(2, $e->getFrom());
$e = new Email();
$e->from('lucas@symfony.com');
$e->from($caramel);
$this->assertSame([$caramel], $e->getFrom());
}
public function testReplyTo()
{
$e = new Email();
$helene = new Address('helene@symfony.com');
$thomas = new NamedAddress('thomas@symfony.com', 'Thomas');
$caramel = new Address('caramel@symfony.com');
$this->assertSame($e, $e->replyTo('fabien@symfony.com', $helene, $thomas));
$v = $e->getReplyTo();
$this->assertCount(3, $v);
$this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
$this->assertSame($helene, $v[1]);
$this->assertSame($thomas, $v[2]);
$this->assertSame($e, $e->addReplyTo('lucas@symfony.com', $caramel));
$v = $e->getReplyTo();
$this->assertCount(5, $v);
$this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
$this->assertSame($helene, $v[1]);
$this->assertSame($thomas, $v[2]);
$this->assertEquals(new Address('lucas@symfony.com'), $v[3]);
$this->assertSame($caramel, $v[4]);
$e = new Email();
$e->addReplyTo('lucas@symfony.com', $caramel);
$this->assertCount(2, $e->getReplyTo());
$e = new Email();
$e->replyTo('lucas@symfony.com');
$e->replyTo($caramel);
$this->assertSame([$caramel], $e->getReplyTo());
}
public function testTo()
{
$e = new Email();
$helene = new Address('helene@symfony.com');
$thomas = new NamedAddress('thomas@symfony.com', 'Thomas');
$caramel = new Address('caramel@symfony.com');
$this->assertSame($e, $e->to('fabien@symfony.com', $helene, $thomas));
$v = $e->getTo();
$this->assertCount(3, $v);
$this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
$this->assertSame($helene, $v[1]);
$this->assertSame($thomas, $v[2]);
$this->assertSame($e, $e->addTo('lucas@symfony.com', $caramel));
$v = $e->getTo();
$this->assertCount(5, $v);
$this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
$this->assertSame($helene, $v[1]);
$this->assertSame($thomas, $v[2]);
$this->assertEquals(new Address('lucas@symfony.com'), $v[3]);
$this->assertSame($caramel, $v[4]);
$e = new Email();
$e->addTo('lucas@symfony.com', $caramel);
$this->assertCount(2, $e->getTo());
$e = new Email();
$e->to('lucas@symfony.com');
$e->to($caramel);
$this->assertSame([$caramel], $e->getTo());
}
public function testCc()
{
$e = new Email();
$helene = new Address('helene@symfony.com');
$thomas = new NamedAddress('thomas@symfony.com', 'Thomas');
$caramel = new Address('caramel@symfony.com');
$this->assertSame($e, $e->cc('fabien@symfony.com', $helene, $thomas));
$v = $e->getCc();
$this->assertCount(3, $v);
$this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
$this->assertSame($helene, $v[1]);
$this->assertSame($thomas, $v[2]);
$this->assertSame($e, $e->addCc('lucas@symfony.com', $caramel));
$v = $e->getCc();
$this->assertCount(5, $v);
$this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
$this->assertSame($helene, $v[1]);
$this->assertSame($thomas, $v[2]);
$this->assertEquals(new Address('lucas@symfony.com'), $v[3]);
$this->assertSame($caramel, $v[4]);
$e = new Email();
$e->addCc('lucas@symfony.com', $caramel);
$this->assertCount(2, $e->getCc());
$e = new Email();
$e->cc('lucas@symfony.com');
$e->cc($caramel);
$this->assertSame([$caramel], $e->getCc());
}
public function testBcc()
{
$e = new Email();
$helene = new Address('helene@symfony.com');
$thomas = new NamedAddress('thomas@symfony.com', 'Thomas');
$caramel = new Address('caramel@symfony.com');
$this->assertSame($e, $e->bcc('fabien@symfony.com', $helene, $thomas));
$v = $e->getBcc();
$this->assertCount(3, $v);
$this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
$this->assertSame($helene, $v[1]);
$this->assertSame($thomas, $v[2]);
$this->assertSame($e, $e->addBcc('lucas@symfony.com', $caramel));
$v = $e->getBcc();
$this->assertCount(5, $v);
$this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
$this->assertSame($helene, $v[1]);
$this->assertSame($thomas, $v[2]);
$this->assertEquals(new Address('lucas@symfony.com'), $v[3]);
$this->assertSame($caramel, $v[4]);
$e = new Email();
$e->addBcc('lucas@symfony.com', $caramel);
$this->assertCount(2, $e->getBcc());
$e = new Email();
$e->bcc('lucas@symfony.com');
$e->bcc($caramel);
$this->assertSame([$caramel], $e->getBcc());
}
public function testPriority()
{
$e = new Email();
$this->assertEquals(3, $e->getPriority());
$e->priority(1);
$this->assertEquals(1, $e->getPriority());
$e->priority(10);
$this->assertEquals(5, $e->getPriority());
$e->priority(-10);
$this->assertEquals(1, $e->getPriority());
}
public function testGenerateBodyThrowsWhenEmptyBody()
{
$this->expectException(\LogicException::class);
(new Email())->getBody();
}
public function testGetBody()
{
$e = new Email();
$e->setBody($text = new TextPart('text content'));
$this->assertEquals($text, $e->getBody());
}
public function testGenerateBody()
{
$text = new TextPart('text content');
$html = new TextPart('html content', 'utf-8', 'html');
$att = new DataPart($file = fopen(__DIR__.'/Fixtures/mimetypes/test', 'r'));
$img = new DataPart($image = fopen(__DIR__.'/Fixtures/mimetypes/test.gif', 'r'), 'test.gif');
$e = new Email();
$e->text('text content');
$this->assertEquals($text, $e->getBody());
$this->assertEquals('text content', $e->getTextBody());
$e = new Email();
$e->html('html content');
$this->assertEquals($html, $e->getBody());
$this->assertEquals('html content', $e->getHtmlBody());
$e = new Email();
$e->html('html content');
$e->text('text content');
$this->assertEquals(new AlternativePart($text, $html), $e->getBody());
$e = new Email();
$e->html('html content', 'iso-8859-1');
$e->text('text content', 'iso-8859-1');
$this->assertEquals('iso-8859-1', $e->getTextCharset());
$this->assertEquals('iso-8859-1', $e->getHtmlCharset());
$this->assertEquals(new AlternativePart(new TextPart('text content', 'iso-8859-1'), new TextPart('html content', 'iso-8859-1', 'html')), $e->getBody());
$e = new Email();
$e->attach($file);
$e->text('text content');
$this->assertEquals(new MixedPart($text, $att), $e->getBody());
$e = new Email();
$e->attach($file);
$e->html('html content');
$this->assertEquals(new MixedPart($html, $att), $e->getBody());
$e = new Email();
$e->attach($file);
$this->assertEquals(new MixedPart($att), $e->getBody());
$e = new Email();
$e->html('html content');
$e->text('text content');
$e->attach($file);
$this->assertEquals(new MixedPart(new AlternativePart($text, $html), $att), $e->getBody());
$e = new Email();
$e->html('html content');
$e->text('text content');
$e->attach($file);
$e->attach($image, 'test.gif');
$this->assertEquals(new MixedPart(new AlternativePart($text, $html), $att, $img), $e->getBody());
$e = new Email();
$e->text('text content');
$e->attach($file);
$e->attach($image, 'test.gif');
$this->assertEquals(new MixedPart($text, $att, $img), $e->getBody());
$e = new Email();
$e->html($content = 'html content <img src="test.gif">');
$e->text('text content');
$e->attach($file);
$e->attach($image, 'test.gif');
$fullhtml = new TextPart($content, 'utf-8', 'html');
$this->assertEquals(new MixedPart(new AlternativePart($text, $fullhtml), $att, $img), $e->getBody());
$e = new Email();
$e->html($content = 'html content <img src="cid:test.gif">');
$e->text('text content');
$e->attach($file);
$e->attach($image, 'test.gif');
$body = $e->getBody();
$this->assertInstanceOf(MixedPart::class, $body);
$this->assertCount(2, $related = $body->getParts());
$this->assertInstanceOf(RelatedPart::class, $related[0]);
$this->assertEquals($att, $related[1]);
$this->assertCount(2, $parts = $related[0]->getParts());
$this->assertInstanceOf(AlternativePart::class, $parts[0]);
$generatedHtml = $parts[0]->getParts()[1];
$this->assertStringContainsString('cid:'.$parts[1]->getContentId(), $generatedHtml->getBody());
$content = 'html content <img src="cid:test.gif">';
$r = fopen('php://memory', 'r+', false);
fwrite($r, $content);
rewind($r);
$e = new Email();
$e->html($r);
// embedding the same image twice results in one image only in the email
$e->embed($image, 'test.gif');
$e->embed($image, 'test.gif');
$body = $e->getBody();
$this->assertInstanceOf(RelatedPart::class, $body);
// 2 parts only, not 3 (text + embedded image once)
$this->assertCount(2, $parts = $body->getParts());
$this->assertStringMatchesFormat('html content <img src=3D"cid:%s@symfony">', $parts[0]->bodyToString());
}
public function testAttachments()
{
$contents = file_get_contents($name = __DIR__.'/Fixtures/mimetypes/test', 'r');
$att = new DataPart($file = fopen($name, 'r'), 'test');
$inline = (new DataPart($contents, 'test'))->asInline();
$e = new Email();
$e->attach($file, 'test');
$e->embed($contents, 'test');
$this->assertEquals([$att, $inline], $e->getAttachments());
$att = DataPart::fromPath($name, 'test');
$inline = DataPart::fromPath($name, 'test')->asInline();
$e = new Email();
$e->attachFromPath($name);
$e->embedFromPath($name);
$this->assertEquals([$att->bodyToString(), $inline->bodyToString()], array_map(function (DataPart $a) { return $a->bodyToString(); }, $e->getAttachments()));
$this->assertEquals([$att->getPreparedHeaders(), $inline->getPreparedHeaders()], array_map(function (DataPart $a) { return $a->getPreparedHeaders(); }, $e->getAttachments()));
}
public function testSerialize()
{
$r = fopen('php://memory', 'r+', false);
fwrite($r, 'Text content');
rewind($r);
$e = new Email();
$e->from('fabien@symfony.com');
$e->text($r);
$e->html($r);
$name = __DIR__.'/Fixtures/mimetypes/test';
$file = fopen($name, 'r');
$e->attach($file, 'test');
$expected = clone $e;
$n = unserialize(serialize($e));
$this->assertEquals($expected->getHeaders(), $n->getHeaders());
$this->assertEquals($e->getBody(), $n->getBody());
}
}