HTTP-WebTest
view release on metacpan or search on metacpan
lib/HTTP/WebTest/ReportPlugin.pm view on Meta::CPAN
$self->test_output(undef);
}
=head2 end_tests ()
This method is called by L<HTTP::WebTest|HTTP::WebTest> at the end of
a test run. Its implementation in this class e-mails the test report
according test parameters C<mail***>.
If you redefine this method in subclass be sure to call
the superclass method in the new method:
sub end_tests {
my $self = shift;
# your code here
....
$self->SUPER::end_tests;
}
=cut
sub end_tests {
my $self = shift;
if($self->_email_report_is_expected) {
$self->_send_email_report;
}
}
# check if we need to mail report
sub _email_report_is_expected {
my $self = shift;
$self->global_validate_params(qw(mail));
my $mail = $self->global_test_param('mail');
return unless defined $mail;
return unless $mail eq 'all' or $mail eq 'errors';
return if $mail eq 'errors' and $self->webtest->have_succeed;
return 1;
}
# sends test report on email
sub _send_email_report {
my $self = shift;
$self->global_validate_params(qw(mail_addresses mail_server mail_from));
my $mail_addresses = $self->global_test_param('mail_addresses');
my $mail_server = $self->global_test_param('mail_server', 'localhost');
my $mail_from = $self->global_test_param('mail_from');
my $smtp = Net::SMTP->new($mail_server);
die "HTTP::WebTest: Can't create Net::SMTP object"
unless defined $smtp;
my $from = $mail_from || getlogin() || getpwuid($<) || 'nobody';
$self->_smtp_cmd($smtp, 'mail', $from);
$self->_smtp_cmd($smtp, 'to', @$mail_addresses);
$self->_smtp_cmd($smtp, 'data');
$self->_smtp_cmd($smtp, 'datasend', "From: $from\n");
{
my $mail_addresses = join ', ', @$mail_addresses;
$self->_smtp_cmd($smtp, 'datasend', "To: $mail_addresses\n");
}
$self->_smtp_cmd($smtp, 'datasend',
'Subject: ' . $self->_subject_header . "\n");
$self->_smtp_cmd($smtp, 'datasend', "\n");
$self->_smtp_cmd($smtp, 'datasend', ${$self->test_output});
$self->_smtp_cmd($smtp, 'dataend');
$self->_smtp_cmd($smtp, 'quit');
}
# returns value of subject header for email report
sub _subject_header {
my $self = shift;
$self->global_validate_params(qw(mail_success_subject mail_failure_subject));
my $success_subject
= $self->global_test_param('mail_success_subject',
'Web tests succeeded');
my $fail_subject
= $self->global_test_param('mail_failure_subject',
'WEB TESTS FAILED! FOUND %f ERROR(S)');
my %replace = ('f' => $self->webtest->num_fail,
's' => $self->webtest->num_succeed,
't' => ($self->webtest->num_fail +
$self->webtest->num_succeed),
'%' => '%'
);
my $subject = ($self->webtest->have_succeed ?
$success_subject :
$fail_subject);
$subject =~ s/%(.)/exists $replace{$1} ? $replace{$1} : '%' . $1/ge;
return $subject;
}
# simple helper method that automates error handling
sub _smtp_cmd {
my $self = shift;
my $smtp = shift;
my $cmd = shift;
my $ret = $smtp->$cmd(@_);
unless($ret) {
my $msg = $smtp->message;
die "HTTP::WebTest: mail error for command $cmd: $msg";
}
}
( run in 2.914 seconds using v1.01-cache-2.11-cpan-ceb78f64989 )