wrong expected result
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_mvc.t
CommitLineData
a9df50f8 1use Test::More;
5d50f369 2use strict;
3use warnings;
e10b40fd 4
6f9dfc99 5{
6 no warnings 'redefine';
7 *Catalyst::Utils::ensure_class_loaded = sub { };
8}
9d718832 9
a9df50f8 10use Moose::Meta::Class;
5d50f369 11
a2c0d071 12our @complist_suffix = qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
13
14our @complist = map { "MyMVCTestApp::$_" } @complist_suffix;
5d50f369 15
a9df50f8 16foreach my $comp (@complist) {
17 Moose::Meta::Class->create(
18 $comp =>
19 version => '0.1',
20 );
21}
22our $warnings = 0;
a9df50f8 23
24Moose::Meta::Class->create('Some::Test::Object');
5d50f369 25
a9df50f8 26Moose::Meta::Class->create(
27 'MyMVCTestApp::Model::Test::Object' =>
28 superclasses => [ 'Catalyst::Model', 'Some::Test::Object' ],
29);
30
31{
5d50f369 32 package MyMVCTestApp;
33
34 use base qw/Catalyst/;
35
a9df50f8 36 no warnings 'redefine';
9d718832 37
38 local *Catalyst::IOC::Container::build_locate_components_service = sub {
39 my $self = shift;
40
41 return Bread::Board::BlockInjection->new(
42 lifecycle => 'Singleton',
43 name => 'locate_components',
44 block => sub {
45 return [@complist, 'MyMVCTestApp::Model::Test::Object'];
46
47 },
48 );
49 };
ccc6273e 50 local *Catalyst::Log::warn = sub { $warnings++ };
5d50f369 51
a9df50f8 52 __PACKAGE__->setup;
5d50f369 53}
54
a9df50f8 55ok( $warnings, 'Issues deprecated warnings' );
06316b0d 56is( @{[ MyMVCTestApp->component_list ]}, scalar @complist + 1, 'Loaded all components' );
a9df50f8 57
5d50f369 58is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' );
59
60is( MyMVCTestApp->controller('Controller'),
61 'MyMVCTestApp::C::Controller', 'C::Controller ok' );
62
63is( MyMVCTestApp->model('Model'), 'MyMVCTestApp::M::Model', 'M::Model ok' );
64
65is( MyMVCTestApp->model('Dummy::Model'), 'MyMVCTestApp::Model::Dummy::Model', 'Model::Dummy::Model ok' );
66
67isa_ok( MyMVCTestApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' );
68
69is( MyMVCTestApp->controller('Model::Dummy::Model'), 'MyMVCTestApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' );
70
71is( MyMVCTestApp->view('V'), 'MyMVCTestApp::View::V', 'View::V ok' );
72
73is( MyMVCTestApp->controller('C'), 'MyMVCTestApp::Controller::C', 'Controller::C ok' );
74
75is( MyMVCTestApp->model('M'), 'MyMVCTestApp::Model::M', 'Model::M ok' );
76
77# failed search
78{
79 is( MyMVCTestApp->model('DNE'), undef, 'undef for invalid search' );
80}
81
82is_deeply( [ sort MyMVCTestApp->views ],
83 [ qw/V View/ ],
84 'views ok' );
85
86is_deeply( [ sort MyMVCTestApp->controllers ],
87 [ qw/C Controller Model::Dummy::Model/ ],
88 'controllers ok');
89
90is_deeply( [ sort MyMVCTestApp->models ],
91 [ qw/Dummy::Model M Model Test::Object/ ],
92 'models ok');
93
94{
ccc6273e 95 my $warnings = 0;
96 no warnings 'redefine';
97 local *Catalyst::Log::warn = sub { $warnings++ };
5d50f369 98
a2c0d071 99 is( MyMVCTestApp->view , undef, 'view() w/o a default is undef' );
100 ok( $warnings, 'warnings thrown for view() w/o a default' );
5d50f369 101}
102
103is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok');
104
105my $view = bless {} , 'MyMVCTestApp::View::V';
106is ( bless ({stash=>{current_view_instance=> $view }}, 'MyMVCTestApp')->view , $view, 'current_view_instance ok');
107
108is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp::V::View' }}, 'MyMVCTestApp')->view , $view,
109 'current_view_instance precedes current_view ok');
110
111{
ccc6273e 112 my $warnings = 0;
113 no warnings 'redefine';
114 local *Catalyst::Log::warn = sub { $warnings++ };
5d50f369 115
a2c0d071 116 is( MyMVCTestApp->model, undef, 'model() w/o a default is undef' );
117 ok( $warnings, 'warnings thrown for model() w/o a default' );
5d50f369 118}
119
120is ( bless ({stash=>{current_model=>'M'}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'current_model ok');
121
122my $model = bless {} , 'MyMVCTestApp::Model::M';
123is ( bless ({stash=>{current_model_instance=> $model }}, 'MyMVCTestApp')->model , $model, 'current_model_instance ok');
124
125is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyMVCTestApp::M::Model' }}, 'MyMVCTestApp')->model , $model,
126 'current_model_instance precedes current_model ok');
127
e10b40fd 128{
129 use FindBin '$Bin';
130 use lib "$Bin/../lib";
131
132 use Catalyst::Test 'TestAppController';
133
134 is( get('/foo/test_controller'), 'bar', 'controller() with empty args returns current controller' );
135}
136
a2c0d071 137our @complist_default_view =
138 map { "MyMVCTestAppDefaultView::$_" } @complist_suffix;
139
140{
141 package MyMVCTestAppDefaultView;
142
143 use base qw/Catalyst/;
9d718832 144 no warnings 'redefine';
a2c0d071 145
9d718832 146 local *Catalyst::IOC::Container::build_locate_components_service = sub {
147 my $self = shift;
a2c0d071 148
9d718832 149 return Bread::Board::BlockInjection->new(
150 lifecycle => 'Singleton',
151 name => 'locate_components',
152 block => sub {
153 return \@complist_default_view;
154 },
155 );
156 };
ccc6273e 157 local *Catalyst::Log::warn = sub { $warnings++ };
a2c0d071 158
159 __PACKAGE__->config( default_view => 'V' );
160
161 __PACKAGE__->setup;
162}
163
164is( bless ({stash=>{}}, 'MyMVCTestAppDefaultView')->view, 'MyMVCTestAppDefaultView::View::V', 'default_view ok' );
165is( MyMVCTestAppDefaultView->view , 'MyMVCTestAppDefaultView::View::V', 'default_view in class method ok' );
166
167our @complist_default_model =
168 map { "MyMVCTestAppDefaultModel::$_" } @complist_suffix;
169
170{
171 package MyMVCTestAppDefaultModel;
172
173 use base qw/Catalyst/;
174
a2c0d071 175 no warnings 'redefine';
9d718832 176
177 local *Catalyst::IOC::Container::build_locate_components_service = sub {
178 my $self = shift;
179
180 return Bread::Board::BlockInjection->new(
181 lifecycle => 'Singleton',
182 name => 'locate_components',
183 block => sub {
184 return \@complist_default_model;
185 },
186 );
187 };
ccc6273e 188 local *Catalyst::Log::warn = sub { $warnings++ };
a2c0d071 189
190 __PACKAGE__->config( default_model => 'M' );
191
192 __PACKAGE__->setup;
193}
5d50f369 194
a2c0d071 195is( bless ({stash=>{}}, 'MyMVCTestAppDefaultModel')->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model ok' );
196is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model in class method ok' );
5d50f369 197
198# regexp behavior tests
199{
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' );
204
205 # object w/ qr{}
206 is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
207
208 {
ccc6273e 209 my $warnings = 0;
210 no warnings 'redefine';
211 local *Catalyst::Log::warn = sub { $warnings++ };
5d50f369 212
213 # object w/ regexp fallback
5a53ef3d 214 is( MyMVCTestApp->model( 'Test' ), undef, 'no regexp fallback' );
5d50f369 215 ok( $warnings, 'regexp fallback warnings' );
216 }
217
5a53ef3d 218 is( MyMVCTestApp->view('MyMVCTestApp::V::View$'), undef, 'no regexp fallback');
219
220 is( MyMVCTestApp->controller('MyMVCTestApp::C::Controller$'), undef, 'no regexp fallback');
221
222 is( MyMVCTestApp->model('MyMVCTestApp::M::Model$'), undef, 'no regexp fallback');
5d50f369 223}
224
225{
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' );
228}
229
230{
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' );
233}
234
235{
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' );
238}
239
240# failed search
241{
242 is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' );
243}
244
245#checking @args passed to ACCEPT_CONTEXT
246{
247 my $args;
248
249 {
250 no warnings 'once';
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};
253 }
254
255 my $c = bless {}, 'MyMVCTestApp';
256
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');
260
261
262 $c->model('M', qw/foo bar/);
263 is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
264
265 my $x = $c->view('V', qw/foo2 bar2/);
266 is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
267
02fee6c6 268}
269
270{
271 package MyApp::WithoutRegexFallback;
272
273 use base qw/Catalyst/;
274
9d718832 275 no warnings 'redefine';
276
02fee6c6 277 __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
278
279 __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
280 qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
281
282 # allow $c->log->warn to work
283 __PACKAGE__->setup_log;
284}
285
286{
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');
290}
291
292{
293 my $warnings = 0;
294 no warnings 'redefine';
295 local *Catalyst::Log::warn = sub { $warnings++ };
296
297 # try to get nonexisting object w/o regexp fallback
298 is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');
02fee6c6 299}
a9df50f8 300
5a53ef3d 301done_testing;