added C::E::CGI::NPH
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
1 package Catalyst::Test;
2
3 use strict;
4 use UNIVERSAL::require;
5
6 require Catalyst;
7
8 my $class;
9
10 =head1 NAME
11
12 Catalyst::Test - Test Catalyst applications
13
14 =head1 SYNOPSIS
15
16     # Helper
17     script/test.pl
18
19     # Tests
20     use Catalyst::Test 'TestApp';
21     request('index.html');
22     get('index.html');
23
24     # Request
25     perl -MCatalyst::Test=MyApp -e1 index.html
26
27 =head1 DESCRIPTION
28
29 Test Catalyst applications.
30
31 =head2 METHODS
32
33 =head3 get
34
35 Returns the content.
36
37     my $content = get('foo/bar?test=1');
38
39 =head3 request
40
41 Returns a C<HTTP::Response> object.
42
43     my $res =request('foo/bar?test=1');
44
45 =cut
46
47 {
48     no warnings;
49     CHECK {
50         if ( ( caller(0) )[1] eq '-e' ) {
51             print request( $ARGV[0] || 'http://localhost' )->content;
52         }
53     }
54 }
55
56 sub import {
57     my $self = shift;
58     if ( $class = shift ) {
59         $class->require;
60         unless ( $INC{'Test/Builder.pm'} ) {
61             die qq/Couldn't load "$class", "$@"/ if $@;
62         }
63
64         no strict 'refs';
65
66         unless ( $class->engine->isa('Catalyst::Engine::Test') ) {
67             require Catalyst::Engine::Test;
68             splice( @{"$class\::ISA"}, @{"$class\::ISA"} - 1,
69                 0, 'Catalyst::Engine::Test' );
70         }
71
72         my $caller = caller(0);
73         *{"$caller\::request"} = sub { $class->run(@_) };
74         *{"$caller\::get"}     = sub { $class->run(@_)->content };
75     }
76 }
77
78 =head1 SEE ALSO
79
80 L<Catalyst>.
81
82 =head1 AUTHOR
83
84 Sebastian Riedel, C<sri@cpan.org>
85
86 =head1 COPYRIGHT
87
88 This program is free software, you can redistribute it and/or modify it under
89 the same terms as Perl itself.
90
91 =cut
92
93 1;