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(
25 Moose::Meta::Class->create('Some::Test::Object');
27 Moose::Meta::Class->create(
28 'MyMVCTestApp::Model::Test::Object' =>
29 superclasses => [ 'Catalyst::Model', 'Some::Test::Object' ],
35 use base qw/Catalyst/;
37 no warnings 'redefine';
39 local *Catalyst::IOC::Container::build_locate_components_service = sub {
42 return Bread::Board::BlockInjection->new(
43 lifecycle => 'Singleton',
44 name => 'locate_components',
46 return [@complist, 'MyMVCTestApp::Model::Test::Object'];
51 local *Catalyst::Log::warn = sub { $warnings++ };
52 *Class::MOP::load_class = sub {
55 if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestApp/
61 ok( $warnings, 'Issues deprecated warnings' );
62 is( $loaded, scalar @complist + 1, 'Loaded all components' );
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++ };
164 *Class::MOP::load_class = sub {
167 if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultView/
170 __PACKAGE__->config( default_view => 'V' );
175 is( bless ({stash=>{}}, 'MyMVCTestAppDefaultView')->view, 'MyMVCTestAppDefaultView::View::V', 'default_view ok' );
176 is( MyMVCTestAppDefaultView->view , 'MyMVCTestAppDefaultView::View::V', 'default_view in class method ok' );
178 our @complist_default_model =
179 map { "MyMVCTestAppDefaultModel::$_" } @complist_suffix;
182 package MyMVCTestAppDefaultModel;
184 use base qw/Catalyst/;
186 no warnings 'redefine';
188 local *Catalyst::IOC::Container::build_locate_components_service = sub {
191 return Bread::Board::BlockInjection->new(
192 lifecycle => 'Singleton',
193 name => 'locate_components',
195 return \@complist_default_model;
199 local *Catalyst::Log::warn = sub { $warnings++ };
200 *Class::MOP::load_class = sub {
203 if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultModel/
206 __PACKAGE__->config( default_model => 'M' );
211 is( bless ({stash=>{}}, 'MyMVCTestAppDefaultModel')->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model ok' );
212 is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model in class method ok' );
214 # regexp behavior tests
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' );
222 is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
226 no warnings 'redefine';
227 local *Catalyst::Log::warn = sub { $warnings++ };
229 # object w/ regexp fallback
230 is( MyMVCTestApp->model( 'Test' ), undef, 'no regexp fallback' );
231 ok( $warnings, 'regexp fallback warnings' );
234 is( MyMVCTestApp->view('MyMVCTestApp::V::View$'), undef, 'no regexp fallback');
236 is( MyMVCTestApp->controller('MyMVCTestApp::C::Controller$'), undef, 'no regexp fallback');
238 is( MyMVCTestApp->model('MyMVCTestApp::M::Model$'), undef, 'no regexp fallback');
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' );
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' );
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' );
258 is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' );
261 #checking @args passed to ACCEPT_CONTEXT
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};
271 my $c = bless {}, 'MyMVCTestApp';
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');
278 $c->model('M', qw/foo bar/);
279 is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
281 my $x = $c->view('V', qw/foo2 bar2/);
282 is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
287 package MyApp::WithoutRegexFallback;
289 use base qw/Catalyst/;
291 no warnings 'redefine';
293 *Class::MOP::load_class = sub {
297 __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
299 __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
300 qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
302 # allow $c->log->warn to work
303 __PACKAGE__->setup_log;
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');
314 no warnings 'redefine';
315 local *Catalyst::Log::warn = sub { $warnings++ };
317 # try to get nonexisting object w/o regexp fallback
318 is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');