File Coverage

blib/lib/Ark/Test.pm
Criterion Covered Total %
statement 100 101 99.0
branch 32 36 88.8
condition 3 6 50.0
subroutine 17 17 100.0
pod 0 1 0.0
total 152 161 94.4


line stmt bran cond sub pod time code
1             package Ark::Test;
2 56     56   360539 use Mouse;
  56         291104  
  56         398  
3              
4 56     56   94885 use HTTP::Request;
  56         305487  
  56         1614  
5 56     56   54759 use HTTP::Cookies;
  56         859434  
  56         1931  
6 56     56   46548 use Plack::Test;
  56         43014  
  56         3949  
7              
8 56     56   37675 use FindBin;
  56         101708  
  56         2466  
9 56     56   7893 use Path::Class qw/dir/;
  56         519294  
  56         2870  
10              
11 56     56   32869 use Ark::Test::Context;
  56         324  
  56         5354  
12              
13             sub import {
14 76     76   7636 my ($class, $app_class, @rest) = @_;
15 76         184 my $caller = caller;
16 76         262 my %option = @rest;
17              
18 76 50       343 return unless $app_class;
19              
20 76 100       738 Mouse::load_class($app_class) unless Mouse::is_class_loaded($app_class);
21              
22 76         1484 my $persist_app = undef;
23 76         183 my $cookie;
24              
25             {
26 56     56   373 no strict 'refs';
  56         113  
  56         1868  
  76         129  
27 56     56   293 no warnings 'redefine';
  56         167  
  56         48359  
28              
29 76         878 *{ $caller . '::request'} = sub {
30 288     288   98266 my $app;
31 288 100       1056 unless ($persist_app) {
32 229         3954 $app = $app_class->new;
33              
34 306 100       1119 my @components = map { "${app_class}::${_}" }
  229         2238  
35 229         1047 @{ $option{components} || [] };
36 229         1468 $app->load_component($_) for @components;
37              
38 229 100       729 if ($option{minimal_setup}) {
39 54         251 $app->setup_home;
40              
41 54         895 $app->path_to('action.cache')->remove;
42              
43 54         99168 my $child = fork;
44 54 100       4031 if ($child == 0) {
    50          
45 14         4403 $app->setup_minimal;
46 14         5903 exit;
47             }
48             elsif (!defined($child)) {
49 0         0 die $!;
50             }
51              
52 40         47465204 waitpid $child, 0;
53              
54 40         2744 $app->setup_minimal;
55             }
56             else {
57 175         1216 $app->setup;
58             }
59 215   66     1621 $app->config->{home} ||= dir($FindBin::Bin);
60             }
61              
62 274 100       13532 if ($option{reuse_connection}) {
63 68 100       171 if ($persist_app) {
64 59         112 $app = $persist_app;
65             }
66             else {
67 9         18 $persist_app = $app;
68 9         98 $cookie = HTTP::Cookies->new;
69             }
70             }
71              
72 274 100       5756 my $req = ref($_[0]) eq 'HTTP::Request' ? $_[0] : HTTP::Request->new(@_);
73 274 100       299178 if ($cookie) {
74 68 50 33     221 unless ($req->uri->can('host') and $req->uri->host) {
75 68         811 $req->uri( URI->new('http://localhost' . $req->uri->path_query ) );
76 68         35185 $req->header( Host => 'localhost' );
77             }
78 68 100       3939 $cookie->add_cookie_header($req) unless $req->header('Cookie');
79             }
80              
81 274         23524 my $res;
82             test_psgi(
83             app => $app->handler,
84             client => sub {
85 274     274   894808 my $cb = shift;
86 274         1025 $res = $cb->($req);
87             },
88 274         3118 );
89              
90 274 100       88166 if ($cookie) {
91 68         147 $res->{_request} = $req;
92 68 50       415 $cookie && $cookie->extract_cookies($res);
93             }
94              
95 274 100       26349 $app->path_to('action.cache')->remove if $option{minimal_setup};
96              
97 274         18001 $res;
98 76         568 };
99              
100 76         368 *{ $caller . '::get' } = sub {
101 131     131   103580 &{$caller . '::request'}(GET => @_)->content;
  131         746  
102 76         305 };
103              
104 76         660 *{ $caller . '::reset_app' } = sub() {
105 2     2   1416 undef $persist_app;
106 2         4 undef $cookie;
107 76         320 };
108              
109 76         418 *{ $caller . '::ctx_request'} = sub {
110 62 100   62   47471 unless (Ark::Context->meta->does_role('Ark::Test::Context')) {
111 9         468 Ark::Context->meta->make_mutable;
112 9         209 Ark::Test::Context->meta->apply( Ark::Context->meta );
113 9         24512 Ark::Context->meta->make_immutable;
114             }
115              
116 62         3966 my $res = &{$caller . '::request'}(@_);
  62         368  
117 62         164 return $res, context();
118 76         357 };
119              
120 76         92990 *{ $caller . '::ctx_get' } = sub {
121 22     22   41978 my ($res, $c) = &{$caller . '::ctx_request'}(GET => @_);
  22         133  
122 22         82 return $res->content, $c;
123 76         257 };
124             }
125             }
126              
127             do {
128             my $context;
129             sub context {
130 136 100   136 0 530 if ($_[0]) {
131 74         135 $context = $_[0];
132             }
133 136         663 $context;
134             }
135             };
136              
137             1;
138