adapting unit_core_mvc.t to work (still broken)
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_mvc.t
1 use Test::More;
2 use strict;
3 use warnings;
4 use Moose::Meta::Class;
5
6 use_ok('Catalyst');
7
8 our @complist =
9   map { "MyMVCTestApp::$_"; }
10   qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
11
12 foreach my $comp (@complist) {
13     Moose::Meta::Class->create(
14         $comp =>
15             version => '0.1',
16     );
17 }
18 our $warnings = 0;
19 our $loaded   = 0;
20
21 Moose::Meta::Class->create('Some::Test::Object');
22
23 Moose::Meta::Class->create(
24     'MyMVCTestApp::Model::Test::Object' =>
25         superclasses => [ 'Catalyst::Model', 'Some::Test::Object' ],
26 );
27
28 {
29     package MyMVCTestApp;
30
31     use base qw/Catalyst/;
32
33     sub locate_components {
34         return (@::complist, 'MyMVCTestApp::Model::Test::Object');
35     }
36
37     no warnings 'redefine';
38     *Catalyst::Log::warn = sub { $::warnings++ };
39     *Catalyst::Utils::ensure_class_loaded = sub { $::loaded++ if Class::MOP::is_class_loaded(shift) };
40
41     __PACKAGE__->setup;
42 }
43
44 ok( $warnings, 'Issues deprecated warnings' );
45 is( $loaded, scalar @complist + 1, 'Loaded all components' );
46
47 is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' );
48
49 is( MyMVCTestApp->controller('Controller'),
50     'MyMVCTestApp::C::Controller', 'C::Controller ok' );
51
52 is( MyMVCTestApp->model('Model'), 'MyMVCTestApp::M::Model', 'M::Model ok' );
53
54 is( MyMVCTestApp->model('Dummy::Model'), 'MyMVCTestApp::Model::Dummy::Model', 'Model::Dummy::Model ok' );
55
56 isa_ok( MyMVCTestApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' );
57
58 is( MyMVCTestApp->controller('Model::Dummy::Model'), 'MyMVCTestApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' );
59
60 is( MyMVCTestApp->view('V'), 'MyMVCTestApp::View::V', 'View::V ok' );
61
62 is( MyMVCTestApp->controller('C'), 'MyMVCTestApp::Controller::C', 'Controller::C ok' );
63
64 is( MyMVCTestApp->model('M'), 'MyMVCTestApp::Model::M', 'Model::M ok' );
65
66 # failed search
67 {
68     is( MyMVCTestApp->model('DNE'), undef, 'undef for invalid search' );
69 }
70
71 is_deeply( [ sort MyMVCTestApp->views ],
72            [ qw/V View/ ],
73            'views ok' );
74
75 is_deeply( [ sort MyMVCTestApp->controllers ],
76            [ qw/C Controller Model::Dummy::Model/ ],
77            'controllers ok');
78
79 is_deeply( [ sort MyMVCTestApp->models ],
80            [ qw/Dummy::Model M Model Test::Object/ ],
81            'models ok');
82
83 {
84     $warnings = 0;
85
86     like (MyMVCTestApp->view , qr/^MyMVCTestApp\::(V|View)\::/ , 'view() with no defaults returns *something*');
87     ok( $warnings, 'view() w/o a default is random, warnings thrown' );
88 }
89
90 is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok');
91
92 my $view = bless {} , 'MyMVCTestApp::View::V';
93 is ( bless ({stash=>{current_view_instance=> $view }}, 'MyMVCTestApp')->view , $view, 'current_view_instance ok');
94
95 is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp::V::View' }}, 'MyMVCTestApp')->view , $view,
96   'current_view_instance precedes current_view ok');
97
98 {
99     $warnings = 0;
100
101     ok( my $model = MyMVCTestApp->model );
102
103     ok( (($model =~ /^MyMVCTestApp\::(M|Model)\::/) ||
104         $model->isa('Some::Test::Object')),
105         'model() with no defaults returns *something*' );
106
107     ok( $warnings, 'model() w/o a default is random, warnings thrown' );
108 }
109
110 is ( bless ({stash=>{current_model=>'M'}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'current_model ok');
111
112 my $model = bless {} , 'MyMVCTestApp::Model::M';
113 is ( bless ({stash=>{current_model_instance=> $model }}, 'MyMVCTestApp')->model , $model, 'current_model_instance ok');
114
115 is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyMVCTestApp::M::Model' }}, 'MyMVCTestApp')->model , $model,
116   'current_model_instance precedes current_model ok');
117
118 MyMVCTestApp->config->{default_view} = 'V';
119 is ( bless ({stash=>{}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'default_view ok');
120 is ( MyMVCTestApp->view , 'MyMVCTestApp::View::V', 'default_view in class method ok');
121
122 MyMVCTestApp->config->{default_model} = 'M';
123 is ( bless ({stash=>{}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'default_model ok');
124 is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class method ok');
125
126 # regexp behavior tests
127 {
128     # is_deeply is used because regexp behavior means list context
129     is_deeply( [ MyMVCTestApp->view( qr{^V[ie]+w$} ) ], [ 'MyMVCTestApp::V::View' ], 'regexp view ok' );
130     is_deeply( [ MyMVCTestApp->controller( qr{Dummy\::Model$} ) ], [ 'MyMVCTestApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
131     is_deeply( [ MyMVCTestApp->model( qr{Dum{2}y} ) ], [ 'MyMVCTestApp::Model::Dummy::Model' ], 'regexp model ok' );
132
133     # object w/ qr{}
134     is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
135
136     {
137         $warnings = 0;
138
139         # object w/ regexp fallback
140         is_deeply( [ MyMVCTestApp->model( 'Test' ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
141         ok( $warnings, 'regexp fallback warnings' );
142     }
143
144     is_deeply( [ MyMVCTestApp->view('MyMVCTestApp::V::View$') ], [ 'MyMVCTestApp::V::View' ], 'Explicit return ok');
145     is_deeply( [ MyMVCTestApp->controller('MyMVCTestApp::C::Controller$') ], [ 'MyMVCTestApp::C::Controller' ], 'Explicit return ok');
146     is_deeply( [ MyMVCTestApp->model('MyMVCTestApp::M::Model$') ], [ 'MyMVCTestApp::M::Model' ], 'Explicit return ok');
147 }
148
149 {
150     my @expected = qw( MyMVCTestApp::C::Controller MyMVCTestApp::Controller::C );
151     is_deeply( [ sort MyMVCTestApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' );
152 }
153
154 {
155     my @expected = qw( MyMVCTestApp::V::View MyMVCTestApp::View::V );
156     is_deeply( [ sort MyMVCTestApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' );
157 }
158
159 {
160     my @expected = qw( MyMVCTestApp::M::Model MyMVCTestApp::Model::M );
161     is_deeply( [ sort MyMVCTestApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' );
162 }
163
164 # failed search
165 {
166     is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' );
167 }
168
169 #checking @args passed to ACCEPT_CONTEXT
170 {
171     my $args;
172
173     {
174         no warnings 'once';
175         *MyMVCTestApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
176         *MyMVCTestApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
177     }
178
179     my $c = bless {}, 'MyMVCTestApp';
180
181     # test accept-context with class rather than instance
182     MyMVCTestApp->model('M', qw/foo bar/);
183     is_deeply($args, [qw/foo bar/], 'MyMVCTestApp->model args passed to ACCEPT_CONTEXT ok');
184
185
186     $c->model('M', qw/foo bar/);
187     is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
188
189     my $x = $c->view('V', qw/foo2 bar2/);
190     is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
191
192     # regexp fallback
193     $c->view('::View::V', qw/foo3 bar3/);
194     is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok');
195
196
197 }
198
199 {
200     my $warn = '';
201     no warnings 'redefine';
202     local *Catalyst::Log::warn = sub { $warn .= $_[1] };
203
204     is_deeply (MyMVCTestApp->controller('MyMVCTestApp::Controller::C'),
205         MyMVCTestApp->components->{'MyMVCTestApp::Controller::C'},
206         'controller by fully qualified name ok');
207
208     # You probably meant $c->controller('C') instead of $c->controller({'MyMVCTestApp::Controller::C'})
209     my ($suggested_comp_name, $orig_comp_name) = $warn =~ /You probably meant (.*) instead of (.*) /;
210     isnt($suggested_comp_name, $orig_comp_name, 'suggested fix in warning for fully qualified component names makes sense' );
211 }
212
213 {
214     package MyApp::WithoutRegexFallback;
215
216     use base qw/Catalyst/;
217
218     __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
219
220     __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
221         qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
222
223     # allow $c->log->warn to work
224     __PACKAGE__->setup_log;
225 }
226
227 {
228     # test if non-regex component retrieval still works
229     is( MyApp::WithoutRegexFallback->controller('Another::Foo'),
230         'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found');
231 }
232
233 {
234     my $warnings = 0;
235     no warnings 'redefine';
236     local *Catalyst::Log::warn = sub { $warnings++ };
237
238     # try to get nonexisting object w/o regexp fallback
239     is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');
240     ok( !$warnings, 'no regexp fallback warnings' );
241 }
242
243 done_testing();