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