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