@@ -13,39 +13,18 @@ function setUp() {
1313 $ this ->mailer = new SparkPostHTTPMailer ();
1414 }
1515
16- function test_mailer_is_a_mailer_instance () {
17- $ this -> assertTrue ( $ this ->mailer instanceof \PHPMailer );
18- }
16+ function test_mailSend_calls_sparkpost_send () {
17+ $ stub = Mockery:: mock ( $ this ->mailer );
18+ $ stub -> shouldReceive ( ' sparkpost_send ' )-> andReturn ( ' woowoo ' );
1919
20- function test_recipients_list () {
20+ $ this ->assertTrue (NSA ::invokeMethod ($ stub , 'mailSend ' , null , null ) == 'woowoo ' );
21+ }
2122
22- $ this ->mailer ->addAddress ('abc@xyz.com ' , 'abc ' );
23- $ this ->mailer ->addAddress ('def@xyz.com ' , 'def ' );
24- $ this ->mailer ->addAddress ('noname@xyz.com ' );
25- $ prepared_list = array (
26- array (
27- 'address ' => array (
28- 'email ' => 'abc@xyz.com ' ,
29- 'name ' => 'abc ' ,
30- )
31- ),
32- array (
33- 'address ' => array (
34- 'name ' => 'def ' ,
35- 'email ' => 'def@xyz.com '
36- )
37- ),
38- array (
39- 'address ' => array (
40- 'email ' => 'noname@xyz.com ' ,
41- 'name ' => ''
42- )
43- )
44- );
45- $ this ->assertTrue (NSA ::invokeMethod ($ this ->mailer , 'get_recipients ' ) == $ prepared_list );
23+ function test_mailer_is_a_mailer_instance () {
24+ $ this ->assertTrue ( $ this ->mailer instanceof \PHPMailer );
4625 }
4726
48- function test_sender_with_name () {
27+ function test_get_sender_with_name () {
4928 $ this ->mailer ->setFrom ( 'me@hello.com ' , 'me ' );
5029 $ sender = array (
5130 'name ' => 'me ' ,
@@ -55,7 +34,7 @@ function test_sender_with_name() {
5534 $ this ->assertTrue (NSA ::invokeMethod ($ this ->mailer , 'get_sender ' ) == $ sender );
5635 }
5736
58- function test_sender_without_name () {
37+ function test_get_sender_without_name () {
5938 $ this ->mailer ->setFrom ( 'me@hello.com ' , '' );
6039 $ sender = array (
6140 'email ' => 'me@hello.com '
@@ -131,4 +110,205 @@ function test_get_headers_should_include_cc_if_exists() {
131110
132111 $ this ->assertTrue ($ formatted_headers == $ expected );
133112 }
113+
114+ function test_get_recipients () {
115+ $ this ->mailer ->addAddress ('to@abc.com ' );
116+ $ this ->mailer ->addAddress ('to1@abc.com ' , 'to1 ' );
117+ $ this ->mailer ->addCc ('cc@abc.com ' );
118+ $ this ->mailer ->addCc ('cc1@abc.com ' , 'cc1 ' );
119+ $ this ->mailer ->addBcc ('bcc@abc.com ' );
120+ $ this ->mailer ->addBcc ('bcc1@abc.com ' , 'bcc1 ' );
121+
122+ $ header_to = implode (', ' , [
123+ 'to@abc.com ' ,
124+ 'to1 <to1@abc.com> ' ,
125+ ]);
126+
127+ $ expected = [
128+ [
129+ 'address ' => [
130+ 'email ' => 'to@abc.com ' ,
131+ 'header_to ' => $ header_to
132+ ]
133+ ],
134+ [
135+ 'address ' => [
136+ 'email ' => 'to1@abc.com ' ,
137+ 'header_to ' => $ header_to
138+ ]
139+ ],
140+ [
141+ 'address ' => [
142+ 'email ' => 'bcc@abc.com ' ,
143+ 'header_to ' => $ header_to
144+ ]
145+ ],
146+ [
147+ 'address ' => [
148+ 'email ' => 'bcc1@abc.com ' ,
149+ 'header_to ' => $ header_to
150+ ]
151+ ],
152+ [
153+ 'address ' => [
154+ 'email ' => 'cc@abc.com ' ,
155+ 'header_to ' => $ header_to
156+ ]
157+ ],
158+ [
159+ 'address ' => [
160+ 'email ' => 'cc1@abc.com ' ,
161+ 'header_to ' => $ header_to
162+ ]
163+ ]
164+ ];
165+
166+ $ recipients = NSA ::invokeMethod ($ this ->mailer , 'get_recipients ' );
167+ $ this ->assertTrue ($ recipients == $ expected );
168+ }
169+
170+ function test_get_attachments () {
171+ $ temp = tempnam ('/tmp ' , 'php-wordpress-sparkpost ' );
172+ file_put_contents ($ temp , 'TEST ' );
173+ $ this ->mailer ->addAttachment ($ temp );
174+ $ attachments = NSA ::invokeMethod ($ this ->mailer , 'get_attachments ' );
175+ $ this ->assertTrue ($ attachments [0 ]['type ' ] === 'application/octet-stream ' );
176+ $ this ->assertTrue ($ attachments [0 ]['name ' ] === basename ($ temp ));
177+ $ this ->assertTrue ($ attachments [0 ]['data ' ] === base64_encode ('TEST ' ));
178+ unlink ($ temp );
179+ }
180+
181+ function test_isMail () {
182+ // test if isMail sets correct mailer
183+ $ this ->mailer ->Mailer = 'abc ' ;
184+ $ this ->assertTrue ($ this ->mailer ->Mailer === 'abc ' );
185+ $ this ->mailer ->isMail ();
186+ $ this ->assertTrue ($ this ->mailer ->Mailer === 'sparkpost ' );
187+ }
188+
189+ function test_get_request_body_without_template () {
190+ // WITHOUT TEMPLATE
191+ $ this ->mailer ->addAddress ('abc@xyz.com ' , 'abc ' );
192+ $ this ->mailer ->addBcc ('bcc@xyz.com ' , 'bcc ' );
193+ $ this ->mailer ->addCc ('cc@xyz.com ' , 'cc ' );
194+ $ this ->mailer ->setFrom ( 'me@hello.com ' , 'me ' );
195+
196+ NSA ::setProperty ($ this ->mailer , 'settings ' , [
197+ 'enable_tracking ' => true ,
198+ 'transactional ' => false
199+ ]);
200+
201+ $ header_to = 'abc <abc@xyz.com> ' ;
202+ $ expected_request_body = [
203+ 'recipients ' => [
204+ [
205+ 'address ' => [
206+ 'email ' => 'abc@xyz.com ' ,
207+ 'header_to ' => $ header_to
208+ ]
209+ ],
210+ [
211+ 'address ' => [
212+ 'email ' => 'bcc@xyz.com ' ,
213+ 'header_to ' => $ header_to
214+ ]
215+ ],
216+ [
217+ 'address ' => [
218+ 'email ' => 'cc@xyz.com ' ,
219+ 'header_to ' => $ header_to
220+ ]
221+ ]
222+ ],
223+ 'options ' => [
224+ 'open_tracking ' => (bool ) true ,
225+ 'click_tracking ' => (bool ) true ,
226+ 'transactional ' => (bool ) false
227+ ],
228+ 'content ' => [
229+ 'from ' => [
230+ 'name ' => 'me ' ,
231+ 'email ' =>'me@hello.com '
232+ ],
233+ 'subject ' => '' ,
234+ 'headers ' => [],
235+ 'text ' => ''
236+ ]
237+ ];
238+
239+ $ actual = NSA ::invokeMethod ($ this ->mailer , 'get_request_body ' );
240+ // for simpler expectation reset content.headers to empty array.
241+ // alternative is to stub get_headers which isn't working expectedly
242+ $ actual ['content ' ]['headers ' ] = [];
243+ $ this ->assertTrue ($ expected_request_body == $ actual );
244+
245+ //INCLUDE REPLYTO
246+ $ this ->mailer ->addReplyTo ('reply@abc.com ' , 'reply-to ' );
247+ $ this ->mailer ->addCustomHeader ('Reply-To ' , 'reply-to <reply@abc.com> ' ); //for below version v4.6
248+ $ actual = NSA ::invokeMethod ($ this ->mailer , 'get_request_body ' );
249+ $ actual ['content ' ]['headers ' ] = []; //see note above
250+ $ expected_request_body ['content ' ]['reply_to ' ] = 'reply-to <reply@abc.com> ' ;
251+ $ this ->assertTrue ($ expected_request_body == $ actual );
252+ }
253+
254+ function test_get_request_body_with_template () {
255+ $ this ->mailer ->addAddress ('abc@xyz.com ' , 'abc ' );
256+ $ this ->mailer ->addBcc ('bcc@xyz.com ' , 'bcc ' );
257+ $ this ->mailer ->addCc ('cc@xyz.com ' , 'cc ' );
258+ $ this ->mailer ->setFrom ( 'me@hello.com ' , 'me ' );
259+ $ header_to = 'abc <abc@xyz.com> ' ;
260+ NSA ::setProperty ($ this ->mailer , 'settings ' , [
261+ 'enable_tracking ' => true ,
262+ 'transactional ' => false ,
263+ 'template ' => 'hello '
264+ ]);
265+
266+ $ expected_request_body = [
267+ 'recipients ' => [
268+ [
269+ 'address ' => [
270+ 'email ' => 'abc@xyz.com ' ,
271+ 'header_to ' => $ header_to
272+ ]
273+ ],
274+ [
275+ 'address ' => [
276+ 'email ' => 'bcc@xyz.com ' ,
277+ 'header_to ' => $ header_to
278+ ]
279+ ],
280+ [
281+ 'address ' => [
282+ 'email ' => 'cc@xyz.com ' ,
283+ 'header_to ' => $ header_to
284+ ]
285+ ]
286+ ],
287+ 'options ' => [
288+ 'open_tracking ' => (bool ) true ,
289+ 'click_tracking ' => (bool ) true ,
290+ 'transactional ' => (bool ) false
291+ ],
292+ 'content ' => [
293+ 'template_id ' => 'hello ' ,
294+ ],
295+ 'substitution_data ' => [
296+ 'content ' => '' ,
297+ 'subject ' => '' ,
298+ 'from_name ' => 'me ' ,
299+ 'from ' => 'me <me@hello.com> ' ,
300+ 'from_localpart ' => 'me '
301+ ]
302+ ];
303+
304+ $ actual = NSA ::invokeMethod ($ this ->mailer , 'get_request_body ' );
305+ $ this ->assertTrue ($ expected_request_body == $ actual );
306+
307+ //INCLUDE REPLYTO
308+ $ this ->mailer ->addReplyTo ('reply@abc.com ' , 'reply-to ' );
309+ $ this ->mailer ->addCustomHeader ('Reply-To ' , 'reply-to <reply@abc.com> ' ); //for below version v4.6
310+ $ actual = NSA ::invokeMethod ($ this ->mailer , 'get_request_body ' );
311+ $ expected_request_body ['substitution_data ' ]['reply_to ' ] = 'reply-to <reply@abc.com> ' ;
312+ $ this ->assertTrue ($ expected_request_body == $ actual );
313+ }
134314}
0 commit comments