Commit | Line | Data |
ec4d7259 |
1 | use warnings; |
2 | use strict; |
3 | use HTTP::Request::Common; |
4 | use Test::More; |
5 | |
6 | { |
58ece1ea |
7 | package TestRole; |
8 | |
9 | use Moose::Role; |
10 | |
11 | sub role { 'role' } |
88e5a8b0 |
12 | |
3e560748 |
13 | package Local::Model::Foo; |
14 | |
15 | use Moose; |
16 | extends 'Catalyst::Model'; |
17 | |
18 | has a => (is=>'ro', required=>1); |
58ece1ea |
19 | has b => (is=>'ro'); |
3e560748 |
20 | |
21 | sub foo { shift->a . 'foo' } |
22 | |
ec4d7259 |
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 | |
3e560748 |
32 | sub not_found :Local { pop->res->from_psgi_response([404, [], ['Not Found']]) } |
ec4d7259 |
33 | |
34 | package MyApp::Model::User; |
35 | $INC{'MyApp/Model/User.pm'} = __FILE__; |
36 | |
3e560748 |
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 | }; |
ec4d7259 |
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) = @_; |
88e5a8b0 |
72 | |
e39312ba |
73 | Test::More::ok(my $user = $c->model("User")->find($int)); |
58ece1ea |
74 | Test::More::is($c->model("User")->zoo->a, 2); |
75 | Test::More::is($c->model("Foo")->role, 'role'); |
067a21ea |
76 | Test::More::is($c->model("One")->a, 'one'); |
77 | Test::More::is($c->model("Two")->a, 'two'); |
88e5a8b0 |
78 | |
ec4d7259 |
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 | |
067a21ea |
92 | MyApp->inject_components( |
93 | 'Model::One' => { from_component => 'Local::Model::Foo' }, |
94 | 'Model::Two' => { from_component => 'Local::Model::Foo' }, |
95 | ); |
96 | |
ec4d7259 |
97 | MyApp->config({ |
58ece1ea |
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'] }, |
3e560748 |
102 | }, |
e689ecb7 |
103 | 'Controller::Err' => { a => 100, b => 200, namespace => 'error' }, |
58ece1ea |
104 | 'Model::Zoo' => { a => 2 }, |
105 | 'Model::Foo' => { a => 100 }, |
067a21ea |
106 | 'Model::One' => { a => 'one' }, |
107 | 'Model::Two' => { a => 'two' }, |
108 | |
ec4d7259 |
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 | |
3e560748 |
121 | { |
122 | my $res = request '/error/not_found'; |
123 | is $res->content, 'Not Found'; |
124 | } |
125 | |
ec4d7259 |
126 | done_testing; |