aboutsummaryrefslogtreecommitdiff
path: root/t/lib/Test/Sietima.pm
blob: e61640a0d6c3ca188e190d962a27d9aa114f1e06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package Test::Sietima; 
use strict;
use warnings;
use 5.020;
use lib 't/lib';
use Import::Into;
use Email::Stuffer;
use Email::Sender::Transport::Test;
use Data::Printer;
use Sietima;
use Test2::Bundle::Extended;
use Test2::Tools::MoreCompare qw(bag);
use Test2::API qw(context);
use namespace::clean;
 
sub import {
    my $target = caller;
    Test2::Bundle::Extended->import::into($target);
    Test2::Plugin::DieOnFail->import::into($target);
    Test2::Tools::MoreCompare->import::into($target,qw(bag));
    for my $function (qw(transport make_sietima make_mail
                         deliveries_are test_sending)) {
        no strict 'refs';
        *{"${target}::${function}"} = __PACKAGE__->can($function);
    }
    return;
}
 
my $return_path = 'sietima-test@list.example.com';
 
sub transport {
    state $transport = Email::Sender::Transport::Test->new;
    return $transport;
}
 
sub make_sietima {
    my (%args) = @_;
 
    my $class = 'Sietima';
    if (my $traits = delete $args{with_traits}) {
        $class = $class->with_traits(@{$traits});
    }
 
    $class->new({
        return_path => $return_path,
        %args,
        transport => transport(),
    });
}
 
sub make_mail {
    my (%args) = @_;
 
    Email::Stuffer
          ->from($args{from}||'someone@users.example.com')
          ->to($args{to}||$return_path)
          ->text_body($args{body}||'some simple message')
          ->email;
}
 
sub deliveries_are {
    my (%args) = @_;
 
    my $ctx = context();
    my $to = $args{to};
    my @recipients = ref($to) ? @{$to} : $to;
    my @deliveries = transport->deliveries;
    is(
        \@deliveries,
        array {
            if (@recipients) {
                item hash {
                    field envelope => hash {
                        field from => $args{from}||$return_path;
                        field to => bag {
                            for (@recipients) {
                                item object { call address => $_ };
                            }
                        };
                    };
                };
            }
            end();
        },
        'the deliveries should be as expected',
        np @deliveries,
    );
    $ctx->release;
}
 
sub test_sending {
    my (%args) = @_;
    my $ctx = context();
 
    my $sietima = delete $args{sietima};
    if (!$sietima or ref($sietimaeq 'HASH') {
        $sietima = make_sietima(%{$sietima||{}});
    }
    my $mail = delete $args{mail};
    if (!$mail or ref($maileq 'HASH') {
        $mail = make_mail(
            to => $sietima->return_path,
            %{$mail||{}},
        );
    }
 
    transport->clear_deliveries;
 
    ok(
        lives { $sietima->handle_mail($mail) },
        'should handle the mail',
        $@,
    );
 
    $args{from} ||= $sietima->return_path;
    $args{to} ||= [ map { $_->address} @{$sietima->subscribers} ];
    deliveries_are(%args);
 
    $ctx->release;
}
 
1;