call to $class->import in Catalyst::Test
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
1 package Catalyst::Test;
2
3 use strict;
4 use UNIVERSAL::require;
5
6 $ENV{CATALYST_ENGINE} = 'Test';
7
8 =head1 NAME
9
10 Catalyst::Test - Test Catalyst applications
11
12 =head1 SYNOPSIS
13
14     # Helper
15     script/test.pl
16
17     # Tests
18     use Catalyst::Test 'TestApp';
19     request('index.html');
20     get('index.html');
21
22     # Tests with inline apps need to use Catalyst::Engine::Test
23     package TestApp;
24
25     use Catalyst qw[-Engine=Test];
26
27     __PACKAGE__->action(
28         foo => sub {
29             my ( $self, $c ) = @_;
30             $c->res->output('bar');
31         }
32     );
33
34     package main;
35
36     use Test::More tests => 1;
37     use Catalyst::Test 'TestApp';
38
39     ok( get('/foo') =~ /bar/ );
40
41
42 =head1 DESCRIPTION
43
44 Test Catalyst applications.
45
46 =head2 METHODS
47
48 =head3 get
49
50 Returns the content.
51
52     my $content = get('foo/bar?test=1');
53
54 =head3 request
55
56 Returns a C<HTTP::Response> object.
57
58     my $res =request('foo/bar?test=1');
59
60 =cut
61
62 sub import {
63     my $self = shift;
64     if ( my $class = shift ) {
65         $class->require;
66         unless ( $INC{'Test/Builder.pm'} ) {
67             die qq/Couldn't load "$class", "$@"/ if $@;
68         }
69         $class->import;
70
71         no strict 'refs';
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;