1 use Test::More tests => 48;
8 map { "MyMVCTestApp::$_"; }
9 qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
15 use base qw/Catalyst/;
17 __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) } @complist } );
20 bless $thingie, 'Some::Test::Object';
21 __PACKAGE__->components->{'MyMVCTestApp::Model::Test::Object'} = $thingie;
23 # allow $c->log->warn to work
24 __PACKAGE__->setup_log;
27 is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' );
29 is( MyMVCTestApp->controller('Controller'),
30 'MyMVCTestApp::C::Controller', 'C::Controller ok' );
32 is( MyMVCTestApp->model('Model'), 'MyMVCTestApp::M::Model', 'M::Model ok' );
34 is( MyMVCTestApp->model('Dummy::Model'), 'MyMVCTestApp::Model::Dummy::Model', 'Model::Dummy::Model ok' );
36 isa_ok( MyMVCTestApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' );
38 is( MyMVCTestApp->controller('Model::Dummy::Model'), 'MyMVCTestApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' );
40 is( MyMVCTestApp->view('V'), 'MyMVCTestApp::View::V', 'View::V ok' );
42 is( MyMVCTestApp->controller('C'), 'MyMVCTestApp::Controller::C', 'Controller::C ok' );
44 is( MyMVCTestApp->model('M'), 'MyMVCTestApp::Model::M', 'Model::M ok' );
48 is( MyMVCTestApp->model('DNE'), undef, 'undef for invalid search' );
51 is_deeply( [ sort MyMVCTestApp->views ],
55 is_deeply( [ sort MyMVCTestApp->controllers ],
56 [ qw/C Controller Model::Dummy::Model/ ],
59 is_deeply( [ sort MyMVCTestApp->models ],
60 [ qw/Dummy::Model M Model Test::Object/ ],
64 eval { MyMVCTestApp->view; };
65 ok( $@, 'view() with no defaults and multiple choices dies' );
68 is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok');
70 my $view = bless {} , 'MyMVCTestApp::View::V';
71 is ( bless ({stash=>{current_view_instance=> $view }}, 'MyMVCTestApp')->view , $view, 'current_view_instance ok');
73 is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp::V::View' }}, 'MyMVCTestApp')->view , $view,
74 'current_view_instance precedes current_view ok');
77 eval { MyMVCTestApp->model; };
78 ok( $@, 'model() with no defaults and multiple choices dies' );
81 is ( bless ({stash=>{current_model=>'M'}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'current_model ok');
83 my $model = bless {} , 'MyMVCTestApp::Model::M';
84 is ( bless ({stash=>{current_model_instance=> $model }}, 'MyMVCTestApp')->model , $model, 'current_model_instance ok');
86 is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyMVCTestApp::M::Model' }}, 'MyMVCTestApp')->model , $model,
87 'current_model_instance precedes current_model ok');
89 MyMVCTestApp->config->{default_view} = 'V';
90 is ( bless ({stash=>{}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'default_view ok');
91 is ( MyMVCTestApp->view , 'MyMVCTestApp::View::V', 'default_view in class method ok');
93 MyMVCTestApp->config->{default_model} = 'M';
94 is ( bless ({stash=>{}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'default_model ok');
95 is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class method ok');
97 # regexp behavior tests
99 # is_deeply is used because regexp behavior means list context
100 is_deeply( [ MyMVCTestApp->view( qr{^V[ie]+w$} ) ], [ 'MyMVCTestApp::V::View' ], 'regexp view ok' );
101 is_deeply( [ MyMVCTestApp->controller( qr{Dummy\::Model$} ) ], [ 'MyMVCTestApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
102 is_deeply( [ MyMVCTestApp->model( qr{Dum{2}y} ) ], [ 'MyMVCTestApp::Model::Dummy::Model' ], 'regexp model ok' );
105 is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
109 no warnings 'redefine';
110 local *Catalyst::Log::warn = sub { $warnings++ };
112 # object w/ regexp fallback
113 is_deeply( [ MyMVCTestApp->model( 'Test' ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
114 ok( $warnings, 'regexp fallback warnings' );
117 is_deeply( [ MyMVCTestApp->view('MyMVCTestApp::V::View$') ], [ 'MyMVCTestApp::V::View' ], 'Explicit return ok');
118 is_deeply( [ MyMVCTestApp->controller('MyMVCTestApp::C::Controller$') ], [ 'MyMVCTestApp::C::Controller' ], 'Explicit return ok');
119 is_deeply( [ MyMVCTestApp->model('MyMVCTestApp::M::Model$') ], [ 'MyMVCTestApp::M::Model' ], 'Explicit return ok');
123 my @expected = qw( MyMVCTestApp::C::Controller MyMVCTestApp::Controller::C );
124 is_deeply( [ sort MyMVCTestApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' );
128 my @expected = qw( MyMVCTestApp::V::View MyMVCTestApp::View::V );
129 is_deeply( [ sort MyMVCTestApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' );
133 my @expected = qw( MyMVCTestApp::M::Model MyMVCTestApp::Model::M );
134 is_deeply( [ sort MyMVCTestApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' );
139 is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' );
142 #checking @args passed to ACCEPT_CONTEXT
148 *MyMVCTestApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
149 *MyMVCTestApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
152 my $c = bless {}, 'MyMVCTestApp';
154 # test accept-context with class rather than instance
155 MyMVCTestApp->model('M', qw/foo bar/);
156 is_deeply($args, [qw/foo bar/], 'MyMVCTestApp->model args passed to ACCEPT_CONTEXT ok');
159 $c->model('M', qw/foo bar/);
160 is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
162 my $x = $c->view('V', qw/foo2 bar2/);
163 is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
166 $c->view('::View::V', qw/foo3 bar3/);
167 is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok');
174 no warnings 'redefine';
175 local *Catalyst::Log::warn = sub { $warn .= $_[1] };
177 is_deeply (MyMVCTestApp->controller('MyMVCTestApp::Controller::C'),
178 MyMVCTestApp->components->{'MyMVCTestApp::Controller::C'},
179 'controller by fully qualified name ok');
181 # You probably meant $c->controller('C') instead of $c->controller({'MyMVCTestApp::Controller::C'})
182 my ($suggested_comp_name, $orig_comp_name) = $warn =~ /You probably meant (.*) instead of (.*) /;
183 isnt($suggested_comp_name, $orig_comp_name, 'suggested fix in warning for fully qualified component names makes sense' );
187 package MyApp::WithoutRegexFallback;
189 use base qw/Catalyst/;
191 __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
193 __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
194 qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
196 # allow $c->log->warn to work
197 __PACKAGE__->setup_log;
201 # test if non-regex component retrieval still works
202 is( MyApp::WithoutRegexFallback->controller('Another::Foo'),
203 'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found');
208 no warnings 'redefine';
209 local *Catalyst::Log::warn = sub { $warnings++ };
211 # try to get nonexisting object w/o regexp fallback
212 is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');
213 ok( !$warnings, 'no regexp fallback warnings' );