| 1 | |||
| 2 | package ASP4::API; | ||
| 3 | |||
| 4 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 4 | ▶ | use warnings 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 4 | ▶ | use ASP4::ConfigLoader; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 4 | ▶ | use ASP4::HTTPContext; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use ASP4::UserAgent; | ||
| 9 | use Data::Properties::YAML; | ||
| 10 | use ASP4::Test::Fixtures; | ||
| 11 | |||
| 12 | sub new | ||
| 13 | { | ||
| 14 | my ($class) = @_;; | ||
| 15 | |||
| 16 | my $config = ASP4::ConfigLoader->load; | ||
| 17 | |||
| 18 | # Our test fixtures: | ||
| 19 | my $test_data = ASP4::Test::Fixtures->new( | ||
| 20 | properties_file => $config->web->application_root . '/etc/test_fixtures.yaml' | ||
| 21 | ) if -f $config->web->application_root . '/etc/test_fixtures.yaml'; | ||
| 22 | |||
| 23 | # Our diagnostic messages: | ||
| 24 | my $properties = Data::Properties::YAML->new( | ||
| 25 | properties_file => $config->web->application_root . '/etc/properties.yaml' | ||
| 26 | ) if -f $config->web->application_root . '/etc/properties.yaml'; | ||
| 27 | |||
| 28 | return bless { | ||
| 29 | test_data => $test_data, | ||
| 30 | properties => $properties, | ||
| 31 | ua => ASP4::UserAgent->new() | ||
| 32 | }, $class; | ||
| 33 | }# end new() | ||
| 34 | |||
| 35 | |||
| 36 | sub test_data { shift->{test_data} } | ||
| 37 | sub properties { shift->{properties} } | ||
| 38 | sub ua { shift->{ua} } | ||
| 39 | sub context { ASP4::HTTPContext->current } | ||
| 40 | sub config { ASP4::ConfigLoader->load } | ||
| 41 | |||
| 42 | sub data { shift->test_data } # Deprecated! - for Apache2::ASP compat only. | ||
| 43 | |||
| 44 | |||
| 45 | sub DESTROY | ||
| 46 | { | ||
| 47 | my $s = shift; | ||
| 48 | undef(%$s); | ||
| 49 | }# end DESTROY() | ||
| 50 | |||
| 51 | 1;# return true: | ||
| 52 | |||
| 53 | =pod | ||
| 54 | |||
| 55 | =head1 NAME | ||
| 56 | |||
| 57 | ASP4::API - Your ASP4 Web App's Public API | ||
| 58 | |||
| 59 | =head1 SYNOPSIS | ||
| 60 | |||
| 61 | #!/usr/bin/perl -w | ||
| 62 | |||
| 63 | use strict; | ||
| 64 | use warnings 'all'; | ||
| 65 | use ASP4::API; | ||
| 66 | |||
| 67 | my $api; BEGIN { $api = ASP4::API->new } | ||
| 68 | |||
| 69 | # Now you can use your other classes: | ||
| 70 | use My::User; | ||
| 71 | use My::Product; | ||
| 72 | use My::Foo; | ||
| 73 | |||
| 74 | # Use the API: | ||
| 75 | |||
| 76 | my $res = $api->ua->get('/index.asp'); | ||
| 77 | if( $res->is_success ) { | ||
| 78 | print $res->content; | ||
| 79 | } | ||
| 80 | |||
| 81 | # Access your test data: | ||
| 82 | warn $res->test_data->contact_form->email; | ||
| 83 | |||
| 84 | # Access your properties YAML: | ||
| 85 | warn $res->properties->contact_form->email->is_missing; | ||
| 86 | |||
| 87 | # Access the application config: | ||
| 88 | warn $api->config->system->settings->foo; | ||
| 89 | |||
| 90 | =head1 DESCRIPTION | ||
| 91 | |||
| 92 | C<ASP4::API> is B<very useful for unit tests> - specifically when writing tests | ||
| 93 | for the actual web pages themselves. | ||
| 94 | |||
| 95 | =head2 Example Unit Test | ||
| 96 | |||
| 97 | #!/usr/bin/perl -w | ||
| 98 | |||
| 99 | use strict; | ||
| 100 | use warnings 'all'; | ||
| 101 | use Test::More 'no_plan'; | ||
| 102 | |||
| 103 | use ASP4::API; | ||
| 104 | |||
| 105 | ok( | ||
| 106 | my $api = ASP4::API->new, "Got api" | ||
| 107 | ); | ||
| 108 | is( | ||
| 109 | $api->ua->get('/hello.asp')->content => 'Hello World!', | ||
| 110 | 'Website is friendly' | ||
| 111 | ); | ||
| 112 | |||
| 113 | =head1 PUBLIC PROPERTIES | ||
| 114 | |||
| 115 | =head2 ua | ||
| 116 | |||
| 117 | Returns an L<ASP4::UserAgent> that can be used to interact with pages on your website. | ||
| 118 | |||
| 119 | =head2 context | ||
| 120 | |||
| 121 | Returns the current instance of L<ASP4::HTTPContext> in use. | ||
| 122 | |||
| 123 | =head2 config | ||
| 124 | |||
| 125 | Returns the L<ASP4::Config> object for the web application. | ||
| 126 | |||
| 127 | =head2 properties | ||
| 128 | |||
| 129 | Returns an object representing your C</etc/properties.yaml> file. | ||
| 130 | |||
| 131 | =head2 data | ||
| 132 | |||
| 133 | Returns an object representing your C</etc/test_fixtures.yaml> file. | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||