Don't set up the moose controller unless Moose is available.
[catagits/Catalyst-Runtime.git] / t / lib / TestApp.pm
1 package TestApp;
2
3 use strict;
4 use Catalyst qw/
5     Test::Errors 
6     Test::Headers 
7     Test::Plugin
8     Test::Inline
9     +TestApp::Plugin::FullyQualified
10 /;
11 use Catalyst::Utils;
12
13 our $VERSION = '0.01';
14
15 TestApp->config( name => 'TestApp', root => '/some/dir' );
16
17 unless (eval 'require Moose; 1') {
18     TestApp->config(setup_components => { except => 'TestApp::Controller::Moose' });
19 }
20
21 TestApp->setup;
22
23 sub index : Private {
24     my ( $self, $c ) = @_;
25     $c->res->body('root index');
26 }
27
28 sub global_action : Private {
29     my ( $self, $c ) = @_;
30     $c->forward('TestApp::View::Dump::Request');
31 }
32
33 sub execute {
34     my $c      = shift;
35     my $class  = ref( $c->component( $_[0] ) ) || $_[0];
36     my $action = "$_[1]";
37
38     my $method;
39
40     if ( $action =~ /->(\w+)$/ ) {
41         $method = $1;
42     }
43     elsif ( $action =~ /\/(\w+)$/ ) {
44         $method = $1;
45     }
46     elsif ( $action =~ /^(\w+)$/ ) {
47         $method = $action;
48     }
49
50     if ( $class && $method && $method !~ /^_/ ) {
51         my $executed = sprintf( "%s->%s", $class, $method );
52         my @executed = $c->response->headers->header('X-Catalyst-Executed');
53         push @executed, $executed;
54         $c->response->headers->header(
55             'X-Catalyst-Executed' => join ', ',
56             @executed
57         );
58     }
59
60     return $c->SUPER::execute(@_);
61 }
62
63 # Replace the very large HTML error page with
64 # useful info if something crashes during a test
65 sub finalize_error {
66     my $c = shift;
67     
68     $c->NEXT::finalize_error(@_);
69     
70     $c->res->status(500);
71     $c->res->body( 'FATAL ERROR: ' . join( ', ', @{ $c->error } ) );
72 }
73
74 sub class_forward_test_method :Private {
75     my ( $self, $c ) = @_;
76     $c->response->headers->header( 'X-Class-Forward-Test-Method' => 1 );
77 }
78
79 sub class_go_test_method :Private {
80     my ( $self, $c ) = @_;
81     $c->response->headers->header( 'X-Class-Go-Test-Method' => 1 );
82 }
83
84 sub class_visit_test_method :Private {
85     my ( $self, $c ) = @_;
86     $c->response->headers->header( 'X-Class-Visit-Test-Method' => 1 );
87 }
88
89 sub loop_test : Local {
90     my ( $self, $c ) = @_;
91
92     for( 1..1001 ) {
93         $c->forward( 'class_forward_test_method' );
94     }
95 }
96
97 sub recursion_test : Local {
98     my ( $self, $c ) = @_;
99     $c->forward( 'recursion_test' );
100 }
101
102 {
103     no warnings 'redefine';
104     sub Catalyst::Log::error { }
105 }
106
107 # Make sure we can load Inline plugins. 
108
109 package Catalyst::Plugin::Test::Inline;
110
111 use strict;
112
113 use base qw/Catalyst::Base Class::Data::Inheritable/;
114
115 1;