| File: | t/module.t |
| Coverage: | 100.0% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | 1 1 1 | 9188 5 38 | use strict; | ||||
| 2 | 1 1 1 | 7 3 31 | use warnings; | ||||
| 3 | 1 1 1 | 467 52066 10324 | use Test::Most; | ||||
| 4 | 1 1 1 | 65558 5725 9 | use Test::MockObject; | ||||
| 5 | 1 1 1 | 489 22785 24 | use IO::All 'io'; | ||||
| 6 | 1 1 1 | 186 12 5298 | use File::Basename 'dirname'; | ||||
| 7 | |||||||
| 8 | 1 1 1 1 1 | 109804 1598 7 4 24 | use_ok('Email::Mailer'); | ||||
| 9 | |||||||
| 10 | 1 | 2670 | my @mail; | ||||
| 11 | Test::MockObject->fake_module( 'Email::Mailer', 'sendmail', sub { | ||||||
| 12 | 9 | 81 | push( @mail, shift ); | ||||
| 13 | 1 | 64 | } ); | ||||
| 14 | |||||||
| 15 | sub file_qr { | ||||||
| 16 | 9 | 1162 | my $qr = io( dirname($0) . '/qr/' . shift )->all; | ||||
| 17 | 9 | 25723 | chomp($qr); | ||||
| 18 | 9 | 376 | $qr =~ s/\r?\n/\\s+/msg; | ||||
| 19 | 9 | 701 | return qr/$qr/ms; | ||||
| 20 | } | ||||||
| 21 | |||||||
| 22 | #------------------------------------------------------------------------------- | ||||||
| 23 | |||||||
| 24 | 1 | 192 | @mail = (); | ||||
| 25 | lives_ok( | ||||||
| 26 | sub { | ||||||
| 27 | 1 | 36 | Email::Mailer->send( | ||||
| 28 | to => 'to@example.com', | ||||||
| 29 | from => 'from@example.com', | ||||||
| 30 | subject => 'Test Email', | ||||||
| 31 | text => 'This is a simple text-only email.', | ||||||
| 32 | ) | ||||||
| 33 | }, | ||||||
| 34 | 1 | 48 | 'Email::Mailer->send(...) text-only email', | ||||
| 35 | ); | ||||||
| 36 | 1 | 434 | is( @mail, 1, '1 mail generated' ); | ||||
| 37 | 1 | 357 | is( ref $mail[0], 'Email::MIME', 'mail object created is Email::MIME' ); | ||||
| 38 | 1 | 220 | like( $mail[0]->as_string, file_qr('text_only.qr'), 'text_only.qr' ); | ||||
| 39 | |||||||
| 40 | #------------------------------------------------------------------------------- | ||||||
| 41 | |||||||
| 42 | 1 | 324 | @mail = (); | ||||
| 43 | lives_ok( | ||||||
| 44 | sub { | ||||||
| 45 | 1 | 36 | Email::Mailer->new->send( | ||||
| 46 | to => 'to@example.com', | ||||||
| 47 | from => 'from@example.com', | ||||||
| 48 | subject => 'Test Email', | ||||||
| 49 | html => q{ | ||||||
| 50 | <p> | ||||||
| 51 | This is a generic message for <b>testing purposes only</b> | ||||||
| 52 | with regard to some stuff and things: | ||||||
| 53 | </p> | ||||||
| 54 | <ul> | ||||||
| 55 | <li>Stuff</li> | ||||||
| 56 | <li>Things</li> | ||||||
| 57 | </ul> | ||||||
| 58 | }, | ||||||
| 59 | ) | ||||||
| 60 | }, | ||||||
| 61 | 1 | 12 | 'Email::Mailer->new->send(...) HTML + auto-text', | ||||
| 62 | ); | ||||||
| 63 | 1 | 415 | is( @mail, 1, '1 mail generated' ); | ||||
| 64 | 1 | 237 | is( ref $mail[0], 'Email::MIME', 'mail object created is Email::MIME' ); | ||||
| 65 | 1 | 249 | like( $mail[0]->as_string, file_qr('html_auto_text.qr'), 'html_auto_text.qr' ); | ||||
| 66 | |||||||
| 67 | #------------------------------------------------------------------------------- | ||||||
| 68 | |||||||
| 69 | 1 | 242 | @mail = (); | ||||
| 70 | lives_ok( | ||||||
| 71 | sub { | ||||||
| 72 | 1 | 56 | Email::Mailer->new( | ||||
| 73 | to => 'to@example.com', | ||||||
| 74 | from => 'from@example.com', | ||||||
| 75 | subject => 'Test Email', | ||||||
| 76 | html => q{ | ||||||
| 77 | <p> | ||||||
| 78 | This is a generic message for <b>testing purposes only</b> | ||||||
| 79 | with regard to some stuff and things: | ||||||
| 80 | </p> | ||||||
| 81 | <img src="} . dirname($0) . q{/blank.gif"> | ||||||
| 82 | <ul> | ||||||
| 83 | <li>Stuff</li> | ||||||
| 84 | <li>Things</li> | ||||||
| 85 | </ul> | ||||||
| 86 | }, | ||||||
| 87 | )->send | ||||||
| 88 | }, | ||||||
| 89 | 1 | 12 | 'Email::Mailer->new(...)->send HTML + auto-text', | ||||
| 90 | ); | ||||||
| 91 | 1 | 360 | is( @mail, 1, '1 mail generated' ); | ||||
| 92 | 1 | 200 | is( ref $mail[0], 'Email::MIME', 'mail object created is Email::MIME' ); | ||||
| 93 | 1 | 182 | like( $mail[0]->as_string, file_qr('html_auto_text_img.qr'), 'html_auto_text_img.qr' ); | ||||
| 94 | |||||||
| 95 | #------------------------------------------------------------------------------- | ||||||
| 96 | |||||||
| 97 | 1 | 310 | @mail = (); | ||||
| 98 | lives_ok( | ||||||
| 99 | sub { | ||||||
| 100 | 1 | 31 | Email::Mailer->new->send( | ||||
| 101 | to => 'to@example.com', | ||||||
| 102 | from => 'from@example.com', | ||||||
| 103 | subject => 'Test Email', | ||||||
| 104 | embed => 0, | ||||||
| 105 | html => q{ | ||||||
| 106 | <p> | ||||||
| 107 | This is a generic message for <b>testing purposes only</b> | ||||||
| 108 | with regard to some stuff and things: | ||||||
| 109 | </p> | ||||||
| 110 | <img src="} . dirname($0) . q{/blank.gif"> | ||||||
| 111 | <ul> | ||||||
| 112 | <li>Stuff</li> | ||||||
| 113 | <li>Things</li> | ||||||
| 114 | </ul> | ||||||
| 115 | }, | ||||||
| 116 | ) | ||||||
| 117 | }, | ||||||
| 118 | 1 | 10 | 'Email::Mailer->new->send(...) HTML + auto-text', | ||||
| 119 | ); | ||||||
| 120 | 1 | 368 | is( @mail, 1, '1 mail generated' ); | ||||
| 121 | 1 | 261 | is( ref $mail[0], 'Email::MIME', 'mail object created is Email::MIME' ); | ||||
| 122 | 1 | 233 | like( $mail[0]->as_string, file_qr('html_auto_text_img_noembed.qr'), 'html_auto_text_img_noembed.qr' ); | ||||
| 123 | |||||||
| 124 | #------------------------------------------------------------------------------- | ||||||
| 125 | |||||||
| 126 | 1 | 378 | @mail = (); | ||||
| 127 | lives_ok( | ||||||
| 128 | sub { | ||||||
| 129 | 1 | 41 | Email::Mailer->send( | ||||
| 130 | to => 'to@example.com', | ||||||
| 131 | from => 'from@example.com', | ||||||
| 132 | subject => 'Test Email', | ||||||
| 133 | text => 'This is a simple text-only email.', | ||||||
| 134 | html => q{ | ||||||
| 135 | <p> | ||||||
| 136 | This is a generic message for <b>testing purposes only</b> | ||||||
| 137 | with regard to some stuff and things: | ||||||
| 138 | </p> | ||||||
| 139 | }, | ||||||
| 140 | ) | ||||||
| 141 | }, | ||||||
| 142 | 1 | 14 | 'Email::Mailer->send HTML + text', | ||||
| 143 | ); | ||||||
| 144 | 1 | 319 | is( @mail, 1, '1 mail generated' ); | ||||
| 145 | 1 | 223 | is( ref $mail[0], 'Email::MIME', 'mail object created is Email::MIME' ); | ||||
| 146 | 1 | 207 | like( $mail[0]->as_string, file_qr('html_text.qr'), 'html_text.qr' ); | ||||
| 147 | |||||||
| 148 | #------------------------------------------------------------------------------- | ||||||
| 149 | |||||||
| 150 | 1 | 273 | @mail = (); | ||||
| 151 | lives_ok( | ||||||
| 152 | sub { | ||||||
| 153 | 1 | 74 | Email::Mailer->send( | ||||
| 154 | to => 'to@example.com', | ||||||
| 155 | from => 'from@example.com', | ||||||
| 156 | subject => 'Test Email', | ||||||
| 157 | text => 'This is a simple text-only email.', | ||||||
| 158 | html => '<p>This is a generic message for <b>testing purposes only</b>.</p>', | ||||||
| 159 | attachments => [ | ||||||
| 160 | { | ||||||
| 161 | ctype => 'image/gif', | ||||||
| 162 | source => dirname($0) . '/blank.gif', | ||||||
| 163 | }, | ||||||
| 164 | { | ||||||
| 165 | ctype => 'image/gif', | ||||||
| 166 | content => io( dirname($0) . '/blank.gif' )->binary->all, | ||||||
| 167 | name => 'blank.gif', | ||||||
| 168 | }, | ||||||
| 169 | ], | ||||||
| 170 | ) | ||||||
| 171 | }, | ||||||
| 172 | 1 | 12 | 'Email::Mailer->send HTML + text + attachments', | ||||
| 173 | ); | ||||||
| 174 | 1 | 340 | is( @mail, 1, '1 mail generated' ); | ||||
| 175 | 1 | 258 | is( ref $mail[0], 'Email::MIME', 'mail object created is Email::MIME' ); | ||||
| 176 | 1 | 181 | like( $mail[0]->as_string, file_qr('html_text_attachments.qr'), 'html_text_attachments.qr' ); | ||||
| 177 | |||||||
| 178 | #------------------------------------------------------------------------------- | ||||||
| 179 | |||||||
| 180 | 1 | 339 | @mail = (); | ||||
| 181 | lives_ok( | ||||||
| 182 | sub { | ||||||
| 183 | 1 | 36 | Email::Mailer->new( | ||||
| 184 | from => 'from@example.com', | ||||||
| 185 | subject => 'Test Email', | ||||||
| 186 | html => '<p>This is a generic message for <b>testing purposes only</b>.</p>', | ||||||
| 187 | )->send( | ||||||
| 188 | { to => 'person_0@example.com' }, | ||||||
| 189 | { | ||||||
| 190 | to => 'person_1@example.com', | ||||||
| 191 | subject => 'Override $subject with this', | ||||||
| 192 | }, | ||||||
| 193 | ) | ||||||
| 194 | }, | ||||||
| 195 | 1 | 12 | 'Email::Mailer->new(...)->send( iterative_send )', | ||||
| 196 | ); | ||||||
| 197 | 1 | 334 | is( @mail, 2, '2 mails generated' ); | ||||
| 198 | 1 | 196 | like( $mail[0]->as_string, file_qr('iterative_send_0.qr'), 'iterative_send_0.qr' ); | ||||
| 199 | 1 | 323 | like( $mail[1]->as_string, file_qr('iterative_send_1.qr'), 'iterative_send_1.qr' ); | ||||
| 200 | |||||||
| 201 | #------------------------------------------------------------------------------- | ||||||
| 202 | |||||||
| 203 | 1 | 500 | @mail = (); | ||||
| 204 | lives_ok( | ||||||
| 205 | sub { | ||||||
| 206 | Email::Mailer->new( | ||||||
| 207 | to => 'to@example.com', | ||||||
| 208 | from => 'from@example.com', | ||||||
| 209 | subject => \'Test Email: [% content %]', | ||||||
| 210 | html => \'<p>This is a generic message: <b>[% content %]</b>.</p>', | ||||||
| 211 | process => sub { | ||||||
| 212 | 2 | 17 | my ( $template, $data ) = @_; | ||||
| 213 | 2 | 23 | $template =~ s/\[%\s*content\s*%\]/$data->{content}/g; | ||||
| 214 | 2 | 10 | return $template; | ||||
| 215 | }, | ||||||
| 216 | 1 | 41 | )->send( to => 'override@example.com', data => { content => 'Process' } ) | ||||
| 217 | }, | ||||||
| 218 | 1 | 12 | 'Email::Mailer->new(...)->send(...) templating', | ||||
| 219 | ); | ||||||
| 220 | 1 | 300 | is( @mail, 1, '1 mail generated' ); | ||||
| 221 | 1 | 185 | like( $mail[0]->as_string, file_qr('templating.qr'), 'templating.qr' ); | ||||
| 222 | |||||||
| 223 | 1 | 216 | done_testing; | ||||