update distar url
[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) {
64     require Scalar::Util;
65     require Devel::Cycle;
66
67     has leaks => (
68         is      => 'ro',
69         default => sub { [] },
70     );
71
72     sub count_leaks {
73         my ($ctx) = @_;
74         return scalar @{ $ctx->leaks };
75     }
76
77     after finalize => sub {
78         my ($ctx) = @_;
79         my @leaks;
80
81         my $weak_ctx = $ctx;
82         Scalar::Util::weaken $weak_ctx;
83
84         Devel::Cycle::find_cycle($ctx, sub {
85             my ($path) = @_;
86             push @leaks, $path
87                 if $path->[0]->[2] == $weak_ctx;
88         });
89
90         push @{ $ctx->leaks }, @leaks;
91     };
92 }
93
94 TestApp->setup;
95
96 sub execute {
97     my $c      = shift;
98     my $class  = ref( $c->component( $_[0] ) ) || $_[0];
99     my $action = $_[1]->reverse;
100
101     my $method;
102
103     if ( $action =~ /->(\w+)$/ ) {
104         $method = $1;
105     }
106     elsif ( $action =~ /\/(\w+)$/ ) {
107         $method = $1;
108     }
109     elsif ( $action =~ /^(\w+)$/ ) {
110         $method = $action;
111     }
112
113     if ( $class && $method && $method !~ /^_/ ) {
114         my $executed = sprintf( "%s->%s", $class, $method );
115         my @executed = $c->response->headers->header('X-Catalyst-Executed');
116         push @executed, $executed;
117         $c->response->headers->header(
118             'X-Catalyst-Executed' => join ', ',
119             @executed
120         );
121     }
122     no warnings 'recursion';
123     return $c->SUPER::execute(@_);
124 }
125
126 # Replace the very large HTML error page with
127 # useful info if something crashes during a test
128 sub finalize_error {
129     my $c = shift;
130
131     $c->next::method(@_);
132
133     $c->res->status(500);
134     $c->res->body( 'FATAL ERROR: ' . join( ', ', @{ $c->error } ) );
135 }
136
137 {
138     no warnings 'redefine';
139     sub Catalyst::Log::error { }
140 }
141
142 # Pretend to be Plugin::Session and hook finalize_headers to send a header
143
144 sub finalize_headers {
145     my $c = shift;
146
147     $c->res->header('X-Test-Header', 'valid');
148
149     my $call_count = $c->stash->{finalize_headers_call_count} || 0;
150     $call_count++;
151     $c->stash(finalize_headers_call_count => $call_count);
152     $c->res->header('X-Test-Header-Call-Count' => $call_count);
153
154     return $c->maybe::next::method(@_);
155 }
156
157 # Make sure we can load Inline plugins.
158
159 package Catalyst::Plugin::Test::Inline;
160 use Moose;
161
162 1;