| 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<Email::Send> 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<should> 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 | 8 | ▶ | use 5.005; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 69 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 70 | |||||||||||
| 71 | 8 | ▶ | use vars qw{$VERSION}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 72 | BEGIN { | ||||||||||
| 73 | 8 | ▶ | $VERSION = '2.201'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | # No longer allow direct access to the array | ||||||||||
| 77 | 8 | my @DELIVERIES = (); | |||||||||
| 78 | |||||||||||
| 79 | # This mailer is always available | ||||||||||
| 80 | 9 | ▶ | sub is_available { 1 } | ||||||||
|
Subroutine
is_available: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 81 | |||||||||||
| 82 | =pod | ||||||||||
| 83 | |||||||||||
| 84 | =head2 send $message | ||||||||||
| 85 | |||||||||||
| 86 | As for every other L<Email::Send> mailer, C<send> 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<undef> it ends up as is in the trap. In this | ||||||||||
| 94 | manner, you can see B<exactly> 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 | ▶ | my ($self, $email, @rest) = @_; | ||||||||
|
Subroutine
send: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 105 | |||||||||||
| 106 | 8 | push @DELIVERIES, [ $self, $email, \@rest ]; | |||||||||
| 107 | 8 | return 1; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | =pod | ||||||||||
| 111 | |||||||||||
| 112 | =head2 emails | ||||||||||
| 113 | |||||||||||
| 114 | The C<emails> 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 | ▶ | return scalar @DELIVERIES unless wantarray; | ||||||||
|
Subroutine
emails: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 125 | 4 | return map { $_->[1] } @DELIVERIES; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | =pod | ||||||||||
| 129 | |||||||||||
| 130 | =head2 clear | ||||||||||
| 131 | |||||||||||
| 132 | The C<clear> 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 | ▶ | @DELIVERIES = (); | ||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 144 | 3 | return 1; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | =head2 deliveries | ||||||||||
| 148 | |||||||||||
| 149 | This method returns a list of arrayrefs, one for each call to C<send> 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<send> was called. The second is | ||||||||||
| 155 | the email that was given to C<send>. The third is the rest of the arguments | ||||||||||
| 156 | given to C<send>. | ||||||||||
| 157 | |||||||||||
| 158 | =cut | ||||||||||
| 159 | |||||||||||
| 160 | sub deliveries { | ||||||||||
| 161 | @DELIVERIES | ||||||||||
| 162 | 2 | ▶ | } | ||||||||
|
Subroutine
deliveries: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 163 | |||||||||||
| 164 | 8 | 1; | |||||||||
| 165 | |||||||||||
| 166 | =pod | ||||||||||
| 167 | |||||||||||
| 168 | =head1 SUPPORT | ||||||||||
| 169 | |||||||||||
| 170 | All bugs should be filed via the CPAN bug tracker at | ||||||||||
| 171 | |||||||||||
| 172 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Email-Send-Test> | ||||||||||
| 173 | |||||||||||
| 174 | For other issues, or commercial enhancement or support, contact the author. | ||||||||||
| 175 | |||||||||||
| 176 | =head1 AUTHORS | ||||||||||
| 177 | |||||||||||
| 178 | Current maintainer: Ricardo SIGNES, <F<rjbs@cpan.org>>. | ||||||||||
| 179 | |||||||||||
| 180 | Original author: Adam Kennedy E<lt>cpan@ali.asE<gt>, L<http://ali.as/> | ||||||||||
| 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 | ||||||||||