blib/lib/Email/Sender/Transport/Test.pm

filter: Statementstmt 100% 47 / 47 Branchbran 91.6% 11 / 12 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 15 / 15 Podpod 12.5% 1 / 8 totaltotal 89.5% 86 / 96 CC 11 SCAR 16.9
CC
11
Cov
97%
send_email
11.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.4
« blib/lib/Email/Sender/Util.pm ↑ Summary blib/lib/Email/Sender/Transport/Mbox.pm »
1 package Email::Sender::Transport::Test 2.603;
2 # ABSTRACT: deliver mail in memory for testing
3  
4 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Email::Sender::Failure::Multi;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Email::Sender::Success::Partial;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use MooX::Types::MooseLike::Base qw(ArrayRef Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #pod =head1 DESCRIPTION
11 #pod
12 #pod This transport is meant for testing email deliveries in memory. It will store
13 #pod a record of any delivery made so that they can be inspected afterward.
14 #pod
15 #pod =for Pod::Coverage recipient_failure delivery_failure
16 #pod
17 #pod By default, the Test transport will not allow partial success and will always
18 #pod succeed. It can be made to fail predictably, however, if it is extended and
19 #pod its C<recipient_failure> or C<delivery_failure> methods are overridden. These
20 #pod methods are called as follows:
21 #pod
22 #pod $self->delivery_failure($email, $envelope);
23 #pod
24 #pod $self->recipient_failure($to);
25 #pod
26 #pod If they return true, the sending will fail. If the transport was created with
27 #pod a true C<allow_partial_success> attribute, recipient failures can cause partial
28 #pod success to be returned.
29 #pod
30 #pod For more flexible failure modes, you can override more aggressively or can use
31 #pod L<Email::Sender::Transport::Failable>.
32 #pod
33 #pod =attr deliveries
34 #pod
35 #pod =for Pod::Coverage clear_deliveries
36 #pod
37 #pod This attribute stores an arrayref of all the deliveries made via the transport.
38 #pod The C<clear_deliveries> method returns a list of them.
39 #pod
40 #pod Each delivery is a hashref, in the following format:
41 #pod
42 #pod {
43 #pod email => $email,
44 #pod envelope => $envelope,
45 #pod successes => \@ok_rcpts,
46 #pod failures => \@failures,
47 #pod }
48 #pod
49 #pod Both successful and failed deliveries are stored.
50 #pod
51 #pod A number of methods related to this attribute are provided:
52 #pod
53 #pod =for :list
54 #pod * delivery_count
55 #pod * clear_deliveries
56 #pod * shift_deliveries
57 #pod
58 #pod =cut
59  
60 5 ▶has allow_partial_success => (is => 'ro', isa => Bool, default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
61  
62 6 ▶sub recipient_failure { }
Subroutine recipient_failure: called CC=1 SCAR=0.0
Pod undocumented
63 6 ▶sub delivery_failure { }
Subroutine delivery_failure: called CC=1 SCAR=0.0
Pod undocumented
64  
65 has deliveries => (
66 isa => ArrayRef,
67 init_arg => undef,
68 6 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 4 is => 'ro',
70 reader => '_deliveries',
71 );
72  
73 6 ▶sub delivery_count { scalar @{ $_[0]->_deliveries } }
Subroutine delivery_count: called CC=1 SCAR=0.0
Pod undocumented
74 8 ▶sub record_delivery { push @{ shift->_deliveries }, @_ }
Subroutine record_delivery: called CC=1 SCAR=0.0
Pod undocumented
75 4 ▶sub deliveries { @{ $_[0]->_deliveries } }
Subroutine deliveries: called CC=1 SCAR=0.0
Pod documented
76 3 ▶sub shift_deliveries { shift @{ $_[0]->_deliveries } }
Subroutine shift_deliveries: called CC=1 SCAR=0.0
Pod undocumented
77 1 ▶sub clear_deliveries { @{ $_[0]->_deliveries } = () }
Subroutine clear_deliveries: called CC=1 SCAR=0.0
Pod undocumented
78  
79 13 ▶sub send_email {
Subroutine send_email: called CC=11 SCAR=24.0
Pod undocumented
80 13 my ($self, $email, $envelope) = @_;
81  
82 13 my @failures;
83 my @ok_rcpts;
84  
85 13 ▶ if (my $failure = $self->delivery_failure($email, $envelope)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $failure = $self->delivery_failure($email, $envelope)) 1 12 13
86 1 $failure->throw;
87 }
88  
89 12 for my $to (@{ $envelope->{to} }) {
90 14 ▶ if (my $failure = $self->recipient_failure($to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $failure = $self->recipient_failure($to)) { } 5 9 14
91 5 push @failures, $failure;
92 } else {
93 9 push @ok_rcpts, $to;
94 }
95 }
96  
97 12 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (@failures and @ok_rcpts == 0 || !$self->allow_partial_success) 4 8 12
Condition100%6/6
@failures and @ok_rcpts == 0 || !$self->allow_partial_success
!ll&&!rl&&r
714
@ok_rcpts == 0 or not $self->allow_partial_success
l!l&&r!l&&!r
311
MC/DC100%3/3
@failures and @ok_rcpts == 0 || !$self->allow_partial_success
@failures @ok_rcpts == 0 not $self->allow_partial_success
Truth table100%4/4
A and B or not(C)
A= @failuresB= @ok_rcpts == 0C= not $self->allow_partial_success
ABCresult
0XX0
1000
11X1
1011
98 @failures
99 and ((@ok_rcpts == 0) or (! $self->allow_partial_success))
100 ) {
101 4 ▶ $failures[0]->throw if @failures == 1 and @ok_rcpts == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @failures == 1 and @ok_rcpts == 0 3 1 4
Condition66%2/3
@failures == 1 and @ok_rcpts == 0
!ll&&!rl&&r
013
MC/DC50%1/2
@failures == 1 and @ok_rcpts == 0
@failures == 1 @ok_rcpts == 0
Truth table66%2/3
A and B
A= @failures == 1B= @ok_rcpts == 0
ABresult
0X0
100
111
102  
103 1 ▶ my $message = sprintf '%s recipients were rejected',
Branch: 1 branch
BranchTrueFalseTotal
@ok_rcpts ? : 1 0 1
104 @ok_rcpts ? 'some' : 'all';
105  
106 1 Email::Sender::Failure::Multi->throw(
107 message => $message,
108 failures => \@failures,
109 );
110 }
111  
112 $self->record_delivery({
113 8 email => $email,
114 envelope => $envelope,
115 successes => \@ok_rcpts,
116 failures => \@failures,
117 });
118  
119 # XXX: We must report partial success (failures) if applicable.
120 8 ▶ return $self->success unless @failures;
Branch: 1 branch
BranchTrueFalseTotal
unless @failures 7 1 8
121 1 return Email::Sender::Success::Partial->new({
122 failure => Email::Sender::Failure::Multi->new({
123 message => 'some recipients were rejected',
124 failures => \@failures
125 }),
126 });
127 }
128  
129 4 with 'Email::Sender::Transport';
130 4 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 4 1;
132  
133 __END__
« blib/lib/Email/Sender/Util.pm ↑ Summary blib/lib/Email/Sender/Transport/Mbox.pm »