updated C::Test and documented changes
[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 $ENV{CATALYST_ENGINE} = 'Test';
10
11 =head1 NAME
12
13 Catalyst::Test - Test Catalyst applications
14
15 =head1 SYNOPSIS
16
17     # Helper
18     script/test.pl
19
20     # Tests
21     use Catalyst::Test 'TestApp';
22     request('index.html');
23     get('index.html');
24
25     # Tests with inline apps need to use Catalyst::Engine::Test
26     package TestApp;
27
28     use Catalyst qw[-Engine=Test];
29
30     __PACKAGE__->action(
31         foo => sub {
32             my ( $self, $c ) = @_;
33             $c->res->output('bar');
34         }
35     );
36
37     package main;
38
39     use Test::More tests => 1;
40     use Catalyst::Test 'TestApp';
41
42     ok( get('/foo') =~ /bar/ );
43
44
45 =head1 DESCRIPTION
46
47 Test Catalyst applications.
48
49 =head2 METHODS
50
51 =head3 get
52
53 Returns the content.
54
55     my $content = get('foo/bar?test=1');
56
57 =head3 request
58
59 Returns a C<HTTP::Response> object.
60
61     my $res =request('foo/bar?test=1');
62
63 =cut
64
65 sub import {
66     my $self = shift;
67     if ( $class = shift ) {
68         $class->require;
69         unless ( $INC{'Test/Builder.pm'} ) {
70             die qq/Couldn't load "$class", "$@"/ if $@;
71         }
72
73         no strict 'refs';
74         my $caller = caller(0);
75         *{"$caller\::request"} = sub { $class->run(@_) };
76         *{"$caller\::get"}     = sub { $class->run(@_)->content };
77     }
78 }
79
80 =head1 SEE ALSO
81
82 L<Catalyst>.
83
84 =head1 AUTHOR
85
86 Sebastian Riedel, C<sri@cpan.org>
87
88 =head1 COPYRIGHT
89
90 This program is free software, you can redistribute it and/or modify it under
91 the same terms as Perl itself.
92
93 =cut
94
95 1;