2905838f6ae7d50e8cbdd83b103f91cad5423361
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_mvc.t
1 use Test::More;
2 use strict;
3 use warnings;
4
5 {
6     no warnings 'redefine';
7     *Catalyst::Utils::ensure_class_loaded = sub { };
8 }
9
10 use Moose::Meta::Class;
11
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;
15
16 foreach my $comp (@complist) {
17     Moose::Meta::Class->create(
18         $comp =>
19             version => '0.1',
20     );
21 }
22 our $warnings = 0;
23 our $loaded   = 0;
24
25 Moose::Meta::Class->create('Some::Test::Object');
26
27 Moose::Meta::Class->create(
28     'MyMVCTestApp::Model::Test::Object' =>
29         superclasses => [ 'Catalyst::Model', 'Some::Test::Object' ],
30 );
31
32 {
33     package MyMVCTestApp;
34
35     use base qw/Catalyst/;
36
37     no warnings 'redefine';
38
39     local *Catalyst::IOC::Container::build_locate_components_service = sub {
40         my $self = shift;
41
42         return Bread::Board::BlockInjection->new(
43             lifecycle => 'Singleton',
44             name      => 'locate_components',
45             block     => sub {
46                 return [@complist, 'MyMVCTestApp::Model::Test::Object'];
47
48             },
49         );
50     };
51     local *Catalyst::Log::warn = sub { $warnings++ };
52     *Class::MOP::load_class = sub {
53         my $class = shift;
54         $loaded++
55             if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestApp/
56     };
57
58     __PACKAGE__->setup;
59 }
60
61 ok( $warnings, 'Issues deprecated warnings' );
62 is( $loaded, scalar @complist + 1, 'Loaded all components' );
63
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 {
101     my $warnings = 0;
102     no warnings 'redefine';
103     local *Catalyst::Log::warn = sub { $warnings++ };
104
105     is( MyMVCTestApp->view , undef, 'view() w/o a default is undef' );
106     ok( $warnings, 'warnings thrown for view() w/o a default' );
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 {
118     my $warnings = 0;
119     no warnings 'redefine';
120     local *Catalyst::Log::warn = sub { $warnings++ };
121
122     is( MyMVCTestApp->model, undef, 'model() w/o a default is undef' );
123     ok( $warnings, 'warnings thrown for model() w/o a default' );
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
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
143 our @complist_default_view =
144     map { "MyMVCTestAppDefaultView::$_" } @complist_suffix;
145
146 {
147     package MyMVCTestAppDefaultView;
148
149     use base qw/Catalyst/;
150     no warnings 'redefine';
151
152     local *Catalyst::IOC::Container::build_locate_components_service = sub {
153         my $self = shift;
154
155         return Bread::Board::BlockInjection->new(
156             lifecycle => 'Singleton',
157             name      => 'locate_components',
158             block     => sub {
159                 return \@complist_default_view;
160             },
161         );
162     };
163     local *Catalyst::Log::warn = sub { $warnings++ };
164     *Class::MOP::load_class = sub {
165         my $class = shift;
166         $loaded++
167             if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultView/
168     };
169
170     __PACKAGE__->config( default_view => 'V' );
171
172     __PACKAGE__->setup;
173 }
174
175 is( bless ({stash=>{}}, 'MyMVCTestAppDefaultView')->view, 'MyMVCTestAppDefaultView::View::V', 'default_view ok' );
176 is( MyMVCTestAppDefaultView->view , 'MyMVCTestAppDefaultView::View::V', 'default_view in class method ok' );
177
178 our @complist_default_model =
179     map { "MyMVCTestAppDefaultModel::$_" } @complist_suffix;
180
181 {
182     package MyMVCTestAppDefaultModel;
183
184     use base qw/Catalyst/;
185
186     no warnings 'redefine';
187
188     local *Catalyst::IOC::Container::build_locate_components_service = sub {
189         my $self = shift;
190
191         return Bread::Board::BlockInjection->new(
192             lifecycle => 'Singleton',
193             name      => 'locate_components',
194             block     => sub {
195                 return \@complist_default_model;
196             },
197         );
198     };
199     local *Catalyst::Log::warn = sub { $warnings++ };
200     *Class::MOP::load_class = sub {
201         my $class = shift;
202         $loaded++
203             if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultModel/
204     };
205
206     __PACKAGE__->config( default_model => 'M' );
207
208     __PACKAGE__->setup;
209 }
210
211 is( bless ({stash=>{}}, 'MyMVCTestAppDefaultModel')->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model ok' );
212 is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model in class method ok' );
213
214 # regexp behavior tests
215 {
216     # is_deeply is used because regexp behavior means list context
217     is_deeply( [ MyMVCTestApp->view( qr{^V[ie]+w$} ) ], [ 'MyMVCTestApp::V::View' ], 'regexp view ok' );
218     is_deeply( [ MyMVCTestApp->controller( qr{Dummy\::Model$} ) ], [ 'MyMVCTestApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
219     is_deeply( [ MyMVCTestApp->model( qr{Dum{2}y} ) ], [ 'MyMVCTestApp::Model::Dummy::Model' ], 'regexp model ok' );
220
221     # object w/ qr{}
222     is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
223
224     {
225         my $warnings = 0;
226         no warnings 'redefine';
227         local *Catalyst::Log::warn = sub { $warnings++ };
228
229         # object w/ regexp fallback
230         is( MyMVCTestApp->model( 'Test' ), undef, 'no regexp fallback' );
231         ok( $warnings, 'regexp fallback warnings' );
232     }
233
234     is( MyMVCTestApp->view('MyMVCTestApp::V::View$'), undef, 'no regexp fallback');
235
236     is( MyMVCTestApp->controller('MyMVCTestApp::C::Controller$'), undef, 'no regexp fallback');
237
238     is( MyMVCTestApp->model('MyMVCTestApp::M::Model$'), undef, 'no regexp fallback');
239 }
240
241 {
242     my @expected = qw( MyMVCTestApp::C::Controller MyMVCTestApp::Controller::C );
243     is_deeply( [ sort MyMVCTestApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' );
244 }
245
246 {
247     my @expected = qw( MyMVCTestApp::V::View MyMVCTestApp::View::V );
248     is_deeply( [ sort MyMVCTestApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' );
249 }
250
251 {
252     my @expected = qw( MyMVCTestApp::M::Model MyMVCTestApp::Model::M );
253     is_deeply( [ sort MyMVCTestApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' );
254 }
255
256 # failed search
257 {
258     is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' );
259 }
260
261 #checking @args passed to ACCEPT_CONTEXT
262 {
263     my $args;
264
265     {
266         no warnings 'once';
267         *MyMVCTestApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
268         *MyMVCTestApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
269     }
270
271     my $c = bless {}, 'MyMVCTestApp';
272
273     # test accept-context with class rather than instance
274     MyMVCTestApp->model('M', qw/foo bar/);
275     is_deeply($args, [qw/foo bar/], 'MyMVCTestApp->model args passed to ACCEPT_CONTEXT ok');
276
277
278     $c->model('M', qw/foo bar/);
279     is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
280
281     my $x = $c->view('V', qw/foo2 bar2/);
282     is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
283
284 }
285
286 {
287     package MyApp::WithoutRegexFallback;
288
289     use base qw/Catalyst/;
290
291     no warnings 'redefine';
292
293     *Class::MOP::load_class = sub {
294         $loaded++;
295     };
296
297     __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
298
299     __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
300         qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
301
302     # allow $c->log->warn to work
303     __PACKAGE__->setup_log;
304 }
305
306 {
307     # test if non-regex component retrieval still works
308     is( MyApp::WithoutRegexFallback->controller('Another::Foo'),
309         'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found');
310 }
311
312 {
313     my $warnings = 0;
314     no warnings 'redefine';
315     local *Catalyst::Log::warn = sub { $warnings++ };
316
317     # try to get nonexisting object w/o regexp fallback
318     is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');
319 }
320
321 done_testing;