File Coverage

blib/lib/Email/Send/Test.pm
Criterion Covered Total %
statement 20 20 100.0
branch 2 2 100.0
condition n/a
subroutine 9 9 100.0
pod 4 5 80.0
total 35 36 97.2


line stmt bran cond sub pod time code
1             package Email::Send::Test;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Email::Send::Test - Captures emails sent via Email::Send for testing
8              
9             =head1 SYNOPSIS
10              
11             # Load as normal
12             use Email::Send;
13             use Email::Send::Test;
14            
15             # Always clear the email trap before each test to prevent unexpected
16             # results, and thus spurious test results.
17             Email::Send::Test->clear;
18            
19             ### BEGIN YOUR CODE TO BE TESTED (example follows)
20             my $sender = Email::Send->new({ mailer => 'Test' });
21             $sender->send( $message );
22             ### END YOUR CODE TO BE TESTED
23            
24             # Check that the number and type (and content) of mails
25             # matched what you expect.
26             my @emails = Email::Send::Test->emails;
27             is( scalar(@emails), 1, 'Sent 1 email' );
28             isa_ok( $emails[0], 'Email::MIME' ); # Email::Simple subclasses pass through
29              
30             =head1 DESCRIPTION
31              
32             Email::Send::Test is a driver for use in testing applications that use
33             L to send email.
34              
35             To be able to use it in testing, you will need some sort of configuration
36             mechanism to specify the delivery method to be used, or some other way
37             that in your testing scripts you can convince your code to use "Test" as
38             the mailer, rather than "Sendmail" or another real mailer.
39              
40             =head2 How does it Work
41              
42             Email::Send::Test is a trap for emails. When an email is sent, it adds the
43             emails to an internal array without doing anything at all to them, and
44             returns success to the caller.
45              
46             If your application sends one email, there will be one in the trap. If you
47             send 20, there will be 20, and so on.
48              
49             A typical test will involve doing running some code that B result
50             in an email being sent, and then checking in the trap to see if the
51             code did actually send out the email.
52              
53             If you want you can get the emails out the trap and examine them. If you
54             only care that something got sent you can simply clear the trap and move
55             on to your next test.
56              
57             =head2 The Email Trap
58              
59             The email trap is a simple array fills with whatever is sent.
60              
61             When you send an email, it is pushed onto the end of the array. You can
62             access the array directly if you wish, or use the methods provided.
63              
64             =head1 METHODS
65              
66             =cut
67              
68 6     6   22125 use 5.005;
  6         19  
  6         234  
69 6     6   29 use strict;
  6         22  
  6         211  
70              
71 6     6   29 use vars qw{$VERSION};
  6         10  
  6         469  
72             BEGIN {
73 6     6   1205 $VERSION = '2.199';
74             }
75              
76             # No longer allow direct access to the array
77             my @DELIVERIES = ();
78              
79             # This mailer is always available
80 9     9 0 25 sub is_available { 1 }
81              
82             =pod
83              
84             =head2 send $message
85              
86             As for every other L mailer, C takes the message to be
87             sent.
88              
89             However, in our case there are no arguments of any value to us, and so they
90             are ignored.
91              
92             It is worth nothing that we do NOTHING to check or alter the email. For
93             example, if we are passed C it ends up as is in the trap. In this
94             manner, you can see B what was sent without any possible tampering
95             on the part of the testing mailer.
96              
97             Of course, this doesn't prevent any tampering by Email::Send itself :)
98              
99             Always returns true.
100              
101             =cut
102              
103             sub send {
104 8     8 1 460 my ($self, $email, @rest) = @_;
105              
106 8         64 push @DELIVERIES, [ $self, $email, \@rest ];
107 8         77 return 1;
108             }
109              
110             =pod
111              
112             =head2 emails
113              
114             The C method is the preferred and recommended method of getting
115             access to the email trap.
116              
117             In list context, returns the content of the trap array as a list.
118              
119             In scalar context, returns the number of items in the trap.
120              
121             =cut
122              
123             sub emails {
124 8 100   8 1 294 return scalar @DELIVERIES unless wantarray;
125 4         10 return map { $_->[1] } @DELIVERIES;
  6         23  
126             }
127              
128             =pod
129              
130             =head2 clear
131              
132             The C method resets the trap, emptying it.
133              
134             It is recommended you always clear the trap before each
135             test to ensure any existing emails are removed and don't
136             create a spurious test result.
137              
138             Always returns true.
139              
140             =cut
141              
142             sub clear {
143 3     3 1 31 @DELIVERIES = ();
144 3         18 return 1;
145             }
146              
147             =head2 deliveries
148              
149             This method returns a list of arrayrefs, one for each call to C that has
150             been made. Each arrayref is in the form:
151              
152             [ $mailer, $email, \@rest ]
153              
154             The first element is the invocant on which C was called. The second is
155             the email that was given to C. The third is the rest of the arguments
156             given to C.
157              
158             =cut
159              
160             sub deliveries {
161             @DELIVERIES
162 2     2 1 17 }
163              
164             1;
165              
166             =pod
167              
168             =head1 SUPPORT
169              
170             All bugs should be filed via the CPAN bug tracker at
171              
172             L
173              
174             For other issues, or commercial enhancement or support, contact the author.
175              
176             =head1 AUTHORS
177              
178             Current maintainer: Ricardo SIGNES, >.
179              
180             Original author: Adam Kennedy Ecpan@ali.asE, L
181              
182             =head1 COPYRIGHT
183              
184             Copyright (c) 2004 - 2005 Adam Kennedy. All rights reserved.
185             This program is free software; you can redistribute
186             it and/or modify it under the same terms as Perl itself.
187              
188             The full text of the license can be found in the
189             LICENSE file included with this module.
190              
191             =cut