Merge 'trunk' into 'more_metaclass_compat'
[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 # Test bug found when re-adjusting the metaclass compat code in Moose
48 # in 292360. Test added to Moose in 4b760d6, but leave this attribute
49 # above ->setup so we have some generated methods to be double sure.
50 has an_attribute_before_we_change_base_classes => ( is => 'ro');
51
52 if ($::setup_leakchecker && eval { Class::MOP::load_class('CatalystX::LeakChecker'); 1 }) {
53     with 'CatalystX::LeakChecker';
54
55     has leaks => (
56         is      => 'ro',
57         default => sub { [] },
58     );
59 }
60
61 sub found_leaks {
62     my ($ctx, @leaks) = @_;
63     push @{ $ctx->leaks }, @leaks;
64 }
65
66 sub count_leaks {
67     my ($ctx) = @_;
68     return scalar @{ $ctx->leaks };
69 }
70
71 TestApp->setup;
72
73 sub execute {
74     my $c      = shift;
75     my $class  = ref( $c->component( $_[0] ) ) || $_[0];
76     my $action = $_[1]->reverse;
77
78     my $method;
79
80     if ( $action =~ /->(\w+)$/ ) {
81         $method = $1;
82     }
83     elsif ( $action =~ /\/(\w+)$/ ) {
84         $method = $1;
85     }
86     elsif ( $action =~ /^(\w+)$/ ) {
87         $method = $action;
88     }
89
90     if ( $class && $method && $method !~ /^_/ ) {
91         my $executed = sprintf( "%s->%s", $class, $method );
92         my @executed = $c->response->headers->header('X-Catalyst-Executed');
93         push @executed, $executed;
94         $c->response->headers->header(
95             'X-Catalyst-Executed' => join ', ',
96             @executed
97         );
98     }
99     no warnings 'recursion';
100     return $c->SUPER::execute(@_);
101 }
102
103 # Replace the very large HTML error page with
104 # useful info if something crashes during a test
105 sub finalize_error {
106     my $c = shift;
107     
108     $c->next::method(@_);
109     
110     $c->res->status(500);
111     $c->res->body( 'FATAL ERROR: ' . join( ', ', @{ $c->error } ) );
112 }
113
114 {
115     no warnings 'redefine';
116     sub Catalyst::Log::error { }
117 }
118
119 # Make sure we can load Inline plugins. 
120
121 package Catalyst::Plugin::Test::Inline;
122
123 use strict;
124
125 use base qw/Class::Data::Inheritable/;
126
127 1;