test model Foo + small fixes
[catagits/Catalyst-Runtime.git] / t / lib / TestCustomContainer.pm
1 package TestCustomContainer;
2 use Moose;
3 use namespace::autoclean;
4 use Test::More;
5
6 has app_name => (
7     is => 'ro',
8     isa => 'Str',
9     default => 'TestAppCustomContainer',
10 );
11
12 has container_class => (
13     is => 'ro',
14     isa => 'Str',
15     lazy_build => 1,
16 );
17
18 has sugar => (
19     is => 'ro',
20     isa => 'Int',
21 );
22
23 # Reason for this class:
24 # I wanted have a set of tests that would test both the sugar version of the
25 # container, as the sugar-less. I figured I shouldn't just copy and paste
26 # the tests. So after struggling for hours to find a way to test twice
27 # against the same TestApp using only one file, I decided to break it
28 # into a separate class (this one), and call it at
29 #           -  live_container_custom_container_sugar.t and
30 #           -  live_container_custom_container_nosugar.t
31 # setting only the sugar attribute.
32
33 sub BUILD {
34     my $self = shift;
35
36     $ENV{TEST_APP_CURRENT_CONTAINER} = $self->container_class;
37
38     require Catalyst::Test;
39     Catalyst::Test->import($self->app_name);
40
41     is(get('/container_class'), $self->container_class, 'config is set properly');
42     is(get('/container_isa'), $self->container_class,   'and container isa our container class');
43
44     {
45         # Foo ACCEPT_CONTEXT called twice - total: 2
46         # TestAppCustomContainer::Role::HoldsFoo COMPONENT
47         # and in the block injection
48         ok(my ($res, $c) = ctx_request('/get_model_baz'), 'request');
49         ok($res->is_success, 'request 2xx');
50         is($res->content, 'TestAppCustomContainer::Model::Baz', 'content is expected');
51
52         ok(my $baz = $c->container->get_sub_container('component')->resolve(service => 'model_Baz'), 'fetching Baz');
53         isa_ok($baz, 'TestAppCustomContainer::Model::Baz');
54         is($baz->accept_context_called, 1, 'ACCEPT_CONTEXT called');
55         isa_ok($baz->foo, 'TestAppCustomContainer::Model::Foo', 'Baz got Foo ok');
56
57         ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
58         isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
59         is($foo->baz_got_it, 1, 'Baz accessed Foo once');
60
61         # Foo ACCEPT_CONTEXT called - total: 3
62         ok(get('/get_model_baz'), 'another request');
63         is($baz->accept_context_called, 2, 'ACCEPT_CONTEXT called again');
64         is($foo->baz_got_it, 2, 'Baz accessed Foo again');
65     }
66
67     {
68         # Foo ACCEPT_CONTEXT called twice - total: 5
69         ok(my ($res, $c) = ctx_request('/get_model_bar'), 'request');
70         ok($res->is_success, 'request 2xx');
71         is($res->content, 'TestAppCustomContainer::Model::Bar', 'content is expected');
72
73         ok(my $bar = $c->container->get_sub_container('component')->resolve(service => 'model_Bar'), 'fetching Bar');
74         isa_ok($bar, 'TestAppCustomContainer::Model::Bar');
75
76         # FIXME - is this expected behavior?
77         is($bar->accept_context_called, 1, 'ACCEPT_CONTEXT called');
78         isa_ok($bar->foo, 'TestAppCustomContainer::Model::Foo', 'Bar got Foo ok');
79
80         ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
81         isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
82         is($foo->bar_got_it, 1, 'Bar accessed Foo once');
83
84         # Foo ACCEPT_CONTEXT *not* called - total: 5
85         ok(get('/get_model_bar'), 'another request');
86         is($bar->accept_context_called, 1, 'ACCEPT_CONTEXT not called again (lifecycle is Singleton)');
87         is($foo->bar_got_it, 1, 'Bar didn\'t access Foo again');
88     }
89
90     {
91         # Foo ACCEPT_CONTEXT called - total: 6
92         ok(my ($res, $c) = ctx_request('/get_model_foo'), 'request');
93         ok($res->is_success, 'request 2xx');
94         is($res->content, 'TestAppCustomContainer::Model::Foo', 'content is expected');
95
96         ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
97         isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
98         is($foo->accept_context_called, 6, 'ACCEPT_CONTEXT called');
99         is($foo->bar_got_it, 1, 'Bar accessed Foo once');
100         is($foo->baz_got_it, 2, 'Baz accessed Foo twice');
101     }
102
103     done_testing;
104 }
105
106 sub _build_container_class {
107     my $self = shift;
108
109     my $sugar = $self->sugar ? '' : 'No';
110
111     return $self->app_name . "::${sugar}SugarContainer";
112 }
113
114 __PACKAGE__->meta->make_immutable;
115
116 1;