We appear to have a bug where if lazy => 1 isn't set an exception
[catagits/Catalyst-Runtime.git] / t / lib / TestApp.pm
1 package TestApp;
2
3 use strict;
4 use Catalyst qw/
5     Test::MangleDollarUnderScore
6     Test::Errors 
7     Test::Headers 
8     Test::Plugin
9     Test::Inline
10     +TestApp::Plugin::FullyQualified
11     +TestApp::Plugin::AddDispatchTypes
12     +TestApp::Role
13 /;
14 use Catalyst::Utils;
15
16 use Moose;
17 use namespace::autoclean;
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( name => 'TestApp', root => '/some/dir', use_request_uri_for_path => 1 );
46
47 if ($::setup_leakchecker && eval { Class::MOP::load_class('CatalystX::LeakChecker'); 1 }) {
48     with 'CatalystX::LeakChecker';
49
50     has leaks => (
51         is      => 'ro',
52         default => sub { [] },
53     );
54 }
55
56 sub found_leaks {
57     my ($ctx, @leaks) = @_;
58     push @{ $ctx->leaks }, @leaks;
59 }
60
61 sub count_leaks {
62     my ($ctx) = @_;
63     return scalar @{ $ctx->leaks };
64 }
65
66 TestApp->setup;
67
68 sub execute {
69     my $c      = shift;
70     my $class  = ref( $c->component( $_[0] ) ) || $_[0];
71     my $action = $_[1]->reverse;
72
73     my $method;
74
75     if ( $action =~ /->(\w+)$/ ) {
76         $method = $1;
77     }
78     elsif ( $action =~ /\/(\w+)$/ ) {
79         $method = $1;
80     }
81     elsif ( $action =~ /^(\w+)$/ ) {
82         $method = $action;
83     }
84
85     if ( $class && $method && $method !~ /^_/ ) {
86         my $executed = sprintf( "%s->%s", $class, $method );
87         my @executed = $c->response->headers->header('X-Catalyst-Executed');
88         push @executed, $executed;
89         $c->response->headers->header(
90             'X-Catalyst-Executed' => join ', ',
91             @executed
92         );
93     }
94     no warnings 'recursion';
95     return $c->SUPER::execute(@_);
96 }
97
98 # Replace the very large HTML error page with
99 # useful info if something crashes during a test
100 sub finalize_error {
101     my $c = shift;
102     
103     $c->next::method(@_);
104     
105     $c->res->status(500);
106     $c->res->body( 'FATAL ERROR: ' . join( ', ', @{ $c->error } ) );
107 }
108
109 {
110     no warnings 'redefine';
111     sub Catalyst::Log::error { }
112 }
113
114 # Make sure we can load Inline plugins. 
115
116 package Catalyst::Plugin::Test::Inline;
117
118 use strict;
119
120 use base qw/Class::Data::Inheritable/;
121
122 1;