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 57     57   972247 use Mouse;
  57         253237  
  57         300  
3              
4 57     57   48034 use HTTP::Request;
  57         749131  
  57         1778  
5 57     57   28793 use HTTP::Cookies;
  57         630937  
  57         1853  
6 57     57   24370 use Plack::Test;
  57         32010  
  57         3070  
7              
8 57     57   20684 use FindBin;
  57         44932  
  57         2475  
9 57     57   4395 use Path::Class qw/dir/;
  57         332714  
  57         2962  
10              
11 57     57   24033 use Ark::Test::Context;
  57         150  
  57         4618  
12              
13             sub import {
14 77     77   9894 my ($class, $app_class, @rest) = @_;
15 77         186 my $caller = caller;
16 77         238 my %option = @rest;
17              
18 77 50       305 return unless $app_class;
19              
20 77 100       592 Mouse::load_class($app_class) unless Mouse::is_class_loaded($app_class);
21              
22 77         705 my $persist_app = undef;
23 77         124 my $cookie;
24              
25             {
26 57     57   382 no strict 'refs';
  57         164  
  57         1821  
  77         119  
27 57     57   298 no warnings 'redefine';
  57         141  
  57         45198  
28              
29 77         751 *{ $caller . '::request'} = sub {
30 289     289   105885 my $app;
31 289 100       13356 unless ($persist_app) {
32 230         3204 $app = $app_class->new;
33              
34 307         1104 my @components = map { "${app_class}::${_}" }
35 230 100       593 @{ $option{components} || [] };
  230         964  
36 230         1369 $app->load_component($_) for @components;
37              
38 230 100       680 if ($option{minimal_setup}) {
39 54         208 $app->setup_home;
40              
41 54         1049 $app->path_to('action.cache')->remove;
42              
43 54         76978 my $child = fork;
44 54 100       4150 if ($child == 0) {
    50          
45 14         2335 $app->setup_minimal;
46 14         5752 exit;
47             }
48             elsif (!defined($child)) {
49 0         0 die $!;
50             }
51              
52 40         28497920 waitpid $child, 0;
53              
54 40         3533 $app->setup_minimal;
55             }
56             else {
57 176         686 $app->setup;
58             }
59 216   66     1243 $app->config->{home} ||= dir($FindBin::Bin);
60             }
61              
62 275 100       8535 if ($option{reuse_connection}) {
63 69 100       197 if ($persist_app) {
64 59         103 $app = $persist_app;
65             }
66             else {
67 10         30 $persist_app = $app;
68 10         125 $cookie = HTTP::Cookies->new;
69             }
70             }
71              
72 275 100       3783 my $req = ref($_[0]) eq 'HTTP::Request' ? $_[0] : HTTP::Request->new(@_);
73 275 100       268724 if ($cookie) {
74 69 50 33     214 unless ($req->uri->can('host') and $req->uri->host) {
75 69         780 $req->uri( URI->new('http://localhost' . $req->uri->path_query ) );
76 69         34147 $req->header( Host => 'localhost' );
77             }
78 69 100       4598 $cookie->add_cookie_header($req) unless $req->header('Cookie');
79             }
80              
81 275         26324 my $res;
82             test_psgi(
83             app => $app->handler,
84             client => sub {
85 275     275   784829 my $cb = shift;
86 275         871 $res = $cb->($req);
87             },
88 275         2309 );
89              
90 275 100       86573 if ($cookie) {
91 69         151 $res->{_request} = $req;
92 69 50       309 $cookie && $cookie->extract_cookies($res);
93             }
94              
95 275 100       35866 $app->path_to('action.cache')->remove if $option{minimal_setup};
96              
97 275         12201 $res;
98 77         466 };
99              
100 77         326 *{ $caller . '::get' } = sub {
101 131     131   85737 &{$caller . '::request'}(GET => @_)->content;
  131         653  
102 77         297 };
103              
104 77         351 *{ $caller . '::reset_app' } = sub() {
105 2     2   1884 undef $persist_app;
106 2         11 undef $cookie;
107 77         341 };
108              
109 77         373 *{ $caller . '::ctx_request'} = sub {
110 62 100   62   58993 unless (Ark::Context->meta->does_role('Ark::Test::Context')) {
111 9         522 Ark::Context->meta->make_mutable;
112 9         245 Ark::Test::Context->meta->apply( Ark::Context->meta );
113 9         16619 Ark::Context->meta->make_immutable;
114             }
115              
116 62         3168 my $res = &{$caller . '::request'}(@_);
  62         228  
117 62         158 return $res, context();
118 77         288 };
119              
120 77         83049 *{ $caller . '::ctx_get' } = sub {
121 22     22   52243 my ($res, $c) = &{$caller . '::ctx_request'}(GET => @_);
  22         115  
122 22         82 return $res->content, $c;
123 77         261 };
124             }
125             }
126              
127             do {
128             my $context;
129             sub context {
130 136 100   136 0 342 if ($_[0]) {
131 74         399 $context = $_[0];
132             }
133 136         1555 $context;
134             }
135             };
136              
137             1;
138