4 use Moose::Meta::Class;
9 map { "MyMVCTestApp::$_"; }
10 qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
12 foreach my $comp (@complist) {
13 Moose::Meta::Class->create(
21 Moose::Meta::Class->create('Some::Test::Object');
23 Moose::Meta::Class->create(
24 'MyMVCTestApp::Model::Test::Object' =>
25 superclasses => [ 'Catalyst::Model', 'Some::Test::Object' ],
31 use base qw/Catalyst/;
33 sub locate_components {
34 return (@::complist, 'MyMVCTestApp::Model::Test::Object');
37 no warnings 'redefine';
38 *Catalyst::Log::warn = sub { $::warnings++ };
39 *Catalyst::Utils::ensure_class_loaded = sub { $::loaded++ if Class::MOP::is_class_loaded(shift) };
44 ok( $warnings, 'Issues deprecated warnings' );
45 is( $loaded, scalar @complist + 1, 'Loaded all components' );
47 is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' );
49 is( MyMVCTestApp->controller('Controller'),
50 'MyMVCTestApp::C::Controller', 'C::Controller ok' );
52 is( MyMVCTestApp->model('Model'), 'MyMVCTestApp::M::Model', 'M::Model ok' );
54 is( MyMVCTestApp->model('Dummy::Model'), 'MyMVCTestApp::Model::Dummy::Model', 'Model::Dummy::Model ok' );
56 isa_ok( MyMVCTestApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' );
58 is( MyMVCTestApp->controller('Model::Dummy::Model'), 'MyMVCTestApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' );
60 is( MyMVCTestApp->view('V'), 'MyMVCTestApp::View::V', 'View::V ok' );
62 is( MyMVCTestApp->controller('C'), 'MyMVCTestApp::Controller::C', 'Controller::C ok' );
64 is( MyMVCTestApp->model('M'), 'MyMVCTestApp::Model::M', 'Model::M ok' );
68 is( MyMVCTestApp->model('DNE'), undef, 'undef for invalid search' );
71 is_deeply( [ sort MyMVCTestApp->views ],
75 is_deeply( [ sort MyMVCTestApp->controllers ],
76 [ qw/C Controller Model::Dummy::Model/ ],
79 is_deeply( [ sort MyMVCTestApp->models ],
80 [ qw/Dummy::Model M Model Test::Object/ ],
86 like (MyMVCTestApp->view , qr/^MyMVCTestApp\::(V|View)\::/ , 'view() with no defaults returns *something*');
87 ok( $warnings, 'view() w/o a default is random, warnings thrown' );
90 is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok');
92 my $view = bless {} , 'MyMVCTestApp::View::V';
93 is ( bless ({stash=>{current_view_instance=> $view }}, 'MyMVCTestApp')->view , $view, 'current_view_instance ok');
95 is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp::V::View' }}, 'MyMVCTestApp')->view , $view,
96 'current_view_instance precedes current_view ok');
101 ok( my $model = MyMVCTestApp->model );
103 ok( (($model =~ /^MyMVCTestApp\::(M|Model)\::/) ||
104 $model->isa('Some::Test::Object')),
105 'model() with no defaults returns *something*' );
107 ok( $warnings, 'model() w/o a default is random, warnings thrown' );
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');
118 MyMVCTestApp->config->{default_view} = 'V';
119 is ( bless ({stash=>{}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'default_view ok');
120 is ( MyMVCTestApp->view , 'MyMVCTestApp::View::V', 'default_view in class method ok');
122 MyMVCTestApp->config->{default_model} = 'M';
123 is ( bless ({stash=>{}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'default_model ok');
124 is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class method ok');
126 # regexp behavior tests
128 # is_deeply is used because regexp behavior means list context
129 is_deeply( [ MyMVCTestApp->view( qr{^V[ie]+w$} ) ], [ 'MyMVCTestApp::V::View' ], 'regexp view ok' );
130 is_deeply( [ MyMVCTestApp->controller( qr{Dummy\::Model$} ) ], [ 'MyMVCTestApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
131 is_deeply( [ MyMVCTestApp->model( qr{Dum{2}y} ) ], [ 'MyMVCTestApp::Model::Dummy::Model' ], 'regexp model ok' );
134 is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
139 # object w/ regexp fallback
140 is_deeply( [ MyMVCTestApp->model( 'Test' ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
141 ok( $warnings, 'regexp fallback warnings' );
144 is_deeply( [ MyMVCTestApp->view('MyMVCTestApp::V::View$') ], [ 'MyMVCTestApp::V::View' ], 'Explicit return ok');
145 is_deeply( [ MyMVCTestApp->controller('MyMVCTestApp::C::Controller$') ], [ 'MyMVCTestApp::C::Controller' ], 'Explicit return ok');
146 is_deeply( [ MyMVCTestApp->model('MyMVCTestApp::M::Model$') ], [ 'MyMVCTestApp::M::Model' ], 'Explicit return ok');
150 my @expected = qw( MyMVCTestApp::C::Controller MyMVCTestApp::Controller::C );
151 is_deeply( [ sort MyMVCTestApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' );
155 my @expected = qw( MyMVCTestApp::V::View MyMVCTestApp::View::V );
156 is_deeply( [ sort MyMVCTestApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' );
160 my @expected = qw( MyMVCTestApp::M::Model MyMVCTestApp::Model::M );
161 is_deeply( [ sort MyMVCTestApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' );
166 is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' );
169 #checking @args passed to ACCEPT_CONTEXT
175 *MyMVCTestApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
176 *MyMVCTestApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
179 my $c = bless {}, 'MyMVCTestApp';
181 # test accept-context with class rather than instance
182 MyMVCTestApp->model('M', qw/foo bar/);
183 is_deeply($args, [qw/foo bar/], 'MyMVCTestApp->model args passed to ACCEPT_CONTEXT ok');
186 $c->model('M', qw/foo bar/);
187 is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
189 my $x = $c->view('V', qw/foo2 bar2/);
190 is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
193 $c->view('::View::V', qw/foo3 bar3/);
194 is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok');
201 no warnings 'redefine';
202 local *Catalyst::Log::warn = sub { $warn .= $_[1] };
204 is_deeply (MyMVCTestApp->controller('MyMVCTestApp::Controller::C'),
205 MyMVCTestApp->components->{'MyMVCTestApp::Controller::C'},
206 'controller by fully qualified name ok');
208 # You probably meant $c->controller('C') instead of $c->controller({'MyMVCTestApp::Controller::C'})
209 my ($suggested_comp_name, $orig_comp_name) = $warn =~ /You probably meant (.*) instead of (.*) /;
210 isnt($suggested_comp_name, $orig_comp_name, 'suggested fix in warning for fully qualified component names makes sense' );
214 package MyApp::WithoutRegexFallback;
216 use base qw/Catalyst/;
218 __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
220 __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
221 qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
223 # allow $c->log->warn to work
224 __PACKAGE__->setup_log;
228 # test if non-regex component retrieval still works
229 is( MyApp::WithoutRegexFallback->controller('Another::Foo'),
230 'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found');
235 no warnings 'redefine';
236 local *Catalyst::Log::warn = sub { $warnings++ };
238 # try to get nonexisting object w/o regexp fallback
239 is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');
240 ok( !$warnings, 'no regexp fallback warnings' );