1cc0d1ad451727584e7b281e9263206b38663d22
[catagits/Catalyst-Runtime.git] / t / configured_comps.t
1 use warnings;
2 use strict;
3 use HTTP::Request::Common;
4 use Test::More;
5
6 {
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
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
25   sub not_found :Local { pop->res->from_psgi_response([404, [], ['Not Found']]) }
26
27   package MyApp::Model::User;
28   $INC{'MyApp/Model/User.pm'} = __FILE__;
29
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   };
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);
66
67      $c->model("User")->zoo->a;
68     
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' => {
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
96   });
97
98   MyApp->setup;
99 }
100
101 use Catalyst::Test 'MyApp';
102
103 {
104   my $res = request '/user/1';
105   is $res->content, 'name: john, age: 46';
106 }
107
108 {
109   my $res = request '/error/not_found';
110   is $res->content, 'Not Found';
111 }
112
113 done_testing;