blib/lib/ASP4/API.pm

filter: Statementstmt 85.0% 34 / 40 Branchbran 37.5% 3 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 68.7% 11 / 16 Podpod 75.0% 6 / 8 totaltotal 75.0% 54 / 72 CC 5 SCAR 9.1
CC
5
Cov
77%
new
5.9
test_fixtures
2.0
properties
2.0
CRAP 2.5
« blib/lib/ASP4/FileUpload.pm ↑ Summary blib/lib/ASP4/ConfigNode.pm »
1  
2 package ASP4::API;
3  
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use ASP4::ConfigLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use ASP4::HTTPContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use ASP4::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use ASP4::Test::Fixtures;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶BEGIN { ASP4::ConfigLoader->load }
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new
13 11 ▶{
Subroutine new: called CC=5 SCAR=17.8
Pod documented
14 11 my ($class) = @_;;
15
16 11 my $config = ASP4::ConfigLoader->load;
17
18 # Our test fixtures:
19 11 my $test_data;
20 11 ▶ if( -f $config->web->application_root . '/etc/test_fixtures.json' )
Branch: 2 branches
BranchTrueFalseTotal
if (-f $config->web->application_root . '/etc/test_fixtures.json') { } 11 0 11
elsif (-f $config->web->application_root . '/etc/test_fixtures.yaml') { } 0 0 0
21 {
22 11 eval { require Data::Properties::JSON };
23 11 ▶ $test_data = Data::Properties::JSON->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 11 0 11
24 properties_file => $config->web->application_root . '/etc/test_fixtures.json'
25 ) unless $@;
26 }
27 elsif( -f $config->web->application_root . '/etc/test_fixtures.yaml' )
28 {
29 0 $test_data = ASP4::Test::Fixtures->new(
30 properties_file => $config->web->application_root . '/etc/test_fixtures.yaml'
31 );
32 }# end if()
33
34 # Our diagnostic messages:
35 11 ▶ my $properties = Data::Properties::YAML->new(
Branch: 1 branch
BranchTrueFalseTotal
if -f $config->web->application_root . '/etc/properties.yaml' 11 0 11
36 properties_file => $config->web->application_root . '/etc/properties.yaml'
37 ) if -f $config->web->application_root . '/etc/properties.yaml';
38
39 11 return bless {
40 test_fixtures => $test_data,
41 properties => $properties,
42 ua => ASP4::UserAgent->new(),
43 config => $config,
44 }, $class;
45 }# end new()
46  
47 11 *init = \&new;
48  
49 0 ▶sub test_fixtures { shift->{test_fixtures} }
Subroutine test_fixtures: not called CC=1 SCAR=6.9
Pod undocumented
50 0 ▶sub properties { shift->{properties} }
Subroutine properties: not called CC=1 SCAR=6.9
Pod documented
51 5118 ▶sub ua { shift->{ua} }
Subroutine ua: called CC=1 SCAR=0.0
Pod documented
52 100 ▶sub context { ASP4::HTTPContext->current }
Subroutine context: called CC=1 SCAR=0.0
Pod documented
53 0 ▶sub config { shift->{config} }
Subroutine config: not called CC=1 SCAR=6.9
Pod documented
54 0 ▶sub data { shift->test_fixtures } # XXX: Deprecated! - for Apache2::ASP compat only.
Subroutine data: not called CC=1 SCAR=6.9
Pod documented
55 0 ▶sub test_data { shift->test_fixtures } # XXX: Deprecated!
Subroutine test_data: not called CC=1 SCAR=6.9
Pod undocumented
56  
57 sub DESTROY
58 10 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
59 10 my $s = shift;
60 10 undef(%$s);
61 }# end DESTROY()
62  
63 11 1;# return true:
64  
65 =pod
66  
67 =head1 NAME
68  
69 ASP4::API - Public Programmatic API to an ASP4 Application
70  
71 =head1 SYNOPSIS
72  
73 #!/usr/bin/perl -w
74
75 use strict;
76 use warnings 'all';
77
78 # Load up and initialize ASP4::API *before* using your app's classes:
79 use ASP4::API;
80
81 # *Now* you can use your app's other classes:
82 # because the environment has been initialized (@INC, %ENV, etc):
83 use app::user;
84 use app::product;
85 use app::order;
86
87 # Create an api object:
88 my $api = ASP4::API->new;
89
90 # Use the API:
91 my $res = $api->ua->get('/index.asp');
92 if( $res->is_success ) {
93 print $res->content;
94 }
95
96 # Access your test data:
97 warn $res->test_data->contact_form->email;
98
99 # Access your properties YAML:
100 warn $res->properties->contact_form->email->is_missing;
101
102 # Access the application config:
103 warn $api->config->system->settings->foo;
104  
105 =head1 DESCRIPTION
106  
107 C<ASP4::API> is B<very useful for unit tests> - specifically when writing tests
108 for the actual web pages themselves.
109  
110 =head2 Example Unit Test
111  
112 #!/usr/bin/perl -w
113
114 use strict;
115 use warnings 'all';
116 use Test::More 'no_plan';
117
118 use ASP4::API;
119 my $api = ASP4::API->new();
120
121 ok(
122 $api, "Got api"
123 );
124 like(
125 $api->ua->get('/hello.asp')->content => qr/Hello\s+World\!/,
126 'Website is friendly'
127 );
128  
129 =head1 CONSTRUCTOR
130  
131 =head2 new( )
132  
133 Takes no arguments. Finds and initializes your application's configuration, which
134 means that any other part of your application which requires the configuration
135 to have been loaded up will now work.
136  
137 =head2 init( )
138  
139 C<init()> is simply an alias of C<new()>
140  
141 =head1 PUBLIC PROPERTIES
142  
143 =head2 ua
144  
145 Returns an L<ASP4::UserAgent> that can be used to interact with pages on your website.
146  
147 =head2 context
148  
149 Returns the current instance of L<ASP4::HTTPContext> in use.
150  
151 =head2 config
152  
153 Returns the L<ASP4::Config> object for the web application.
154  
155 =head2 properties
156  
157 Returns an object representing your C</etc/properties.yaml> file.
158  
159 =head2 data
160  
161 Returns an object representing your C</etc/test_fixtures.yaml> file.
162  
163 =head1 BUGS
164  
165 It's possible that some bugs have found their way into this release.
166  
167 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.
168  
169 =head1 HOMEPAGE
170  
171 Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples
172 of ASP4 in action.
173  
174 =cut
175  
« blib/lib/ASP4/FileUpload.pm ↑ Summary blib/lib/ASP4/ConfigNode.pm »