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