6 no warnings 'redefine';
7 *Catalyst::Utils::ensure_class_loaded = sub { };
10 use Moose::Meta::Class;
12 our @complist_suffix = qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
14 our @complist = map { "MyMVCTestApp::$_" } @complist_suffix;
16 foreach my $comp (@complist) {
17 Moose::Meta::Class->create(
24 Moose::Meta::Class->create('Some::Test::Object');
26 Moose::Meta::Class->create(
27 'MyMVCTestApp::Model::Test::Object' =>
28 superclasses => [ 'Catalyst::Model', 'Some::Test::Object' ],
34 use base qw/Catalyst/;
36 no warnings 'redefine';
38 local *Catalyst::IOC::Container::build_locate_components_service = sub {
41 return Bread::Board::BlockInjection->new(
42 lifecycle => 'Singleton',
43 name => 'locate_components',
45 return [@complist, 'MyMVCTestApp::Model::Test::Object'];
50 local *Catalyst::Log::warn = sub { $warnings++ };
55 ok( $warnings, 'Issues deprecated warnings' );
56 is( @{[ MyMVCTestApp->component_list ]}, scalar @complist + 1, 'Loaded all components' );
59 package MyStringThing;
61 use overload '""' => sub { $_[0]->{string} }, fallback => 1;
64 is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' );
66 is( MyMVCTestApp->controller('Controller'),
67 'MyMVCTestApp::C::Controller', 'C::Controller ok' );
69 is( MyMVCTestApp->model('Model'), 'MyMVCTestApp::M::Model', 'M::Model ok' );
71 is( MyMVCTestApp->model('Dummy::Model'), 'MyMVCTestApp::Model::Dummy::Model', 'Model::Dummy::Model ok' );
73 isa_ok( MyMVCTestApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' );
75 is( MyMVCTestApp->controller('Model::Dummy::Model'), 'MyMVCTestApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' );
77 is( MyMVCTestApp->view('V'), 'MyMVCTestApp::View::V', 'View::V ok' );
79 is( MyMVCTestApp->controller('C'), 'MyMVCTestApp::Controller::C', 'Controller::C ok' );
81 is( MyMVCTestApp->model('M'), 'MyMVCTestApp::Model::M', 'Model::M ok' );
85 is( MyMVCTestApp->model('DNE'), undef, 'undef for invalid search' );
88 is_deeply( [ sort MyMVCTestApp->views ],
92 is_deeply( [ sort MyMVCTestApp->controllers ],
93 [ qw/C Controller Model::Dummy::Model/ ],
96 is_deeply( [ sort MyMVCTestApp->models ],
97 [ qw/Dummy::Model M Model Test::Object/ ],
102 no warnings 'redefine';
103 local *Catalyst::Log::warn = sub { $warnings++ };
105 is( MyMVCTestApp->view , undef, 'view() w/o a default is undef' );
106 ok( $warnings, 'warnings thrown for view() w/o a default' );
109 is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok');
111 my $view = bless {} , 'MyMVCTestApp::View::V';
112 is ( bless ({stash=>{current_view_instance=> $view }}, 'MyMVCTestApp')->view , $view, 'current_view_instance ok');
114 is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp::V::View' }}, 'MyMVCTestApp')->view , $view,
115 'current_view_instance precedes current_view ok');
119 no warnings 'redefine';
120 local *Catalyst::Log::warn = sub { $warnings++ };
122 is( MyMVCTestApp->model, undef, 'model() w/o a default is undef' );
123 ok( $warnings, 'warnings thrown for model() w/o a default' );
126 is ( bless ({stash=>{current_model=>'M'}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'current_model ok');
128 my $model = bless {} , 'MyMVCTestApp::Model::M';
129 is ( bless ({stash=>{current_model_instance=> $model }}, 'MyMVCTestApp')->model , $model, 'current_model_instance ok');
131 is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyMVCTestApp::M::Model' }}, 'MyMVCTestApp')->model , $model,
132 'current_model_instance precedes current_model ok');
136 use lib "$Bin/../lib";
138 use Catalyst::Test 'TestAppController';
140 is( get('/foo/test_controller'), 'bar', 'controller() with empty args returns current controller' );
143 our @complist_default_view =
144 map { "MyMVCTestAppDefaultView::$_" } @complist_suffix;
147 package MyMVCTestAppDefaultView;
149 use base qw/Catalyst/;
150 no warnings 'redefine';
152 local *Catalyst::IOC::Container::build_locate_components_service = sub {
155 return Bread::Board::BlockInjection->new(
156 lifecycle => 'Singleton',
157 name => 'locate_components',
159 return \@complist_default_view;
163 local *Catalyst::Log::warn = sub { $warnings++ };
165 __PACKAGE__->config( default_view => 'V' );
170 is( bless ({stash=>{}}, 'MyMVCTestAppDefaultView')->view, 'MyMVCTestAppDefaultView::View::V', 'default_view ok' );
171 is( MyMVCTestAppDefaultView->view , 'MyMVCTestAppDefaultView::View::V', 'default_view in class method ok' );
173 our @complist_default_model =
174 map { "MyMVCTestAppDefaultModel::$_" } @complist_suffix;
177 package MyMVCTestAppDefaultModel;
179 use base qw/Catalyst/;
181 no warnings 'redefine';
183 local *Catalyst::IOC::Container::build_locate_components_service = sub {
186 return Bread::Board::BlockInjection->new(
187 lifecycle => 'Singleton',
188 name => 'locate_components',
190 return \@complist_default_model;
194 local *Catalyst::Log::warn = sub { $warnings++ };
196 __PACKAGE__->config( default_model => 'M' );
201 is( bless ({stash=>{}}, 'MyMVCTestAppDefaultModel')->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model ok' );
202 is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model in class method ok' );
204 # regexp behavior tests
206 # is_deeply is used because regexp behavior means list context
207 is_deeply( [ MyMVCTestApp->view( qr{^V[ie]+w$} ) ], [ 'MyMVCTestApp::V::View' ], 'regexp view ok' );
208 is_deeply( [ MyMVCTestApp->controller( qr{Dummy\::Model$} ) ], [ 'MyMVCTestApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
209 is_deeply( [ MyMVCTestApp->model( qr{Dum{2}y} ) ], [ 'MyMVCTestApp::Model::Dummy::Model' ], 'regexp model ok' );
212 is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
214 is_deeply([ MyMVCTestApp->model( bless({ string => 'Model' }, 'MyStringThing') ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::M::Model'} ], 'Explicit model search with overloaded object');
218 no warnings 'redefine';
219 local *Catalyst::Log::warn = sub { $warnings++ };
221 # object w/ regexp fallback
222 is_deeply( [ MyMVCTestApp->model( bless({ string => 'Test' }, 'MyStringThing') ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
223 ok( $warnings, 'regexp fallback warnings' );
228 no warnings 'redefine';
229 local *Catalyst::Log::warn = sub { $warnings++ };
231 # object w/ regexp fallback
232 is( MyMVCTestApp->model( 'Test' ), undef, 'no regexp fallback' );
233 ok( $warnings, 'regexp fallback warnings' );
236 is( MyMVCTestApp->view('MyMVCTestApp::V::View$'), undef, 'no regexp fallback');
238 is( MyMVCTestApp->controller('MyMVCTestApp::C::Controller$'), undef, 'no regexp fallback');
240 is( MyMVCTestApp->model('MyMVCTestApp::M::Model$'), undef, 'no regexp fallback');
244 my @expected = qw( MyMVCTestApp::C::Controller MyMVCTestApp::Controller::C );
245 is_deeply( [ sort MyMVCTestApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' );
249 my @expected = qw( MyMVCTestApp::V::View MyMVCTestApp::View::V );
250 is_deeply( [ sort MyMVCTestApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' );
254 my @expected = qw( MyMVCTestApp::M::Model MyMVCTestApp::Model::M );
255 is_deeply( [ sort MyMVCTestApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' );
260 is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' );
263 #checking @args passed to ACCEPT_CONTEXT
269 *MyMVCTestApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
270 *MyMVCTestApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
273 my $c = bless {}, 'MyMVCTestApp';
275 # test accept-context with class rather than instance
276 MyMVCTestApp->model('M', qw/foo bar/);
277 is_deeply($args, [qw/foo bar/], 'MyMVCTestApp->model args passed to ACCEPT_CONTEXT ok');
280 $c->model('M', qw/foo bar/);
281 is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
283 my $x = $c->view('V', qw/foo2 bar2/);
284 is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
289 package MyApp::WithoutRegexFallback;
291 use base qw/Catalyst/;
293 no warnings 'redefine';
295 __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
297 __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
298 qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
300 # allow $c->log->warn to work
301 __PACKAGE__->setup_log;
305 # test if non-regex component retrieval still works
306 is( MyApp::WithoutRegexFallback->controller('Another::Foo'),
307 'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found');
312 no warnings 'redefine';
313 local *Catalyst::Log::warn = sub { $warnings++ };
315 # try to get nonexisting object w/o regexp fallback
316 is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');