File Coverage

blib/lib/KelpX/Symbiosis/Test.pm
Criterion Covered Total %
statement 22 22 100.0
branch 6 8 75.0
condition n/a
subroutine 6 6 100.0
pod 1 3 33.3
total 35 39 89.7


line stmt bran cond sub pod time code
1             package KelpX::Symbiosis::Test;
2              
3             our $VERSION = '1.12';
4              
5 6     6   842775 use Kelp::Base;
  6         101096  
  6         44  
6 6     6   4471 use Kelp::Test;
  6         306510  
  6         49  
7              
8             attr "-app" => sub { die "`app` parameter is required" };
9              
10             sub wrap
11             {
12 5     5 1 5753 my ($class, %args) = @_;
13 5         45 my $self = $class->new(%args);
14 5         65 return Kelp::Test->new(app => $self);
15             }
16              
17             sub run
18             {
19 19     19 0 75224 shift->app->run_all(@_);
20             }
21              
22             sub can
23             {
24 14     14 0 35915 my ($self, $func) = @_;
25              
26 14 100       51 if (ref $self) {
27 2         8 my $can = $self->app->can($func);
28 2 50       22 return $can if defined $can;
29             }
30              
31 12         106 return $self->SUPER::can($func);
32             }
33              
34             sub AUTOLOAD
35             {
36 21     21   366640 my ($self) = shift;
37              
38 21         54 my $func = our $AUTOLOAD;
39 21 100       602 return if $func =~ /::DESTROY$/;
40 15         97 $func =~ s/.*:://;
41              
42 15         62 my $method = $self->app->can($func);
43 15 50       137 die "Kelp cannot $func" unless $method;
44 15         49 $method->($self->app, @_);
45             }
46              
47             1;
48             __END__
49              
50             =head1 NAME
51              
52             KelpX::Symbiosis::Test - Allow testing symbiotic environments using Kelp::Test
53              
54             =head1 SYNOPSIS
55              
56             # in test file
57             use KelpX::Symbiosis::Test;
58              
59             my $t = KelpX::Symbiosis::Test->wrap(app => $kelp_app);
60              
61             # continue testing using $t, just like Kelp::Test
62              
63             =head1 DESCRIPTION
64              
65             This module allows testing Kelp apps with Symbiosis using L<Kelp::Test>. The problem with I<Kelp::Test> is that it automatically runs I<run()> on the app without any way to configure this behavior. Symbiotic apps use I<run_all()> to run the whole environment, while I<run()> stays the same and only runs Kelp. This module replaces those two methods and autoloads the rest of the methods from Kelp instance.
66              
67             =head1 USAGE
68              
69             =head2 wrap
70              
71             I<new in 1.10>
72              
73             Instead of using I<Kelp::Test::new> use I<KelpX::Symbiosis::Test::wrap> with the same interface. Then you can create test cases not only for Kelp routes but also for the rest of Plack applications. The I<wrap> method will return a L<Kelp::Test> object, so refer to its documentation for more details.
74              
75             =head1 HOW DOES IT WORK?
76              
77             The main Kelp instance is wrapped in this module class and the resulting object is passed into Kelp::Test instead. I<KelpX::Symbiosis::Test> autoloads Kelp methods and wraps I<run_all> inside I<run>, which allows L<Kelp::Test> to use it.