33use Openbuildings \Swiftmailer \CssInlinerPlugin ;
44
55/**
6- * @group css-inliner-plugin
6+ * @group css-inliner-plugin
77 */
8- class CssInlinerPluginTest extends PHPUnit_Framework_TestCase {
9-
10- public $ html = <<<HTML
11- <html>
12- <head>
13- <style>
14- .block {
15- width: 100px;
16- height: 20px;
17- }
18- div.block ul li.small {
19- margin: 10px;
20- }
21- </style>
22- </head>
23- <body>
24- <div class="block">
25- text
26-
27- <ul>
28- <li>
29- Big list
30- </li>
31- <li class="small">
32- Small list
33- </li>
34- </ul>
35- </div>
36- </body>
37- </html>
38- HTML ;
39-
40- public $ converted_html = <<<CONVERTED
41- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
42- <html><head><style>
43- .block {
44- width: 100px;
45- height: 20px;
46- }
47- div.block ul li.small {
48- margin: 10px;
49- }
50- </style></head><body>
51- <div class="block" style="height: 20px; width: 100px;">
52- text
53-
54- <ul><li>
55- Big list
56- </li>
57- <li class="small" style="margin: 10px;">
58- Small list
59- </li>
60- </ul></div>
61- </body></html>
62-
63- CONVERTED ;
8+ class CssInlinerPluginTest extends PHPUnit_Framework_TestCase
9+ {
10+ /**
11+ * @var Swift_Mailer
12+ */
13+ private $ mailer ;
14+
15+ /**
16+ * @var string
17+ */
18+ private $ email_raw ;
19+
20+ /**
21+ * @var string
22+ */
23+ private $ email_converted ;
6424
6525 public function test_html_body ()
6626 {
67- $ mailer = Swift_Mailer:: newInstance (Swift_NullTransport:: newInstance () );
27+ $ message = $ this -> create_message ( );
6828
69- $ mailer ->registerPLugin (new CssInlinerPlugin ());
29+ $ message ->setContentType ('text/html ' );
30+ $ message ->setBody ($ this ->email_raw );
7031
71- $ message = Swift_Message:: newInstance ( );
32+ $ this -> mailer -> send ( $ message );
7233
73- $ message->
setFrom (
'[email protected] ' );
74- $ message->
setTo (
'[email protected] ' );
75- $ message ->setSubject ('Test ' );
76- $ message ->setContentType ('text/html ' );
77- $ message ->setBody ($ this ->html );
34+ $ this ->assertEquals ($ this ->email_converted , $ message ->getBody ());
35+ }
7836
79- $ mailer ->send ($ message );
80-
81- $ this ->assertEquals ($ this ->converted_html , $ message ->getBody ());
37+ public function test_html_part ()
38+ {
39+ $ message = $ this ->create_message ();
40+
41+ $ message ->addPart ($ this ->email_raw , 'text/html ' );
42+ $ message ->addPart ('plain part ' , 'text/plain ' );
43+
44+ $ this ->mailer ->send ($ message );
45+
46+ $ children = $ message ->getChildren ();
47+
48+ $ this ->assertEquals ($ this ->email_converted , $ children [0 ]->getBody ());
8249 }
8350
51+ public function test_default_converter_uses_inline_styles_block ()
52+ {
53+ $ plugin = new CssInlinerPlugin ();
8454
85- public function test_html_part ()
55+ $ converter = \PHPUnit_Framework_Assert::readAttribute ($ plugin , 'converter ' );
56+
57+ $ this ->assertTrue (
58+ \PHPUnit_Framework_Assert::readAttribute ($ converter , 'useInlineStylesBlock ' ),
59+ 'setUseInlineStylesBlock() should be called on default $converter '
60+ );
61+ }
62+
63+ public function test_injected_conveter_is_used_istead_of_default ()
8664 {
65+ $ converterStub = $ this
66+ ->getMockBuilder ('TijsVerkoyen\CssToInlineStyles\CssToInlineStyles ' )
67+ ->setMethods (array ('convert ' , 'setUseInlineStylesBlock ' ))
68+ ->getMock ();
69+
70+ // "our" converter should be used
71+ $ converterStub
72+ ->expects ($ this ->atLeastOnce ())
73+ ->method ('convert ' );
74+
75+ $ converterStub
76+ ->expects ($ this ->never ())
77+ ->method ('setUseInlineStylesBlock ' );
78+
79+ $ message = $ this ->create_message ();
80+ $ message ->setContentType ('text/html ' );
81+
8782 $ mailer = Swift_Mailer::newInstance (Swift_NullTransport::newInstance ());
83+ $ mailer ->registerPlugin (new CssInlinerPlugin ($ converterStub ));
84+ $ mailer ->send ($ message );
85+ }
8886
89- $ mailer ->registerPLugin (new CssInlinerPlugin ());
87+ protected function setUp ()
88+ {
89+ $ dir = __DIR__ .'/../fixtures/ ' ;
90+
91+ $ this ->email_raw = file_get_contents ($ dir .'email_raw.html ' );
92+ $ this ->email_converted = file_get_contents ($ dir .'email_converted.html ' );
9093
94+ $ this ->mailer = Swift_Mailer::newInstance (Swift_NullTransport::newInstance ());
95+ $ this ->mailer ->registerPLugin (new CssInlinerPlugin ());
96+ }
97+
98+ /**
99+ * @return Swift_Message
100+ */
101+ private function create_message ()
102+ {
91103 $ message = Swift_Message::newInstance ();
92104
93105 $ message->
setFrom (
'[email protected] ' );
94106 $ message->
setTo (
'[email protected] ' );
95107 $ message ->setSubject ('Test ' );
96- $ message ->addPart ($ this ->html , 'text/html ' );
97- $ message ->addPart ('plain part ' , 'text/plain ' );
98-
99- $ mailer ->send ($ message );
100108
101- $ children = $ message ->getChildren ();
102-
103- $ this ->assertEquals ($ this ->converted_html , $ children [0 ]->getBody ());
109+ return $ message ;
104110 }
105- }
111+ }
0 commit comments