15
15
use Symfony \Bridge \Twig \Mime \BodyRenderer ;
16
16
use Symfony \Bridge \Twig \Mime \TemplatedEmail ;
17
17
use Symfony \Component \Mime \Part \Multipart \AlternativePart ;
18
- use Symfony \Component \Mime \Part \Multipart \MixedPart ;
19
- use Symfony \Component \Mime \Part \Multipart \RelatedPart ;
20
- use Symfony \Component \Mime \Part \TextPart ;
21
18
use Twig \Environment ;
22
19
use Twig \Loader \ArrayLoader ;
23
20
24
21
class RendererTest extends TestCase
25
22
{
26
23
public function testRenderTextOnly (): void
27
24
{
28
- $ email = $ this ->prepareEmail (null , 'Text ' , null );
25
+ $ email = $ this ->prepareEmail ('Text ' , null );
29
26
$ this ->assertEquals ('Text ' , $ email ->getBody ()->bodyToString ());
30
27
}
31
28
32
29
public function testRenderHtmlOnly (): void
33
30
{
34
- $ email = $ this ->prepareEmail (null , null , '<b>HTML</b> ' );
31
+ $ email = $ this ->prepareEmail (null , '<b>HTML</b> ' );
35
32
$ body = $ email ->getBody ();
36
33
$ this ->assertInstanceOf (AlternativePart::class, $ body );
37
34
$ this ->assertEquals ('HTML ' , $ body ->getParts ()[0 ]->bodyToString ());
@@ -40,7 +37,7 @@ public function testRenderHtmlOnly(): void
40
37
41
38
public function testRenderHtmlOnlyWithTextSet (): void
42
39
{
43
- $ email = $ this ->prepareEmail (null , null , '<b>HTML</b> ' );
40
+ $ email = $ this ->prepareEmail (null , '<b>HTML</b> ' );
44
41
$ email ->text ('Text ' );
45
42
$ body = $ email ->getBody ();
46
43
$ this ->assertInstanceOf (AlternativePart::class, $ body );
@@ -50,118 +47,23 @@ public function testRenderHtmlOnlyWithTextSet(): void
50
47
51
48
public function testRenderTextAndHtml (): void
52
49
{
53
- $ email = $ this ->prepareEmail (null , 'Text ' , '<b>HTML</b> ' );
50
+ $ email = $ this ->prepareEmail ('Text ' , '<b>HTML</b> ' );
54
51
$ body = $ email ->getBody ();
55
52
$ this ->assertInstanceOf (AlternativePart::class, $ body );
56
53
$ this ->assertEquals ('Text ' , $ body ->getParts ()[0 ]->bodyToString ());
57
54
$ this ->assertEquals ('<b>HTML</b> ' , $ body ->getParts ()[1 ]->bodyToString ());
58
55
}
59
56
60
- public function testRenderFullOnly (): void
61
- {
62
- $ email = $ this ->prepareEmail (<<<EOF
63
- {% block subject %}Subject{% endblock %}
64
- {% block text %}Text{% endblock %}
65
- {% block html %}<b>HTML</b>{% endblock %}
66
- EOF
67
- , null , null );
68
- $ body = $ email ->getBody ();
69
- $ this ->assertInstanceOf (AlternativePart::class, $ body );
70
- $ this ->assertEquals ('Subject ' , $ email ->getSubject ());
71
- $ this ->assertEquals ('Text ' , $ body ->getParts ()[0 ]->bodyToString ());
72
- $ this ->assertEquals ('<b>HTML</b> ' , $ body ->getParts ()[1 ]->bodyToString ());
73
- }
74
-
75
- public function testRenderFullOnlyWithTextOnly (): void
76
- {
77
- $ email = $ this ->prepareEmail (<<<EOF
78
- {% block text %}Text{% endblock %}
79
- EOF
80
- , null , null );
81
- $ body = $ email ->getBody ();
82
- $ this ->assertInstanceOf (TextPart::class, $ body );
83
- $ this ->assertEquals ('' , $ email ->getSubject ());
84
- $ this ->assertEquals ('Text ' , $ body ->bodyToString ());
85
- }
86
-
87
- public function testRenderFullOnlyWithHtmlOnly (): void
88
- {
89
- $ email = $ this ->prepareEmail (<<<EOF
90
- {% block html %}<b>HTML</b>{% endblock %}
91
- EOF
92
- , null , null );
93
- $ body = $ email ->getBody ();
94
- $ this ->assertInstanceOf (AlternativePart::class, $ body );
95
- $ this ->assertEquals ('' , $ email ->getSubject ());
96
- $ this ->assertEquals ('HTML ' , $ body ->getParts ()[0 ]->bodyToString ());
97
- $ this ->assertEquals ('<b>HTML</b> ' , $ body ->getParts ()[1 ]->bodyToString ());
98
- }
99
-
100
- public function testRenderFullAndText (): void
101
- {
102
- $ email = $ this ->prepareEmail (<<<EOF
103
- {% block text %}Text full{% endblock %}
104
- {% block html %}<b>HTML</b>{% endblock %}
105
- EOF
106
- , 'Text ' , null );
107
- $ body = $ email ->getBody ();
108
- $ this ->assertInstanceOf (AlternativePart::class, $ body );
109
- $ this ->assertEquals ('Text ' , $ body ->getParts ()[0 ]->bodyToString ());
110
- $ this ->assertEquals ('<b>HTML</b> ' , $ body ->getParts ()[1 ]->bodyToString ());
111
- }
112
-
113
- public function testRenderFullAndHtml (): void
114
- {
115
- $ email = $ this ->prepareEmail (<<<EOF
116
- {% block text %}Text full{% endblock %}
117
- {% block html %}<b>HTML</b>{% endblock %}
118
- EOF
119
- , null , '<i>HTML</i> ' );
120
- $ body = $ email ->getBody ();
121
- $ this ->assertInstanceOf (AlternativePart::class, $ body );
122
- $ this ->assertEquals ('Text full ' , $ body ->getParts ()[0 ]->bodyToString ());
123
- $ this ->assertEquals ('<i>HTML</i> ' , $ body ->getParts ()[1 ]->bodyToString ());
124
- }
125
-
126
- public function testRenderHtmlWithEmbeddedImages (): void
127
- {
128
- $ email = $ this ->prepareEmail (null , null , '<img src="{{ email.image("image.jpg") }}" /> ' );
129
- $ body = $ email ->getBody ();
130
- $ this ->assertInstanceOf (RelatedPart::class, $ body );
131
- $ this ->assertInstanceOf (AlternativePart::class, $ body ->getParts ()[0 ]);
132
- $ this ->assertStringMatchesFormat ('<img src=3D"cid:%s@symfony" /> ' , $ body ->getParts ()[0 ]->getParts ()[1 ]->bodyToString ());
133
- $ this ->assertEquals ('Some image data ' , base64_decode ($ body ->getParts ()[1 ]->bodyToString ()));
134
- }
135
-
136
- public function testRenderFullWithAttachments (): void
137
- {
138
- $ email = $ this ->prepareEmail (<<<EOF
139
- {% block text %}Text{% endblock %}
140
- {% block config %}
141
- {% do email.attach('document.txt') %}
142
- {% endblock %}
143
- EOF
144
- , null , null );
145
- $ body = $ email ->getBody ();
146
- $ this ->assertInstanceOf (MixedPart::class, $ body );
147
- $ this ->assertEquals ('Text ' , $ body ->getParts ()[0 ]->bodyToString ());
148
- $ this ->assertEquals ('Some text document... ' , base64_decode ($ body ->getParts ()[1 ]->bodyToString ()));
149
- }
150
-
151
- private function prepareEmail (?string $ full , ?string $ text , ?string $ html ): TemplatedEmail
57
+ private function prepareEmail (?string $ text , ?string $ html ): TemplatedEmail
152
58
{
153
59
$ twig = new Environment (new ArrayLoader ([
154
- 'full ' => $ full ,
155
60
'text ' => $ text ,
156
61
'html ' => $ html ,
157
62
'document.txt ' => 'Some text document... ' ,
158
63
'image.jpg ' => 'Some image data ' ,
159
64
]));
160
65
$ renderer = new BodyRenderer ($ twig );
161
66
$ email = (new TemplatedEmail ())->to ('fabien@symfony.com ' )->from ('helene@symfony.com ' );
162
- if (null !== $ full ) {
163
- $ email ->template ('full ' );
164
- }
165
67
if (null !== $ text ) {
166
68
$ email ->textTemplate ('text ' );
167
69
}
0 commit comments