merged after conflict resolution
[catagits/Catalyst-Runtime.git] / t / lib / TestApp.pm
1 package TestApp;
2 use strict;
3 use Catalyst qw/
4     Test::MangleDollarUnderScore
5     Test::Errors 
6     Test::Headers 
7     Test::Plugin
8     Test::Inline
9     +TestApp::Plugin::FullyQualified
10     +TestApp::Plugin::AddDispatchTypes
11     +TestApp::Role
12 /;
13 use Catalyst::Utils;
14
15 use Moose;
16 use namespace::autoclean;
17
18 # -----------
19 # t/aggregate/unit_core_ctx_attr.t pukes until lazy is true
20 package Greeting;
21 use Moose;
22 sub hello_notlazy { 'hello there' }
23 sub hello_lazy    { 'hello there' }
24
25 package TestApp;
26 has 'my_greeting_obj_notlazy' => (
27    is      => 'ro',
28    isa     => 'Greeting',
29    default => sub { Greeting->new() },
30    handles => [ qw( hello_notlazy ) ],
31    lazy    => 0,
32 );
33 has 'my_greeting_obj_lazy' => (
34    is      => 'ro',
35    isa     => 'Greeting',
36    default => sub { Greeting->new() },
37    handles => [ qw( hello_lazy ) ],
38    lazy    => 1,
39 );
40 # -----------
41
42 our $VERSION = '0.01';
43
44 TestApp->config( 
45     name => 'TestApp', 
46     root => '/some/dir', 
47     use_request_uri_for_path => 1, 
48     'Controller::Action::Action' => {
49         action_args => {
50             action_action_nine => { another_extra_arg => 13 }
51         }
52     },
53     encoding => 'UTF-8',
54     abort_chain_on_error_fix => 1,
55 );
56
57 # Test bug found when re-adjusting the metaclass compat code in Moose
58 # in 292360. Test added to Moose in 4b760d6, but leave this attribute
59 # above ->setup so we have some generated methods to be double sure.
60 has an_attribute_before_we_change_base_classes => ( is => 'ro');
61
62 if ($::setup_leakchecker && eval { Class::MOP::load_class('CatalystX::LeakChecker'); 1 }) {
63     with 'CatalystX::LeakChecker';
64
65     has leaks => (
66         is      => 'ro',
67         default => sub { [] },
68     );
69 }
70
71 sub found_leaks {
72     my ($ctx, @leaks) = @_;
73     push @{ $ctx->leaks }, @leaks;
74 }
75
76 sub count_leaks {
77     my ($ctx) = @_;
78     return scalar @{ $ctx->leaks };
79 }
80
81 TestApp->setup;
82
83 sub execute {
84     my $c      = shift;
85     my $class  = ref( $c->component( $_[0] ) ) || $_[0];
86     my $action = $_[1]->reverse;
87
88     my $method;
89
90     if ( $action =~ /->(\w+)$/ ) {
91         $method = $1;
92     }
93     elsif ( $action =~ /\/(\w+)$/ ) {
94         $method = $1;
95     }
96     elsif ( $action =~ /^(\w+)$/ ) {
97         $method = $action;
98     }
99
100     if ( $class && $method && $method !~ /^_/ ) {
101         my $executed = sprintf( "%s->%s", $class, $method );
102         my @executed = $c->response->headers->header('X-Catalyst-Executed');
103         push @executed, $executed;
104         $c->response->headers->header(
105             'X-Catalyst-Executed' => join ', ',
106             @executed
107         );
108     }
109     no warnings 'recursion';
110     return $c->SUPER::execute(@_);
111 }
112
113 # Replace the very large HTML error page with
114 # useful info if something crashes during a test
115 sub finalize_error {
116     my $c = shift;
117     
118     $c->next::method(@_);
119     
120     $c->res->status(500);
121     $c->res->body( 'FATAL ERROR: ' . join( ', ', @{ $c->error } ) );
122 }
123
124 {
125     no warnings 'redefine';
126     sub Catalyst::Log::error { }
127 }
128
129 # Pretend to be Plugin::Session and hook finalize_headers to send a header
130
131 sub finalize_headers {
132     my $c = shift;
133
134     $c->res->header('X-Test-Header', 'valid');
135
136     my $call_count = $c->stash->{finalize_headers_call_count} || 0;
137     $call_count++;
138     $c->stash(finalize_headers_call_count => $call_count);
139     $c->res->header('X-Test-Header-Call-Count' => $call_count);
140
141     return $c->maybe::next::method(@_);
142 }
143
144 # Make sure we can load Inline plugins. 
145
146 package Catalyst::Plugin::Test::Inline;
147
148 use strict;
149
150 use base qw/Class::Data::Inheritable/;
151
152 1;