diff options
author | dakkar <dakkar@thenautilus.net> | 2016-06-16 18:09:39 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2016-06-16 18:09:39 +0100 |
commit | ee35cc15e1e05fb48ec9ecf476c0c5f6d4e50314 (patch) | |
tree | d45412a86b77c347e669be5b24a37a8b59bdb0e2 /t/lib | |
parent | role: drop mail from non-subscribers (diff) | |
download | Sietima-ee35cc15e1e05fb48ec9ecf476c0c5f6d4e50314.tar.gz Sietima-ee35cc15e1e05fb48ec9ecf476c0c5f6d4e50314.tar.bz2 Sietima-ee35cc15e1e05fb48ec9ecf476c0c5f6d4e50314.zip |
factor out common test functions
Diffstat (limited to 't/lib')
-rw-r--r-- | t/lib/Test/Sietima.pm | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/t/lib/Test/Sietima.pm b/t/lib/Test/Sietima.pm new file mode 100644 index 0000000..e61640a --- /dev/null +++ b/t/lib/Test/Sietima.pm @@ -0,0 +1,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($sietima) eq 'HASH') { + $sietima = make_sietima(%{$sietima||{}}); + } + my $mail = delete $args{mail}; + if (!$mail or ref($mail) eq '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; |