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