-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
BodyRendererTest.php
89 lines (79 loc) · 2.97 KB
/
BodyRendererTest.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
<?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\Bridge\Twig\Tests\Mime;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Exception\InvalidArgumentException;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
class BodyRendererTest extends TestCase
{
public function testRenderTextOnly(): void
{
$email = $this->prepareEmail('Text', null);
$this->assertEquals('Text', $email->getBody()->bodyToString());
}
public function testRenderHtmlOnly(): void
{
$html = '<head>head</head><b>HTML</b><style type="text/css">css</style>';
$email = $this->prepareEmail(null, $html);
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
$this->assertEquals('HTML', $body->getParts()[0]->bodyToString());
$this->assertEquals(str_replace('=', '=3D', $html), $body->getParts()[1]->bodyToString());
}
public function testRenderHtmlOnlyWithTextSet(): void
{
$email = $this->prepareEmail(null, '<b>HTML</b>');
$email->text('Text');
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
$this->assertEquals('Text', $body->getParts()[0]->bodyToString());
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
}
public function testRenderTextAndHtml(): void
{
$email = $this->prepareEmail('Text', '<b>HTML</b>');
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
$this->assertEquals('Text', $body->getParts()[0]->bodyToString());
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
}
public function testRenderWithContextReservedEmailEntry(): void
{
$this->expectException(InvalidArgumentException::class);
$this->prepareEmail('Text', '', ['email' => 'reserved!']);
}
private function prepareEmail(?string $text, ?string $html, array $context = []): TemplatedEmail
{
$twig = new Environment(new ArrayLoader([
'text' => $text,
'html' => $html,
'document.txt' => 'Some text document...',
'image.jpg' => 'Some image data',
]));
$renderer = new BodyRenderer($twig);
$email = (new TemplatedEmail())
->to('fabien@symfony.com')
->from('helene@symfony.com')
->context($context)
;
if (null !== $text) {
$email->textTemplate('text');
}
if (null !== $html) {
$email->htmlTemplate('html');
}
$renderer->render($email);
return $email;
}
}