TODO item done, whitespace fix
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_mvc.t
CommitLineData
a9df50f8 1use Test::More;
5d50f369 2use strict;
3use warnings;
e10b40fd 4
6f9dfc99 5{
6 no warnings 'redefine';
7 *Catalyst::Utils::ensure_class_loaded = sub { };
8}
9d718832 9
a9df50f8 10use Moose::Meta::Class;
5d50f369 11
a2c0d071 12our @complist_suffix = qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
13
14our @complist = map { "MyMVCTestApp::$_" } @complist_suffix;
5d50f369 15
a9df50f8 16foreach my $comp (@complist) {
17 Moose::Meta::Class->create(
18 $comp =>
19 version => '0.1',
20 );
21}
22our $warnings = 0;
23our $loaded = 0;
24
25Moose::Meta::Class->create('Some::Test::Object');
5d50f369 26
a9df50f8 27Moose::Meta::Class->create(
28 'MyMVCTestApp::Model::Test::Object' =>
29 superclasses => [ 'Catalyst::Model', 'Some::Test::Object' ],
30);
31
32{
5d50f369 33 package MyMVCTestApp;
34
35 use base qw/Catalyst/;
36
a9df50f8 37 no warnings 'redefine';
9d718832 38
39 local *Catalyst::IOC::Container::build_locate_components_service = sub {
40 my $self = shift;
41
42 return Bread::Board::BlockInjection->new(
43 lifecycle => 'Singleton',
44 name => 'locate_components',
45 block => sub {
46 return [@complist, 'MyMVCTestApp::Model::Test::Object'];
47
48 },
49 );
50 };
ccc6273e 51 local *Catalyst::Log::warn = sub { $warnings++ };
9d718832 52 *Class::MOP::load_class = sub {
e10b40fd 53 my $class = shift;
ccc6273e 54 $loaded++
e10b40fd 55 if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestApp/
56 };
5d50f369 57
a9df50f8 58 __PACKAGE__->setup;
5d50f369 59}
60
a9df50f8 61ok( $warnings, 'Issues deprecated warnings' );
62is( $loaded, scalar @complist + 1, 'Loaded all components' );
63
5d50f369 64is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' );
65
66is( MyMVCTestApp->controller('Controller'),
67 'MyMVCTestApp::C::Controller', 'C::Controller ok' );
68
69is( MyMVCTestApp->model('Model'), 'MyMVCTestApp::M::Model', 'M::Model ok' );
70
71is( MyMVCTestApp->model('Dummy::Model'), 'MyMVCTestApp::Model::Dummy::Model', 'Model::Dummy::Model ok' );
72
73isa_ok( MyMVCTestApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' );
74
75is( MyMVCTestApp->controller('Model::Dummy::Model'), 'MyMVCTestApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' );
76
77is( MyMVCTestApp->view('V'), 'MyMVCTestApp::View::V', 'View::V ok' );
78
79is( MyMVCTestApp->controller('C'), 'MyMVCTestApp::Controller::C', 'Controller::C ok' );
80
81is( MyMVCTestApp->model('M'), 'MyMVCTestApp::Model::M', 'Model::M ok' );
82
83# failed search
84{
85 is( MyMVCTestApp->model('DNE'), undef, 'undef for invalid search' );
86}
87
88is_deeply( [ sort MyMVCTestApp->views ],
89 [ qw/V View/ ],
90 'views ok' );
91
92is_deeply( [ sort MyMVCTestApp->controllers ],
93 [ qw/C Controller Model::Dummy::Model/ ],
94 'controllers ok');
95
96is_deeply( [ sort MyMVCTestApp->models ],
97 [ qw/Dummy::Model M Model Test::Object/ ],
98 'models ok');
99
100{
ccc6273e 101 my $warnings = 0;
102 no warnings 'redefine';
103 local *Catalyst::Log::warn = sub { $warnings++ };
5d50f369 104
a2c0d071 105 is( MyMVCTestApp->view , undef, 'view() w/o a default is undef' );
106 ok( $warnings, 'warnings thrown for view() w/o a default' );
5d50f369 107}
108
109is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok');
110
111my $view = bless {} , 'MyMVCTestApp::View::V';
112is ( bless ({stash=>{current_view_instance=> $view }}, 'MyMVCTestApp')->view , $view, 'current_view_instance ok');
113
114is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp::V::View' }}, 'MyMVCTestApp')->view , $view,
115 'current_view_instance precedes current_view ok');
116
117{
ccc6273e 118 my $warnings = 0;
119 no warnings 'redefine';
120 local *Catalyst::Log::warn = sub { $warnings++ };
5d50f369 121
a2c0d071 122 is( MyMVCTestApp->model, undef, 'model() w/o a default is undef' );
123 ok( $warnings, 'warnings thrown for model() w/o a default' );
5d50f369 124}
125
126is ( bless ({stash=>{current_model=>'M'}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'current_model ok');
127
128my $model = bless {} , 'MyMVCTestApp::Model::M';
129is ( bless ({stash=>{current_model_instance=> $model }}, 'MyMVCTestApp')->model , $model, 'current_model_instance ok');
130
131is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyMVCTestApp::M::Model' }}, 'MyMVCTestApp')->model , $model,
132 'current_model_instance precedes current_model ok');
133
e10b40fd 134{
135 use FindBin '$Bin';
136 use lib "$Bin/../lib";
137
138 use Catalyst::Test 'TestAppController';
139
140 is( get('/foo/test_controller'), 'bar', 'controller() with empty args returns current controller' );
141}
142
a2c0d071 143our @complist_default_view =
144 map { "MyMVCTestAppDefaultView::$_" } @complist_suffix;
145
146{
147 package MyMVCTestAppDefaultView;
148
149 use base qw/Catalyst/;
9d718832 150 no warnings 'redefine';
a2c0d071 151
9d718832 152 local *Catalyst::IOC::Container::build_locate_components_service = sub {
153 my $self = shift;
a2c0d071 154
9d718832 155 return Bread::Board::BlockInjection->new(
156 lifecycle => 'Singleton',
157 name => 'locate_components',
158 block => sub {
159 return \@complist_default_view;
160 },
161 );
162 };
ccc6273e 163 local *Catalyst::Log::warn = sub { $warnings++ };
9d718832 164 *Class::MOP::load_class = sub {
a2c0d071 165 my $class = shift;
ccc6273e 166 $loaded++
a2c0d071 167 if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultView/
168 };
169
170 __PACKAGE__->config( default_view => 'V' );
171
172 __PACKAGE__->setup;
173}
174
175is( bless ({stash=>{}}, 'MyMVCTestAppDefaultView')->view, 'MyMVCTestAppDefaultView::View::V', 'default_view ok' );
176is( MyMVCTestAppDefaultView->view , 'MyMVCTestAppDefaultView::View::V', 'default_view in class method ok' );
177
178our @complist_default_model =
179 map { "MyMVCTestAppDefaultModel::$_" } @complist_suffix;
180
181{
182 package MyMVCTestAppDefaultModel;
183
184 use base qw/Catalyst/;
185
a2c0d071 186 no warnings 'redefine';
9d718832 187
188 local *Catalyst::IOC::Container::build_locate_components_service = sub {
189 my $self = shift;
190
191 return Bread::Board::BlockInjection->new(
192 lifecycle => 'Singleton',
193 name => 'locate_components',
194 block => sub {
195 return \@complist_default_model;
196 },
197 );
198 };
ccc6273e 199 local *Catalyst::Log::warn = sub { $warnings++ };
9d718832 200 *Class::MOP::load_class = sub {
a2c0d071 201 my $class = shift;
ccc6273e 202 $loaded++
a2c0d071 203 if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultModel/
204 };
205
206 __PACKAGE__->config( default_model => 'M' );
207
208 __PACKAGE__->setup;
209}
5d50f369 210
a2c0d071 211is( bless ({stash=>{}}, 'MyMVCTestAppDefaultModel')->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model ok' );
212is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model in class method ok' );
5d50f369 213
214# regexp behavior tests
215{
216 # is_deeply is used because regexp behavior means list context
217 is_deeply( [ MyMVCTestApp->view( qr{^V[ie]+w$} ) ], [ 'MyMVCTestApp::V::View' ], 'regexp view ok' );
218 is_deeply( [ MyMVCTestApp->controller( qr{Dummy\::Model$} ) ], [ 'MyMVCTestApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
219 is_deeply( [ MyMVCTestApp->model( qr{Dum{2}y} ) ], [ 'MyMVCTestApp::Model::Dummy::Model' ], 'regexp model ok' );
220
221 # object w/ qr{}
222 is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
223
224 {
ccc6273e 225 my $warnings = 0;
226 no warnings 'redefine';
227 local *Catalyst::Log::warn = sub { $warnings++ };
5d50f369 228
229 # object w/ regexp fallback
5a53ef3d 230 is( MyMVCTestApp->model( 'Test' ), undef, 'no regexp fallback' );
5d50f369 231 ok( $warnings, 'regexp fallback warnings' );
232 }
233
5a53ef3d 234 is( MyMVCTestApp->view('MyMVCTestApp::V::View$'), undef, 'no regexp fallback');
235
236 is( MyMVCTestApp->controller('MyMVCTestApp::C::Controller$'), undef, 'no regexp fallback');
237
238 is( MyMVCTestApp->model('MyMVCTestApp::M::Model$'), undef, 'no regexp fallback');
5d50f369 239}
240
241{
242 my @expected = qw( MyMVCTestApp::C::Controller MyMVCTestApp::Controller::C );
243 is_deeply( [ sort MyMVCTestApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' );
244}
245
246{
247 my @expected = qw( MyMVCTestApp::V::View MyMVCTestApp::View::V );
248 is_deeply( [ sort MyMVCTestApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' );
249}
250
251{
252 my @expected = qw( MyMVCTestApp::M::Model MyMVCTestApp::Model::M );
253 is_deeply( [ sort MyMVCTestApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' );
254}
255
256# failed search
257{
258 is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' );
259}
260
261#checking @args passed to ACCEPT_CONTEXT
262{
263 my $args;
264
265 {
266 no warnings 'once';
267 *MyMVCTestApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
268 *MyMVCTestApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
269 }
270
271 my $c = bless {}, 'MyMVCTestApp';
272
273 # test accept-context with class rather than instance
274 MyMVCTestApp->model('M', qw/foo bar/);
275 is_deeply($args, [qw/foo bar/], 'MyMVCTestApp->model args passed to ACCEPT_CONTEXT ok');
276
277
278 $c->model('M', qw/foo bar/);
279 is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
280
281 my $x = $c->view('V', qw/foo2 bar2/);
282 is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
283
02fee6c6 284}
285
286{
287 package MyApp::WithoutRegexFallback;
288
289 use base qw/Catalyst/;
290
9d718832 291 no warnings 'redefine';
292
293 *Class::MOP::load_class = sub {
294 $loaded++;
295 };
296
02fee6c6 297 __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
298
299 __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
300 qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
301
302 # allow $c->log->warn to work
303 __PACKAGE__->setup_log;
304}
305
306{
307 # test if non-regex component retrieval still works
308 is( MyApp::WithoutRegexFallback->controller('Another::Foo'),
309 'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found');
310}
311
312{
313 my $warnings = 0;
314 no warnings 'redefine';
315 local *Catalyst::Log::warn = sub { $warnings++ };
316
317 # try to get nonexisting object w/o regexp fallback
318 is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');
02fee6c6 319}
a9df50f8 320
5a53ef3d 321done_testing;