3 use HTTP::Request::Common;
13 package Local::Model::Foo;
16 extends 'Catalyst::Model';
18 has a => (is=>'ro', required=>1);
21 sub foo { shift->a . 'foo' }
23 package Local::Controller::Errors;
26 use MooseX::MethodAttributes;
28 extends 'Catalyst::Controller';
30 has ['a', 'b'] => (is=>'ro', required=>1);
32 sub not_found :Local { pop->res->from_psgi_response([404, [], ['Not Found']]) }
34 package MyApp::Model::User;
35 $INC{'MyApp/Model/User.pm'} = __FILE__;
38 extends 'Catalyst::Model';
40 has 'zoo' => (is=>'ro', required=>1, isa=>'Object');
42 around 'COMPONENT', sub {
43 my ($orig, $class, $app, $config) = @_;
44 $config->{zoo} = $app->model('Zoo');
46 return $class->$orig($app, $config);
50 1 => { name => 'john', age => 46 },
51 2 => { name => 'mary', age => 36 },
52 3 => { name => 'ian', age => 25 },
53 4 => { name => 'visha', age => 18 },
58 my $user = $users{$id} || return;
59 return bless $user, "MyApp::Model::User::user";
62 package MyApp::Controller::Root;
63 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
66 use MooseX::MethodAttributes;
68 extends 'Catalyst::Controller';
70 sub user :Local Args(1) {
71 my ($self, $c, $int) = @_;
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');
79 $c->res->body("name: $user->{name}, age: $user->{age}");
82 sub default :Default {
83 my ($self, $c, $int) = @_;
84 $c->res->body('default');
87 MyApp::Controller::Root->config(namespace=>'');
92 MyApp->inject_components(
93 'Model::One' => { from_component => 'Local::Model::Foo' },
94 'Model::Two' => { from_component => 'Local::Model::Foo' },
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'] },
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' },
114 use Catalyst::Test 'MyApp';
117 my $res = request '/user/1';
118 is $res->content, 'name: john, age: 46';
122 my $res = request '/error/not_found';
123 is $res->content, 'Not Found';