example "delayed" component setup
[catagits/Catalyst-Runtime.git] / t / configured_comps.t
CommitLineData
ec4d7259 1use warnings;
2use strict;
3use HTTP::Request::Common;
4use Test::More;
5
6{
3e560748 7 package Local::Model::Foo;
8
9 use Moose;
10 extends 'Catalyst::Model';
11
12 has a => (is=>'ro', required=>1);
13
14 sub foo { shift->a . 'foo' }
15
ec4d7259 16 package Local::Controller::Errors;
17
18 use Moose;
19 use MooseX::MethodAttributes;
20
21 extends 'Catalyst::Controller';
22
23 has ['a', 'b'] => (is=>'ro', required=>1);
24
3e560748 25 sub not_found :Local { pop->res->from_psgi_response([404, [], ['Not Found']]) }
ec4d7259 26
27 package MyApp::Model::User;
28 $INC{'MyApp/Model/User.pm'} = __FILE__;
29
3e560748 30 use Moose;
31 extends 'Catalyst::Model';
32
33 has 'zoo' => (is=>'ro', required=>1, isa=>'Object');
34
35 around 'COMPONENT', sub {
36 my ($orig, $class, $app, $config) = @_;
37 $config->{zoo} = $app->model('Zoo');
38
39 return $class->$orig($app, $config);
40 };
ec4d7259 41
42 our %users = (
43 1 => { name => 'john', age => 46 },
44 2 => { name => 'mary', age => 36 },
45 3 => { name => 'ian', age => 25 },
46 4 => { name => 'visha', age => 18 },
47 );
48
49 sub find {
50 my ($self, $id) = @_;
51 my $user = $users{$id} || return;
52 return bless $user, "MyApp::Model::User::user";
53 }
54
55 package MyApp::Controller::Root;
56 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
57
58 use Moose;
59 use MooseX::MethodAttributes;
60
61 extends 'Catalyst::Controller';
62
63 sub user :Local Args(1) {
64 my ($self, $c, $int) = @_;
65 my $user = $c->model("User")->find($int);
3e560748 66
67 $c->model("User")->zoo->a;
68
ec4d7259 69 $c->res->body("name: $user->{name}, age: $user->{age}");
70 }
71
72 sub default :Default {
73 my ($self, $c, $int) = @_;
74 $c->res->body('default');
75 }
76
77 MyApp::Controller::Root->config(namespace=>'');
78
79 package MyApp;
80 use Catalyst;
81
82 MyApp->config({
83 'Controller::Err' => {
3e560748 84 from_component => 'Local::Controller::Errors',
85 args => { a=> 100, b => 200, namespace =>'error' },
86 },
87 'Model::Zoo' => {
88 from_component => 'Local::Model::Foo',
89 args => {a=>2},
90 },
91 'Model::Foo' => {
92 from_component => 'Local::Model::Foo',
93 args => { a=> 100 },
94 },
95
ec4d7259 96 });
97
98 MyApp->setup;
99}
100
101use Catalyst::Test 'MyApp';
102
103{
104 my $res = request '/user/1';
105 is $res->content, 'name: john, age: 46';
106}
107
3e560748 108{
109 my $res = request '/error/not_found';
110 is $res->content, 'Not Found';
111}
112
ec4d7259 113done_testing;