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' } |
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) = @_; |
e39312ba |
72 | |
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'); |
76 | |
ec4d7259 |
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({ |
58ece1ea |
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'] }, |
3e560748 |
95 | }, |
58ece1ea |
96 | 'Controller::Err' => { a => 100, b=>200, namespace=>'error' }, |
97 | 'Model::Zoo' => { a => 2 }, |
98 | 'Model::Foo' => { a => 100 }, |
ec4d7259 |
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 | |
3e560748 |
111 | { |
112 | my $res = request '/error/not_found'; |
113 | is $res->content, 'Not Found'; |
114 | } |
115 | |
ec4d7259 |
116 | done_testing; |