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   50591 use strict;
  4         5  
  4         114  
3 4     4   32 use v5.10;
  4         8  
  4         98  
4              
5 4     4   1452 use parent 'Exporter';
  4         777  
  4         13  
6             our @cmd = qw(stdout stderr output error exit_code);
7             our @EXPORT = (
8             qw(new_paia_test paia_response done_paia_test paia debug),
9             qw(decode_json encode_json),
10             @cmd);
11              
12 4     4   235 use Test::More;
  4         5  
  4         14  
13 4     4   1903 use App::Cmd::Tester;
  4         42322  
  4         18  
14 4     4   3102 use File::Temp qw(tempdir);
  4         48716  
  4         221  
15 4     4   23 use Cwd;
  4         5  
  4         173  
16 4     4   999 use App::PAIA;
  4         9  
  4         25  
17 4     4   2896 use JSON::PP;
  4         36179  
  4         278  
18 4     4   23 use Scalar::Util qw(reftype);
  4         4  
  4         134  
19 4     4   2307 use HTTP::Tiny;
  4         101575  
  4         682  
20              
21             our $CWD = getcwd();
22             our $RESULT;
23              
24 7     7 0 2837 eval "sub $_ { \$RESULT->$_ }" for @cmd; ## no critic
  7     7 0 3397  
  11     11 0 1805  
  0     0 0 0  
  5     5 0 1106  
25              
26             our $HTTP_TINY_REQUEST = \&HTTP::Tiny::request;
27              
28             our $DEFAULT_PSGI = [ 500, [], ["no response faked yet"] ];
29             our $PSGI_RESPONSE = $DEFAULT_PSGI;
30             our $HTTP_REQUEST = sub { $PSGI_RESPONSE };
31              
32             sub mock_http {
33 6     6 0 9 my ($self, $method, $url, $opts) = @_;
34 6         23 my $psgi = $HTTP_REQUEST->(
35             $method, $url, $opts->{headers}, $opts->{content}
36             );
37             return {
38 6         20 protocol => 'HTTP/1.1',
39             status => $psgi->[0],
40 6         32 headers => { @{$psgi->[1]} },
41 6         13 content => join "", @{$psgi->[2]},
42             };
43             };
44              
45             sub new_paia_test(@) { ## no critic
46 5     5 0 274 my (%options) = @_;
47              
48 5         25 chdir tempdir();
49              
50 4     4   32 no warnings 'redefine';
  4         4  
  4         197  
51 5 100       2201 if ($options{mock_http}) {
52 3         16 *HTTP::Tiny::request = \&mock_http;
53             } else {
54 4     4   12 no warnings;
  4         6  
  4         1157  
55 2         8 *HTTP::Tiny::request = $HTTP_TINY_REQUEST;
56             }
57             }
58              
59             sub paia_response(@) { ## no critic
60 4     4 0 956 $PSGI_RESPONSE = $DEFAULT_PSGI;
61 4 50 66     31 if (ref $_[0] and reftype $_[0] eq 'ARRAY') {
62 0         0 $PSGI_RESPONSE = shift;
63             } else {
64 4         6 $PSGI_RESPONSE = $DEFAULT_PSGI;
65 4 100       25 $PSGI_RESPONSE->[0] = $_[0] =~ /^\d+/ ? shift : 200;
66 4 100 66     43 $PSGI_RESPONSE->[1] = shift if ref $_[0] and reftype $_[0] eq 'ARRAY' and @_ > 1;
      66        
67 4         5 my $content = shift;
68 4 50       15 if (reftype $content eq 'HASH') {
    0          
69 4         6 push @{$PSGI_RESPONSE->[1]}, 'Content-type', 'application/json; charset=UTF-8';
  4         10  
70 4         19 $PSGI_RESPONSE->[2] = [ encode_json($content) ];
71             } elsif (reftype $_[1] eq 'ARRAY') {
72 0         0 $PSGI_RESPONSE->[2] = $content;
73             } else {
74 0         0 $PSGI_RESPONSE->[2] = [$content];
75             }
76             }
77             }
78              
79             sub paia(@) { ## no critic
80 23     23 0 6307 $RESULT = test_app('App::PAIA' => [@_]);
81             }
82              
83             sub done_paia_test {
84 4     4 0 1798 chdir $CWD;
85 4         17 done_testing;
86             }
87              
88             sub debug {
89 0   0 0 0   say "# $_" for split "\n", join "\n", (
90             "stdout: ".$RESULT->stdout,
91             "stderr: ".$RESULT->stderr,
92             "error: ".$RESULT->error // 'undef',
93             "exit_code: ".$RESULT->exit_code
94             );
95             }
96              
97             1;
98             __END__