File Coverage

blib/lib/Plack/App/DAIA/Test.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1 2     2   55636 use strict;
  2         3  
  2         46  
2 2     2   6 use warnings;
  2         1  
  2         87  
3             package Plack::App::DAIA::Test;
4             {
5             $Plack::App::DAIA::Test::VERSION = '0.45_1';
6             }
7             #ABSTRACT: Test DAIA Servers
8              
9 2     2   7 use base 'Test::Builder::Module';
  2         3  
  2         164  
10             our @EXPORT = qw(test_daia_psgi test_daia daia_app);
11              
12 2     2   818 use URI::Escape;
  2         2155  
  2         104  
13 2     2   10 use Test::More;
  2         2  
  2         19  
14 2     2   1057 use Plack::Test;
  2         1925  
  2         79  
15 2     2   674 use Plack::App::DAIA;
  0            
  0            
16             use Scalar::Util qw(reftype blessed);
17             use HTTP::Request::Common;
18             use Test::JSON::Entails;
19              
20             sub test_daia {
21             my $app = daia_app(shift) || do {
22             __PACKAGE__->builder->ok(0,"Could not construct DAIA application");
23             return;
24             };
25             my $test_name = "test_daia";
26             $test_name = pop(@_) if @_ % 2;
27             while (@_) {
28             my $id = shift;
29             my $expected = shift;
30             my $res = $app->retrieve($id);
31             if (!_if_daia_check( $res, $expected, $test_name )) {
32             $@ = "The thing isa DAIA::Response" unless $@;
33             __PACKAGE__->builder->ok(0, $@);
34             }
35             }
36             }
37              
38             sub test_daia_psgi {
39             my $app = shift;
40              
41             # TODO: load psgi file if string given and allow for URL
42             my $test_name = "test_daia";
43             $test_name = pop(@_) if @_ % 2;
44             while (@_) {
45             my $id = shift;
46             my $expected = shift;
47             test_psgi $app, sub {
48             my $req = shift->(GET "/?id=".uri_escape($id));
49             my $res = eval { DAIA::parse( $req->content ); };
50             if ($@) {
51             $@ =~ s/DAIA::([A-Z]+::)?[a-z_]+\(\)://ig;
52             $@ =~ s/ at .* line.*//g;
53             $@ =~ s/\s*$//sg;
54             }
55             if (!_if_daia_check( $res, $expected, $test_name )) {
56             $@ = "No valid The thing isa DAIA::Response" unless $@;
57             __PACKAGE__->builder->ok(0, $@);
58             }
59             };
60             }
61             }
62              
63             sub daia_app {
64             my $app = shift;
65             if ( blessed($app) and $app->isa('Plack::App::DAIA') ) {
66             return $app;
67             } elsif ( $app =~ qr{^https?://} ) {
68             my $baseurl = $app . ($app =~ /\?/ ? '&id=' : '?id=');
69             $app = sub {
70             my $id = shift;
71             my $url = $baseurl.$id;
72             my @daia = eval { DAIA->parse($url) };
73             if (!@daia) {
74             $@ ||= '';
75             if ($@) {
76             $@ =~ s/DAIA::([A-Z]+::)?[a-z_]+\(\)://ig;
77             $@ =~ s/ at .* line.*//g;
78             $@ =~ s/\s*$//sg;
79             }
80             $@ = "invalid DAIA from $url: $@";
81             }
82             return $daia[0];
83             };
84             }
85             if ( ref($app) and reftype($app) eq 'CODE' ) {
86             return Plack::App::DAIA->new( code => $app );
87             }
88             return;
89             }
90              
91             # Call C<$code> with C<$daia> and set as C<$_>, if C<$daia> is a L<DAIA::Response>
92             # and return C<$daia> on success. Return C<undef> otherwise.
93             sub _if_daia_check {
94             my ($daia, $expected, $test_name) = @_;
95             if ( blessed($daia) and $daia->isa('DAIA::Response') ) {
96             if ( (reftype($expected)||'') eq 'CODE') {
97             local $_ = $daia;
98             $expected->($daia);
99             } else {
100             local $Test::Builder::Level = $Test::Builder::Level + 2;
101             entails $daia->json, $expected, $test_name;
102             }
103             return $daia;
104             }
105             }
106              
107             1;
108              
109              
110             __END__
111             =pod
112              
113             =head1 NAME
114              
115             Plack::App::DAIA::Test - Test DAIA Servers
116              
117             =head1 VERSION
118              
119             version 0.45_1
120              
121             =head1 SYNOPSIS
122              
123             use Test::More;
124             use Plack::App::DAIA::Test;
125              
126             use Your::App; # your subclass of Plack::App::DAIA
127             my $app = Your::App->new;
128              
129             # or wrap a DAIA server
130             my $app = daia_app( 'http://your.host/pathtodaia' );
131              
132             test_daia $app,
133             'some:id' => sub {
134             my $daia = shift; # or = $_
135             my @docs = $daia->document;
136             is (scalar @docs, 1, 'returned one document');
137             ...
138             },
139             'another:id' => sub {
140             my $daia = shift;
141             ...
142             };
143              
144             # same usage, shown here with an inline server
145              
146             test_daia_psgi
147             sub {
148             my $id = shift;
149             my $daia = DAIA::Response->new();
150             ...
151             return $daia;
152             },
153             'some:id' => sub {
154             my $daia = $_; # or shift
155             ...
156             };
157              
158             done_testing;
159              
160             =head1 DESCRIPTION
161              
162             I<This model is experimental, so take care!> The current version has different
163             behaviour for C<test_daia> and C<test_daia_psgi>, that might get fixed.
164              
165             This module exports two methods for testing L<DAIA> servers. You must provide a
166             DAIA server as code reference or as instance of L<Plack::App::DAIA> and a list
167             of request identifiers and testing code. The testing code is passed a valid
168             L<DAIA::Response> object on success (C<$_> is also set to this response).
169              
170             =head1 METHODS
171              
172             =head2 test_daia ( $app, $id1 => sub { }, $id2 => ... )
173              
174             Calls a DAIA server C<$app>'s request method with one or more identifiers,
175             each given a test function.
176              
177             =head2 test_daia_psgi ( $app, $id => sub { }, $id => ... )
178              
179             Calls a DAIA server C<$app> as L<PSGI> application with one or more
180             identifiers, each given a test function.
181              
182             =head2 daia_app ( $plack_app_daia | $url | $code )
183              
184             Returns an instance of L<Plack::App::DAIA> or undef. Code references or URLs
185             are wrapped. For wrapped URLs C<$@> is set on failure. This method may be removed
186             to be used internally only!
187              
188             =head1 SEE ALSO
189              
190             L<Plack::App::DAIA::Test::Suite> and L<provedaia>.
191              
192             =head1 AUTHOR
193              
194             Jakob Voss
195              
196             =head1 COPYRIGHT AND LICENSE
197              
198             This software is copyright (c) 2012 by Jakob Voss.
199              
200             This is free software; you can redistribute it and/or modify it under
201             the same terms as the Perl 5 programming language system itself.
202              
203             =cut
204