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