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