5 use Moose::Meta::Class;
9 our @complist_suffix = qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
11 our @complist = map { "MyMVCTestApp::$_" } @complist_suffix;
13 foreach my $comp (@complist) {
14 Moose::Meta::Class->create(
22 Moose::Meta::Class->create('Some::Test::Object');
24 Moose::Meta::Class->create(
25 'MyMVCTestApp::Model::Test::Object' =>
26 superclasses => [ 'Catalyst::Model', 'Some::Test::Object' ],
32 use base qw/Catalyst/;
34 sub locate_components {
35 return (@::complist, 'MyMVCTestApp::Model::Test::Object');
38 no warnings 'redefine';
39 *Catalyst::Log::warn = sub { $::warnings++ };
40 *Catalyst::Utils::ensure_class_loaded = sub {
43 if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestApp/
49 ok( $warnings, 'Issues deprecated warnings' );
50 is( $loaded, scalar @complist + 1, 'Loaded all components' );
52 is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' );
54 is( MyMVCTestApp->controller('Controller'),
55 'MyMVCTestApp::C::Controller', 'C::Controller ok' );
57 is( MyMVCTestApp->model('Model'), 'MyMVCTestApp::M::Model', 'M::Model ok' );
59 is( MyMVCTestApp->model('Dummy::Model'), 'MyMVCTestApp::Model::Dummy::Model', 'Model::Dummy::Model ok' );
61 isa_ok( MyMVCTestApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' );
63 is( MyMVCTestApp->controller('Model::Dummy::Model'), 'MyMVCTestApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' );
65 is( MyMVCTestApp->view('V'), 'MyMVCTestApp::View::V', 'View::V ok' );
67 is( MyMVCTestApp->controller('C'), 'MyMVCTestApp::Controller::C', 'Controller::C ok' );
69 is( MyMVCTestApp->model('M'), 'MyMVCTestApp::Model::M', 'Model::M ok' );
73 is( MyMVCTestApp->model('DNE'), undef, 'undef for invalid search' );
76 is_deeply( [ sort MyMVCTestApp->views ],
80 is_deeply( [ sort MyMVCTestApp->controllers ],
81 [ qw/C Controller Model::Dummy::Model/ ],
84 is_deeply( [ sort MyMVCTestApp->models ],
85 [ qw/Dummy::Model M Model Test::Object/ ],
91 is( MyMVCTestApp->view , undef, 'view() w/o a default is undef' );
92 ok( $warnings, 'warnings thrown for view() w/o a default' );
95 is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok');
97 my $view = bless {} , 'MyMVCTestApp::View::V';
98 is ( bless ({stash=>{current_view_instance=> $view }}, 'MyMVCTestApp')->view , $view, 'current_view_instance ok');
100 is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp::V::View' }}, 'MyMVCTestApp')->view , $view,
101 'current_view_instance precedes current_view ok');
106 is( MyMVCTestApp->model, undef, 'model() w/o a default is undef' );
107 ok( $warnings, 'warnings thrown for model() w/o a default' );
110 is ( bless ({stash=>{current_model=>'M'}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'current_model ok');
112 my $model = bless {} , 'MyMVCTestApp::Model::M';
113 is ( bless ({stash=>{current_model_instance=> $model }}, 'MyMVCTestApp')->model , $model, 'current_model_instance ok');
115 is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyMVCTestApp::M::Model' }}, 'MyMVCTestApp')->model , $model,
116 'current_model_instance precedes current_model ok');
120 use lib "$Bin/../lib";
122 use Catalyst::Test 'TestAppController';
124 is( get('/foo/test_controller'), 'bar', 'controller() with empty args returns current controller' );
127 our @complist_default_view =
128 map { "MyMVCTestAppDefaultView::$_" } @complist_suffix;
131 package MyMVCTestAppDefaultView;
133 use base qw/Catalyst/;
135 sub locate_components {
136 return @::complist_default_view;
139 no warnings 'redefine';
140 *Catalyst::Utils::ensure_class_loaded = sub {
143 if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultView/
146 __PACKAGE__->config( default_view => 'V' );
151 is( bless ({stash=>{}}, 'MyMVCTestAppDefaultView')->view, 'MyMVCTestAppDefaultView::View::V', 'default_view ok' );
152 is( MyMVCTestAppDefaultView->view , 'MyMVCTestAppDefaultView::View::V', 'default_view in class method ok' );
154 our @complist_default_model =
155 map { "MyMVCTestAppDefaultModel::$_" } @complist_suffix;
158 package MyMVCTestAppDefaultModel;
160 use base qw/Catalyst/;
162 sub locate_components {
163 return @::complist_default_model;
166 no warnings 'redefine';
167 *Catalyst::Utils::ensure_class_loaded = sub {
170 if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultModel/
173 __PACKAGE__->config( default_model => 'M' );
178 is( bless ({stash=>{}}, 'MyMVCTestAppDefaultModel')->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model ok' );
179 is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model in class method ok' );
181 # regexp behavior tests
183 # is_deeply is used because regexp behavior means list context
184 is_deeply( [ MyMVCTestApp->view( qr{^V[ie]+w$} ) ], [ 'MyMVCTestApp::V::View' ], 'regexp view ok' );
185 is_deeply( [ MyMVCTestApp->controller( qr{Dummy\::Model$} ) ], [ 'MyMVCTestApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
186 is_deeply( [ MyMVCTestApp->model( qr{Dum{2}y} ) ], [ 'MyMVCTestApp::Model::Dummy::Model' ], 'regexp model ok' );
189 is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
194 # object w/ regexp fallback
195 is( MyMVCTestApp->model( 'Test' ), undef, 'no regexp fallback' );
196 ok( $warnings, 'regexp fallback warnings' );
199 is( MyMVCTestApp->view('MyMVCTestApp::V::View$'), undef, 'no regexp fallback');
201 is( MyMVCTestApp->controller('MyMVCTestApp::C::Controller$'), undef, 'no regexp fallback');
203 is( MyMVCTestApp->model('MyMVCTestApp::M::Model$'), undef, 'no regexp fallback');
207 my @expected = qw( MyMVCTestApp::C::Controller MyMVCTestApp::Controller::C );
208 is_deeply( [ sort MyMVCTestApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' );
212 my @expected = qw( MyMVCTestApp::V::View MyMVCTestApp::View::V );
213 is_deeply( [ sort MyMVCTestApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' );
217 my @expected = qw( MyMVCTestApp::M::Model MyMVCTestApp::Model::M );
218 is_deeply( [ sort MyMVCTestApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' );
223 is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' );
226 #checking @args passed to ACCEPT_CONTEXT
232 *MyMVCTestApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
233 *MyMVCTestApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
236 my $c = bless {}, 'MyMVCTestApp';
238 # test accept-context with class rather than instance
239 MyMVCTestApp->model('M', qw/foo bar/);
240 is_deeply($args, [qw/foo bar/], 'MyMVCTestApp->model args passed to ACCEPT_CONTEXT ok');
243 $c->model('M', qw/foo bar/);
244 is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
246 my $x = $c->view('V', qw/foo2 bar2/);
247 is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
252 package MyApp::WithoutRegexFallback;
254 use base qw/Catalyst/;
256 __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
258 __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
259 qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
261 # allow $c->log->warn to work
262 __PACKAGE__->setup_log;
266 # test if non-regex component retrieval still works
267 is( MyApp::WithoutRegexFallback->controller('Another::Foo'),
268 'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found');
273 no warnings 'redefine';
274 local *Catalyst::Log::warn = sub { $warnings++ };
276 # try to get nonexisting object w/o regexp fallback
277 is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');