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' );
58 is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' );
60 is( MyMVCTestApp->controller('Controller'),
61 'MyMVCTestApp::C::Controller', 'C::Controller ok' );
63 is( MyMVCTestApp->model('Model'), 'MyMVCTestApp::M::Model', 'M::Model ok' );
65 is( MyMVCTestApp->model('Dummy::Model'), 'MyMVCTestApp::Model::Dummy::Model', 'Model::Dummy::Model ok' );
67 isa_ok( MyMVCTestApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' );
69 is( MyMVCTestApp->controller('Model::Dummy::Model'), 'MyMVCTestApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' );
71 is( MyMVCTestApp->view('V'), 'MyMVCTestApp::View::V', 'View::V ok' );
73 is( MyMVCTestApp->controller('C'), 'MyMVCTestApp::Controller::C', 'Controller::C ok' );
75 is( MyMVCTestApp->model('M'), 'MyMVCTestApp::Model::M', 'Model::M ok' );
79 is( MyMVCTestApp->model('DNE'), undef, 'undef for invalid search' );
82 is_deeply( [ sort MyMVCTestApp->views ],
86 is_deeply( [ sort MyMVCTestApp->controllers ],
87 [ qw/C Controller Model::Dummy::Model/ ],
90 is_deeply( [ sort MyMVCTestApp->models ],
91 [ qw/Dummy::Model M Model Test::Object/ ],
96 no warnings 'redefine';
97 local *Catalyst::Log::warn = sub { $warnings++ };
99 is( MyMVCTestApp->view , undef, 'view() w/o a default is undef' );
100 ok( $warnings, 'warnings thrown for view() w/o a default' );
103 is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok');
105 my $view = bless {} , 'MyMVCTestApp::View::V';
106 is ( bless ({stash=>{current_view_instance=> $view }}, 'MyMVCTestApp')->view , $view, 'current_view_instance ok');
108 is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp::V::View' }}, 'MyMVCTestApp')->view , $view,
109 'current_view_instance precedes current_view ok');
113 no warnings 'redefine';
114 local *Catalyst::Log::warn = sub { $warnings++ };
116 is( MyMVCTestApp->model, undef, 'model() w/o a default is undef' );
117 ok( $warnings, 'warnings thrown for model() w/o a default' );
120 is ( bless ({stash=>{current_model=>'M'}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'current_model ok');
122 my $model = bless {} , 'MyMVCTestApp::Model::M';
123 is ( bless ({stash=>{current_model_instance=> $model }}, 'MyMVCTestApp')->model , $model, 'current_model_instance ok');
125 is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyMVCTestApp::M::Model' }}, 'MyMVCTestApp')->model , $model,
126 'current_model_instance precedes current_model ok');
130 use lib "$Bin/../lib";
132 use Catalyst::Test 'TestAppController';
134 is( get('/foo/test_controller'), 'bar', 'controller() with empty args returns current controller' );
137 our @complist_default_view =
138 map { "MyMVCTestAppDefaultView::$_" } @complist_suffix;
141 package MyMVCTestAppDefaultView;
143 use base qw/Catalyst/;
144 no warnings 'redefine';
146 local *Catalyst::IOC::Container::build_locate_components_service = sub {
149 return Bread::Board::BlockInjection->new(
150 lifecycle => 'Singleton',
151 name => 'locate_components',
153 return \@complist_default_view;
157 local *Catalyst::Log::warn = sub { $warnings++ };
159 __PACKAGE__->config( default_view => 'V' );
164 is( bless ({stash=>{}}, 'MyMVCTestAppDefaultView')->view, 'MyMVCTestAppDefaultView::View::V', 'default_view ok' );
165 is( MyMVCTestAppDefaultView->view , 'MyMVCTestAppDefaultView::View::V', 'default_view in class method ok' );
167 our @complist_default_model =
168 map { "MyMVCTestAppDefaultModel::$_" } @complist_suffix;
171 package MyMVCTestAppDefaultModel;
173 use base qw/Catalyst/;
175 no warnings 'redefine';
177 local *Catalyst::IOC::Container::build_locate_components_service = sub {
180 return Bread::Board::BlockInjection->new(
181 lifecycle => 'Singleton',
182 name => 'locate_components',
184 return \@complist_default_model;
188 local *Catalyst::Log::warn = sub { $warnings++ };
190 __PACKAGE__->config( default_model => 'M' );
195 is( bless ({stash=>{}}, 'MyMVCTestAppDefaultModel')->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model ok' );
196 is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model in class method ok' );
198 # regexp behavior tests
200 # is_deeply is used because regexp behavior means list context
201 is_deeply( [ MyMVCTestApp->view( qr{^V[ie]+w$} ) ], [ 'MyMVCTestApp::V::View' ], 'regexp view ok' );
202 is_deeply( [ MyMVCTestApp->controller( qr{Dummy\::Model$} ) ], [ 'MyMVCTestApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
203 is_deeply( [ MyMVCTestApp->model( qr{Dum{2}y} ) ], [ 'MyMVCTestApp::Model::Dummy::Model' ], 'regexp model ok' );
206 is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
210 no warnings 'redefine';
211 local *Catalyst::Log::warn = sub { $warnings++ };
213 # object w/ regexp fallback
214 is( MyMVCTestApp->model( 'Test' ), undef, 'no regexp fallback' );
215 ok( $warnings, 'regexp fallback warnings' );
218 is( MyMVCTestApp->view('MyMVCTestApp::V::View$'), undef, 'no regexp fallback');
220 is( MyMVCTestApp->controller('MyMVCTestApp::C::Controller$'), undef, 'no regexp fallback');
222 is( MyMVCTestApp->model('MyMVCTestApp::M::Model$'), undef, 'no regexp fallback');
226 my @expected = qw( MyMVCTestApp::C::Controller MyMVCTestApp::Controller::C );
227 is_deeply( [ sort MyMVCTestApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' );
231 my @expected = qw( MyMVCTestApp::V::View MyMVCTestApp::View::V );
232 is_deeply( [ sort MyMVCTestApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' );
236 my @expected = qw( MyMVCTestApp::M::Model MyMVCTestApp::Model::M );
237 is_deeply( [ sort MyMVCTestApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' );
242 is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' );
245 #checking @args passed to ACCEPT_CONTEXT
251 *MyMVCTestApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
252 *MyMVCTestApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
255 my $c = bless {}, 'MyMVCTestApp';
257 # test accept-context with class rather than instance
258 MyMVCTestApp->model('M', qw/foo bar/);
259 is_deeply($args, [qw/foo bar/], 'MyMVCTestApp->model args passed to ACCEPT_CONTEXT ok');
262 $c->model('M', qw/foo bar/);
263 is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
265 my $x = $c->view('V', qw/foo2 bar2/);
266 is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
271 package MyApp::WithoutRegexFallback;
273 use base qw/Catalyst/;
275 no warnings 'redefine';
277 __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
279 __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
280 qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
282 # allow $c->log->warn to work
283 __PACKAGE__->setup_log;
287 # test if non-regex component retrieval still works
288 is( MyApp::WithoutRegexFallback->controller('Another::Foo'),
289 'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found');
294 no warnings 'redefine';
295 local *Catalyst::Log::warn = sub { $warnings++ };
297 # try to get nonexisting object w/o regexp fallback
298 is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');