Remove MockObject from Makefile.PL
[catagits/Catalyst-Runtime.git] / t / unit_core_mvc.t
CommitLineData
c23b894b 1use Test::More tests => 46;
af3ff00e 2use strict;
3use warnings;
4
5use_ok('Catalyst');
6
7my @complist =
8 map { "MyApp::$_"; }
8dc75d26 9 qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
af3ff00e 10
11{
12
13 package MyApp;
14
15 use base qw/Catalyst/;
16
3b88a455 17 __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) } @complist } );
2f381252 18
c23b894b 19 my $thingie={};
20 bless $thingie, 'Some::Test::Object';
21 __PACKAGE__->components->{'MyApp::Model::Test::Object'} = $thingie;
22
2f381252 23 # allow $c->log->warn to work
24 __PACKAGE__->setup_log;
af3ff00e 25}
26
27is( MyApp->view('View'), 'MyApp::V::View', 'V::View ok' );
28
29is( MyApp->controller('Controller'),
30 'MyApp::C::Controller', 'C::Controller ok' );
31
32is( MyApp->model('Model'), 'MyApp::M::Model', 'M::Model ok' );
33
8dc75d26 34is( MyApp->model('Dummy::Model'), 'MyApp::Model::Dummy::Model', 'Model::Dummy::Model ok' );
35
c23b894b 36isa_ok( MyApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' );
8dc75d26 37
38is( MyApp->controller('Model::Dummy::Model'), 'MyApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' );
39
af3ff00e 40is( MyApp->view('V'), 'MyApp::View::V', 'View::V ok' );
41
42is( MyApp->controller('C'), 'MyApp::Controller::C', 'Controller::C ok' );
43
44is( MyApp->model('M'), 'MyApp::Model::M', 'Model::M ok' );
3b88a455 45
2f381252 46# failed search
47{
48 is( MyApp->model('DNE'), undef, 'undef for invalid search' );
49}
50
3b88a455 51is_deeply( [ sort MyApp->views ],
52 [ qw/V View/ ],
53 'views ok' );
54
55is_deeply( [ sort MyApp->controllers ],
56 [ qw/C Controller Model::Dummy::Model/ ],
57 'controllers ok');
58
59is_deeply( [ sort MyApp->models ],
60 [ qw/Dummy::Model M Model Test::Object/ ],
61 'models ok');
a3b71f0f 62
2f381252 63{
64 my $warnings = 0;
65 no warnings 'redefine';
66 local *Catalyst::Log::warn = sub { $warnings++ };
67
68 like (MyApp->view , qr/^MyApp\::(V|View)\::/ , 'view() with no defaults returns *something*');
69 ok( $warnings, 'view() w/o a default is random, warnings thrown' );
70}
a3b71f0f 71
72is ( bless ({stash=>{current_view=>'V'}}, 'MyApp')->view , 'MyApp::View::V', 'current_view ok');
73
74my $view = bless {} , 'MyApp::View::V';
75is ( bless ({stash=>{current_view_instance=> $view }}, 'MyApp')->view , $view, 'current_view_instance ok');
76
77is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyApp::V::View' }}, 'MyApp')->view , $view,
78 'current_view_instance precedes current_view ok');
79
2f381252 80{
81 my $warnings = 0;
82 no warnings 'redefine';
83 local *Catalyst::Log::warn = sub { $warnings++ };
84
c23b894b 85 ok( my $model = MyApp->model );
86
87 ok( (($model =~ /^MyApp\::(M|Model)\::/) ||
88 $model->isa('Some::Test::Object')),
89 'model() with no defaults returns *something*' );
90
2f381252 91 ok( $warnings, 'model() w/o a default is random, warnings thrown' );
92}
a3b71f0f 93
94is ( bless ({stash=>{current_model=>'M'}}, 'MyApp')->model , 'MyApp::Model::M', 'current_model ok');
95
96my $model = bless {} , 'MyApp::Model::M';
97is ( bless ({stash=>{current_model_instance=> $model }}, 'MyApp')->model , $model, 'current_model_instance ok');
98
99is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyApp::M::Model' }}, 'MyApp')->model , $model,
100 'current_model_instance precedes current_model ok');
101
102MyApp->config->{default_view} = 'V';
103is ( bless ({stash=>{}}, 'MyApp')->view , 'MyApp::View::V', 'default_view ok');
72f87c4b 104is ( MyApp->view , 'MyApp::View::V', 'default_view in class method ok');
a3b71f0f 105
106MyApp->config->{default_model} = 'M';
107is ( bless ({stash=>{}}, 'MyApp')->model , 'MyApp::Model::M', 'default_model ok');
72f87c4b 108is ( MyApp->model , 'MyApp::Model::M', 'default_model in class method ok');
109
2f381252 110# regexp behavior tests
111{
112 # is_deeply is used because regexp behavior means list context
113 is_deeply( [ MyApp->view( qr{^V[ie]+w$} ) ], [ 'MyApp::V::View' ], 'regexp view ok' );
114 is_deeply( [ MyApp->controller( qr{Dummy\::Model$} ) ], [ 'MyApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
115 is_deeply( [ MyApp->model( qr{Dum{2}y} ) ], [ 'MyApp::Model::Dummy::Model' ], 'regexp model ok' );
116
117 # object w/ qr{}
118 is_deeply( [ MyApp->model( qr{Test} ) ], [ MyApp->components->{'MyApp::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( [ MyApp->model( 'Test' ) ], [ MyApp->components->{'MyApp::Model::Test::Object'} ], 'Object returned' );
127 ok( $warnings, 'regexp fallback warnings' );
128 }
129
130 is_deeply( [ MyApp->view('MyApp::V::View$') ], [ 'MyApp::V::View' ], 'Explicit return ok');
131 is_deeply( [ MyApp->controller('MyApp::C::Controller$') ], [ 'MyApp::C::Controller' ], 'Explicit return ok');
132 is_deeply( [ MyApp->model('MyApp::M::Model$') ], [ 'MyApp::M::Model' ], 'Explicit return ok');
133}
134
135{
136 my @expected = qw( MyApp::C::Controller MyApp::Controller::C );
137 is_deeply( [ sort MyApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' );
138}
139
140{
141 my @expected = qw( MyApp::V::View MyApp::View::V );
142 is_deeply( [ sort MyApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' );
143}
144
145{
146 my @expected = qw( MyApp::M::Model MyApp::Model::M );
147 is_deeply( [ sort MyApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' );
148}
149
150# failed search
151{
152 is( scalar MyApp->controller( qr{DNE} ), 0, '0 results for failed search' );
153}
154
72f87c4b 155#checking @args passed to ACCEPT_CONTEXT
72f87c4b 156{
2f381252 157 my $args;
158
15a32cd5 159 {
160 no warnings 'once';
161 *MyApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
162 *MyApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
163 }
164
165 my $c = bless {}, 'MyApp';
2f381252 166
3fc0073c 167 # test accept-context with class rather than instance
168 MyApp->model('M', qw/foo bar/);
8abaac85 169 is_deeply($args, [qw/foo bar/], 'MyApp->model args passed to ACCEPT_CONTEXT ok');
3fc0073c 170
171
15a32cd5 172 $c->model('M', qw/foo bar/);
2f381252 173 is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
174
15a32cd5 175 my $x = $c->view('V', qw/foo2 bar2/);
2f381252 176 is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
177
178 # regexp fallback
15a32cd5 179 $c->view('::View::V', qw/foo3 bar3/);
2f381252 180 is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok');
77fde521 181
8abaac85 182
2f381252 183}