drop IO::Scalar prereq
[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     Test::More::is($c->model("One")->a, 'one');
77     Test::More::is($c->model("Two")->a, 'two');
78
79     $c->res->body("name: $user->{name}, age: $user->{age}");
80   }
81
82   sub default :Default {
83     my ($self, $c, $int) = @_;
84     $c->res->body('default');
85   }
86
87   MyApp::Controller::Root->config(namespace=>'');
88
89   package MyApp;
90   use Catalyst;
91
92   MyApp->inject_components(
93       'Model::One' => { from_component => 'Local::Model::Foo' },
94       'Model::Two' => { from_component => 'Local::Model::Foo' },
95   );
96
97   MyApp->config({
98     inject_components => {
99       'Controller::Err' => { from_component => 'Local::Controller::Errors' },
100       'Model::Zoo' => { from_component => 'Local::Model::Foo' },
101       'Model::Foo' => { from_component => 'Local::Model::Foo', roles => ['TestRole'] },
102     },
103     'Controller::Err' => { a => 100, b => 200, namespace => 'error' },
104     'Model::Zoo' => { a => 2 },
105     'Model::Foo' => { a => 100 },
106     'Model::One' => { a => 'one' },
107     'Model::Two' => { a => 'two' },
108
109   });
110
111   MyApp->setup;
112 }
113
114 use Catalyst::Test 'MyApp';
115
116 {
117   my $res = request '/user/1';
118   is $res->content, 'name: john, age: 46';
119 }
120
121 {
122   my $res = request '/error/not_found';
123   is $res->content, 'Not Found';
124 }
125
126 done_testing;