Commit | Line | Data |
a9df50f8 |
1 | use Test::More; |
5d50f369 |
2 | use strict; |
3 | use warnings; |
e10b40fd |
4 | |
a9df50f8 |
5 | use Moose::Meta::Class; |
5d50f369 |
6 | |
7 | use_ok('Catalyst'); |
8 | |
a9df50f8 |
9 | our @complist = |
5d50f369 |
10 | map { "MyMVCTestApp::$_"; } |
11 | qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/; |
12 | |
a9df50f8 |
13 | foreach my $comp (@complist) { |
14 | Moose::Meta::Class->create( |
15 | $comp => |
16 | version => '0.1', |
17 | ); |
18 | } |
19 | our $warnings = 0; |
20 | our $loaded = 0; |
21 | |
22 | Moose::Meta::Class->create('Some::Test::Object'); |
5d50f369 |
23 | |
a9df50f8 |
24 | Moose::Meta::Class->create( |
25 | 'MyMVCTestApp::Model::Test::Object' => |
26 | superclasses => [ 'Catalyst::Model', 'Some::Test::Object' ], |
27 | ); |
28 | |
29 | { |
5d50f369 |
30 | package MyMVCTestApp; |
31 | |
32 | use base qw/Catalyst/; |
33 | |
a9df50f8 |
34 | sub locate_components { |
35 | return (@::complist, 'MyMVCTestApp::Model::Test::Object'); |
36 | } |
5d50f369 |
37 | |
a9df50f8 |
38 | no warnings 'redefine'; |
39 | *Catalyst::Log::warn = sub { $::warnings++ }; |
e10b40fd |
40 | *Catalyst::Utils::ensure_class_loaded = sub { |
41 | my $class = shift; |
42 | $::loaded++ |
43 | if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestApp/ |
44 | }; |
5d50f369 |
45 | |
a9df50f8 |
46 | __PACKAGE__->setup; |
5d50f369 |
47 | } |
48 | |
a9df50f8 |
49 | ok( $warnings, 'Issues deprecated warnings' ); |
50 | is( $loaded, scalar @complist + 1, 'Loaded all components' ); |
51 | |
5d50f369 |
52 | is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' ); |
53 | |
54 | is( MyMVCTestApp->controller('Controller'), |
55 | 'MyMVCTestApp::C::Controller', 'C::Controller ok' ); |
56 | |
57 | is( MyMVCTestApp->model('Model'), 'MyMVCTestApp::M::Model', 'M::Model ok' ); |
58 | |
59 | is( MyMVCTestApp->model('Dummy::Model'), 'MyMVCTestApp::Model::Dummy::Model', 'Model::Dummy::Model ok' ); |
60 | |
61 | isa_ok( MyMVCTestApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' ); |
62 | |
63 | is( MyMVCTestApp->controller('Model::Dummy::Model'), 'MyMVCTestApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' ); |
64 | |
65 | is( MyMVCTestApp->view('V'), 'MyMVCTestApp::View::V', 'View::V ok' ); |
66 | |
67 | is( MyMVCTestApp->controller('C'), 'MyMVCTestApp::Controller::C', 'Controller::C ok' ); |
68 | |
69 | is( MyMVCTestApp->model('M'), 'MyMVCTestApp::Model::M', 'Model::M ok' ); |
70 | |
71 | # failed search |
72 | { |
73 | is( MyMVCTestApp->model('DNE'), undef, 'undef for invalid search' ); |
74 | } |
75 | |
76 | is_deeply( [ sort MyMVCTestApp->views ], |
77 | [ qw/V View/ ], |
78 | 'views ok' ); |
79 | |
80 | is_deeply( [ sort MyMVCTestApp->controllers ], |
81 | [ qw/C Controller Model::Dummy::Model/ ], |
82 | 'controllers ok'); |
83 | |
84 | is_deeply( [ sort MyMVCTestApp->models ], |
85 | [ qw/Dummy::Model M Model Test::Object/ ], |
86 | 'models ok'); |
87 | |
88 | { |
a9df50f8 |
89 | $warnings = 0; |
5d50f369 |
90 | |
91 | like (MyMVCTestApp->view , qr/^MyMVCTestApp\::(V|View)\::/ , 'view() with no defaults returns *something*'); |
92 | ok( $warnings, 'view() w/o a default is random, warnings thrown' ); |
93 | } |
94 | |
95 | is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok'); |
96 | |
97 | my $view = bless {} , 'MyMVCTestApp::View::V'; |
98 | is ( bless ({stash=>{current_view_instance=> $view }}, 'MyMVCTestApp')->view , $view, 'current_view_instance ok'); |
99 | |
100 | is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp::V::View' }}, 'MyMVCTestApp')->view , $view, |
101 | 'current_view_instance precedes current_view ok'); |
102 | |
103 | { |
a9df50f8 |
104 | $warnings = 0; |
5d50f369 |
105 | |
106 | ok( my $model = MyMVCTestApp->model ); |
107 | |
108 | ok( (($model =~ /^MyMVCTestApp\::(M|Model)\::/) || |
109 | $model->isa('Some::Test::Object')), |
110 | 'model() with no defaults returns *something*' ); |
111 | |
112 | ok( $warnings, 'model() w/o a default is random, warnings thrown' ); |
113 | } |
114 | |
115 | is ( bless ({stash=>{current_model=>'M'}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'current_model ok'); |
116 | |
117 | my $model = bless {} , 'MyMVCTestApp::Model::M'; |
118 | is ( bless ({stash=>{current_model_instance=> $model }}, 'MyMVCTestApp')->model , $model, 'current_model_instance ok'); |
119 | |
120 | is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyMVCTestApp::M::Model' }}, 'MyMVCTestApp')->model , $model, |
121 | 'current_model_instance precedes current_model ok'); |
122 | |
e10b40fd |
123 | { |
124 | use FindBin '$Bin'; |
125 | use lib "$Bin/../lib"; |
126 | |
127 | use Catalyst::Test 'TestAppController'; |
128 | |
129 | is( get('/foo/test_controller'), 'bar', 'controller() with empty args returns current controller' ); |
130 | } |
131 | |
5d50f369 |
132 | MyMVCTestApp->config->{default_view} = 'V'; |
133 | is ( bless ({stash=>{}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'default_view ok'); |
134 | is ( MyMVCTestApp->view , 'MyMVCTestApp::View::V', 'default_view in class method ok'); |
135 | |
136 | MyMVCTestApp->config->{default_model} = 'M'; |
137 | is ( bless ({stash=>{}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'default_model ok'); |
138 | is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class method ok'); |
139 | |
140 | # regexp behavior tests |
141 | { |
142 | # is_deeply is used because regexp behavior means list context |
143 | is_deeply( [ MyMVCTestApp->view( qr{^V[ie]+w$} ) ], [ 'MyMVCTestApp::V::View' ], 'regexp view ok' ); |
144 | is_deeply( [ MyMVCTestApp->controller( qr{Dummy\::Model$} ) ], [ 'MyMVCTestApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' ); |
145 | is_deeply( [ MyMVCTestApp->model( qr{Dum{2}y} ) ], [ 'MyMVCTestApp::Model::Dummy::Model' ], 'regexp model ok' ); |
146 | |
147 | # object w/ qr{} |
148 | is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' ); |
149 | |
150 | { |
a9df50f8 |
151 | $warnings = 0; |
5d50f369 |
152 | |
153 | # object w/ regexp fallback |
5a53ef3d |
154 | is( MyMVCTestApp->model( 'Test' ), undef, 'no regexp fallback' ); |
5d50f369 |
155 | ok( $warnings, 'regexp fallback warnings' ); |
156 | } |
157 | |
5a53ef3d |
158 | is( MyMVCTestApp->view('MyMVCTestApp::V::View$'), undef, 'no regexp fallback'); |
159 | |
160 | is( MyMVCTestApp->controller('MyMVCTestApp::C::Controller$'), undef, 'no regexp fallback'); |
161 | |
162 | is( MyMVCTestApp->model('MyMVCTestApp::M::Model$'), undef, 'no regexp fallback'); |
5d50f369 |
163 | } |
164 | |
165 | { |
166 | my @expected = qw( MyMVCTestApp::C::Controller MyMVCTestApp::Controller::C ); |
167 | is_deeply( [ sort MyMVCTestApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' ); |
168 | } |
169 | |
170 | { |
171 | my @expected = qw( MyMVCTestApp::V::View MyMVCTestApp::View::V ); |
172 | is_deeply( [ sort MyMVCTestApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' ); |
173 | } |
174 | |
175 | { |
176 | my @expected = qw( MyMVCTestApp::M::Model MyMVCTestApp::Model::M ); |
177 | is_deeply( [ sort MyMVCTestApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' ); |
178 | } |
179 | |
180 | # failed search |
181 | { |
182 | is( scalar MyMVCTestApp->controller( qr{DNE} ), 0, '0 results for failed search' ); |
183 | } |
184 | |
185 | #checking @args passed to ACCEPT_CONTEXT |
186 | { |
187 | my $args; |
188 | |
189 | { |
190 | no warnings 'once'; |
191 | *MyMVCTestApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args}; |
192 | *MyMVCTestApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args}; |
193 | } |
194 | |
195 | my $c = bless {}, 'MyMVCTestApp'; |
196 | |
197 | # test accept-context with class rather than instance |
198 | MyMVCTestApp->model('M', qw/foo bar/); |
199 | is_deeply($args, [qw/foo bar/], 'MyMVCTestApp->model args passed to ACCEPT_CONTEXT ok'); |
200 | |
201 | |
202 | $c->model('M', qw/foo bar/); |
203 | is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok'); |
204 | |
205 | my $x = $c->view('V', qw/foo2 bar2/); |
206 | is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok'); |
207 | |
02fee6c6 |
208 | } |
209 | |
210 | { |
211 | package MyApp::WithoutRegexFallback; |
212 | |
213 | use base qw/Catalyst/; |
214 | |
215 | __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } ); |
216 | |
217 | __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) } |
218 | qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } ); |
219 | |
220 | # allow $c->log->warn to work |
221 | __PACKAGE__->setup_log; |
222 | } |
223 | |
224 | { |
225 | # test if non-regex component retrieval still works |
226 | is( MyApp::WithoutRegexFallback->controller('Another::Foo'), |
227 | 'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found'); |
228 | } |
229 | |
230 | { |
231 | my $warnings = 0; |
232 | no warnings 'redefine'; |
233 | local *Catalyst::Log::warn = sub { $warnings++ }; |
234 | |
235 | # try to get nonexisting object w/o regexp fallback |
236 | is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found'); |
02fee6c6 |
237 | } |
a9df50f8 |
238 | |
5a53ef3d |
239 | done_testing; |