15
15
use Symfony \Bridge \Twig \Mime \BodyRenderer ;
16
16
use Symfony \Bridge \Twig \Mime \TemplatedEmail ;
17
17
use Symfony \Component \Mime \Exception \InvalidArgumentException ;
18
+ use Symfony \Component \Mime \HtmlToTextConverter \DefaultHtmlToTextConverter ;
19
+ use Symfony \Component \Mime \HtmlToTextConverter \HtmlToTextConverterInterface ;
18
20
use Symfony \Component \Mime \Part \Multipart \AlternativePart ;
19
21
use Twig \Environment ;
20
22
use Twig \Loader \ArrayLoader ;
@@ -27,14 +29,24 @@ public function testRenderTextOnly()
27
29
$ this ->assertEquals ('Text ' , $ email ->getBody ()->bodyToString ());
28
30
}
29
31
30
- public function testRenderHtmlOnly ()
32
+ public function testRenderHtmlOnlyWithDefaultConverter ()
31
33
{
32
- $ html = '<head>head< /head><b>HTML</b><style type="text/css" >css</style> ' ;
33
- $ email = $ this ->prepareEmail (null , $ html );
34
+ $ html = '<head><meta charset="utf-8">< /head><b>HTML</b><style>css</style> ' ;
35
+ $ email = $ this ->prepareEmail (null , $ html, [], new DefaultHtmlToTextConverter () );
34
36
$ body = $ email ->getBody ();
35
37
$ this ->assertInstanceOf (AlternativePart::class, $ body );
36
38
$ this ->assertEquals ('HTML ' , $ body ->getParts ()[0 ]->bodyToString ());
37
- $ this ->assertEquals (str_replace ('= ' , '=3D ' , $ html ), $ body ->getParts ()[1 ]->bodyToString ());
39
+ $ this ->assertEquals (str_replace (['= ' , "\n" ], ['=3D ' , "\r\n" ], $ html ), $ body ->getParts ()[1 ]->bodyToString ());
40
+ }
41
+
42
+ public function testRenderHtmlOnlyWithLeagueConverter ()
43
+ {
44
+ $ html = '<head><meta charset="utf-8"></head><b>HTML</b><style>css</style> ' ;
45
+ $ email = $ this ->prepareEmail (null , $ html );
46
+ $ body = $ email ->getBody ();
47
+ $ this ->assertInstanceOf (AlternativePart::class, $ body );
48
+ $ this ->assertEquals ('**HTML** ' , $ body ->getParts ()[0 ]->bodyToString ());
49
+ $ this ->assertEquals (str_replace (['= ' , "\n" ], ['=3D ' , "\r\n" ], $ html ), $ body ->getParts ()[1 ]->bodyToString ());
38
50
}
39
51
40
52
public function testRenderMultiLineHtmlOnly ()
@@ -50,7 +62,7 @@ public function testRenderMultiLineHtmlOnly()
50
62
$ email = $ this ->prepareEmail (null , $ html );
51
63
$ body = $ email ->getBody ();
52
64
$ this ->assertInstanceOf (AlternativePart::class, $ body );
53
- $ this ->assertEquals ('HTML ' , str_replace (["\r" , "\n" ], '' , $ body ->getParts ()[0 ]->bodyToString ()));
65
+ $ this ->assertEquals ('** HTML** ' , str_replace (["\r" , "\n" ], '' , $ body ->getParts ()[0 ]->bodyToString ()));
54
66
$ this ->assertEquals (str_replace (['= ' , "\n" ], ['=3D ' , "\r\n" ], $ html ), $ body ->getParts ()[1 ]->bodyToString ());
55
67
}
56
68
@@ -121,15 +133,15 @@ public function testRenderedOnceUnserializableContext()
121
133
$ this ->assertEquals ('Text ' , $ email ->getTextBody ());
122
134
}
123
135
124
- private function prepareEmail (?string $ text , ?string $ html , array $ context = []): TemplatedEmail
136
+ private function prepareEmail (?string $ text , ?string $ html , array $ context = [], HtmlToTextConverterInterface $ converter = null ): TemplatedEmail
125
137
{
126
138
$ twig = new Environment (new ArrayLoader ([
127
139
'text ' => $ text ,
128
140
'html ' => $ html ,
129
141
'document.txt ' => 'Some text document... ' ,
130
142
'image.jpg ' => 'Some image data ' ,
131
143
]));
132
- $ renderer = new BodyRenderer ($ twig );
144
+ $ renderer = new BodyRenderer ($ twig, [], $ converter );
133
145
$ email = (new TemplatedEmail ())
134
146
135
147
0 commit comments