File Coverage

blib/lib/App/PAIA/Tester.pm
Criterion Covered Total %
statement 66 71 92.9
branch 8 12 66.6
condition 6 11 54.5
subroutine 22 24 91.6
pod 0 11 0.0
total 102 129 79.0


line stmt bran cond sub pod time code
1             package App::PAIA::Tester;
2 4     4   51958 use strict;
  4         7  
  4         117  
3 4     4   30 use v5.10;
  4         9  
  4         145  
4              
5             our $VERSION = '0.29';
6              
7 4     4   1524 use parent 'Exporter';
  4         877  
  4         14  
8             our @cmd = qw(stdout stderr output error exit_code);
9             our @EXPORT = (
10             qw(new_paia_test paia_response done_paia_test paia debug),
11             qw(decode_json encode_json),
12             @cmd);
13              
14 4     4   234 use Test::More;
  4         5  
  4         18  
15 4     4   2248 use App::Cmd::Tester;
  4         46802  
  4         22  
16 4     4   3430 use File::Temp qw(tempdir);
  4         51829  
  4         221  
17 4     4   29 use Cwd;
  4         5  
  4         209  
18 4     4   1131 use App::PAIA;
  4         12  
  4         40  
19 4     4   686 use JSON::PP;
  4         5  
  4         231  
20 4     4   18 use Scalar::Util qw(reftype);
  4         5  
  4         162  
21 4     4   16 use HTTP::Tiny;
  4         5  
  4         773  
22              
23             our $CWD = getcwd();
24             our $RESULT;
25              
26 7     7 0 1842 eval "sub $_ { \$RESULT->$_ }" for @cmd; ## no critic
  7     7 0 2752  
  12     12 0 2140  
  0     0 0 0  
  5     5 0 1198  
27              
28             our $HTTP_TINY_REQUEST = \&HTTP::Tiny::request;
29              
30             our $DEFAULT_PSGI = [ 500, [], ["no response faked yet"] ];
31             our $PSGI_RESPONSE = $DEFAULT_PSGI;
32             our $HTTP_REQUEST = sub { $PSGI_RESPONSE };
33              
34             sub mock_http {
35 6     6 0 7 my ($self, $method, $url, $opts) = @_;
36 6         24 my $psgi = $HTTP_REQUEST->(
37             $method, $url, $opts->{headers}, $opts->{content}
38             );
39             return {
40 6         21 protocol => 'HTTP/1.1',
41             status => $psgi->[0],
42 6         33 headers => { @{$psgi->[1]} },
43 6         13 content => join "", @{$psgi->[2]},
44             };
45             };
46              
47             sub new_paia_test(@) { ## no critic
48 5     5 0 172 my (%options) = @_;
49              
50 5         28 chdir tempdir();
51              
52 4     4   19 no warnings 'redefine';
  4         5  
  4         215  
53 5 100       2337 if ($options{mock_http}) {
54 3         22 *HTTP::Tiny::request = \&mock_http;
55             } else {
56 4     4   14 no warnings;
  4         5  
  4         1495  
57 2         6 *HTTP::Tiny::request = $HTTP_TINY_REQUEST;
58             }
59             }
60              
61             sub paia_response(@) { ## no critic
62 4     4 0 909 $PSGI_RESPONSE = $DEFAULT_PSGI;
63 4 50 66     31 if (ref $_[0] and reftype $_[0] eq 'ARRAY') {
64 0         0 $PSGI_RESPONSE = shift;
65             } else {
66 4         6 $PSGI_RESPONSE = $DEFAULT_PSGI;
67 4 100       26 $PSGI_RESPONSE->[0] = $_[0] =~ /^\d+/ ? shift : 200;
68 4 100 66     48 $PSGI_RESPONSE->[1] = shift if ref $_[0] and reftype $_[0] eq 'ARRAY' and @_ > 1;
      66        
69 4         8 my $content = shift;
70 4 50       15 if (reftype $content eq 'HASH') {
    0          
71 4         5 push @{$PSGI_RESPONSE->[1]}, 'Content-type', 'application/json; charset=UTF-8';
  4         10  
72 4         17 $PSGI_RESPONSE->[2] = [ encode_json($content) ];
73             } elsif (reftype $_[1] eq 'ARRAY') {
74 0         0 $PSGI_RESPONSE->[2] = $content;
75             } else {
76 0         0 $PSGI_RESPONSE->[2] = [$content];
77             }
78             }
79             }
80              
81             sub paia(@) { ## no critic
82 25     25 0 8099 $RESULT = test_app('App::PAIA' => [@_]);
83             }
84              
85             sub done_paia_test {
86 4     4 0 1888 chdir $CWD;
87 4         18 done_testing;
88             }
89              
90             sub debug {
91 0   0 0 0   say "# $_" for split "\n", join "\n", (
92             "stdout: ".$RESULT->stdout,
93             "stderr: ".$RESULT->stderr,
94             "error: ".$RESULT->error // 'undef',
95             "exit_code: ".$RESULT->exit_code
96             );
97             }
98              
99             1;
100             __END__