| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Jifty::Test::Email; | ||
| 5 | use Test::More; | ||
| 6 | use Test::Email; | ||
| 7 | use Email::Abstract; | ||
| 8 | |||
| 9 | use base 'Exporter'; | ||
| 10 | our @EXPORT = qw(mail_ok); | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Jifty::Test::Email - Test mail notification | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use Jifty::Test::Email; | ||
| 19 | |||
| 20 | mail_ok { | ||
| 21 | # ... code | ||
| 22 | } { from => 'admin@localhost', body => qr('hello') }, | ||
| 23 | { from => 'admin@localhost', body => qr('hello again') }; | ||
| 24 | |||
| 25 | # ... more code | ||
| 26 | |||
| 27 | # XXX: not yet | ||
| 28 | mail_sent_ok { from => 'admin@localhost', body => qr('hello') }; | ||
| 29 | |||
| 30 | # you should expect all mails by the end of the test | ||
| 31 | |||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | This is a test helper module for jifty, allowing you to expect mail | ||
| 36 | notification generated during the block or the test. | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | =head2 mail_ok BLOCK HASHREF [, HASHREF ...] | ||
| 41 | |||
| 42 | Executes the C<BLOCK>, and expects it to send a number of emails equal | ||
| 43 | to the number of C<HASHREF>s after the C<BLOCK>. L<Test::Email/ok> is | ||
| 44 | used to check if the messages match up to the appropriate hashrefs. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub mail_ok (&@) { | ||
| 49 | my $code = shift; | ||
| 50 | # XXX. ensure mailbox is empty; but make sure the test count is correct | ||
| 51 | $code->(); | ||
| 52 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 53 | my @msgs = Jifty::Test->messages; | ||
| 54 | is(@msgs, @_, "Sent exactly " . @_ . " emails"); | ||
| 55 | |||
| 56 | for my $spec (@_) { | ||
| 57 | my $msg = shift @msgs | ||
| 58 | or ok(0, 'Expecting message but none found.'), next; | ||
| 59 | |||
| 60 | my $te = Email::Abstract->new($msg)->cast('MIME::Entity'); | ||
| 61 | bless $te, 'Test::Email'; | ||
| 62 | $te->ok($spec, "email matched") or diag $msg->as_string; | ||
| 63 | } | ||
| 64 | Jifty::Test->setup_mailbox; | ||
| 65 | } | ||
| 66 | |||
| 67 | =head1 ENDING TESTS | ||
| 68 | |||
| 69 | An END block causes the test to C<die> if there are uncaught | ||
| 70 | notifications at the end of the test script. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | END { | ||
| 75 | my $Test = Jifty::Test->builder; | ||
| 76 | # Such a hack -- try to detect if this is a forked copy and don't | ||
| 77 | # do cleanup in that case. | ||
| 78 | return if $Test->{Original_Pid} != $$; | ||
| 79 | |||
| 80 | if (scalar Jifty::Test->messages) { | ||
| 81 | diag ((scalar Jifty::Test->messages)." uncaught notification email at end of test: "); | ||
| 82 | diag "From: @{[ $_->header('From' ) ]}, Subject: @{[ $_->header('Subject') ]}" | ||
| 83 | for Jifty::Test->messages; | ||
| 84 | die; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 |