Lets stop shitting everywhere
[catagits/Catalyst-Runtime.git] / t / lib / TestCustomContainer.pm
CommitLineData
850a9bbe 1package TestCustomContainer;
2use Moose;
3use namespace::autoclean;
4use Test::More;
5
6has app_name => (
7 is => 'ro',
8 isa => 'Str',
9 default => 'TestAppCustomContainer',
10);
11
12has container_class => (
13 is => 'ro',
14 isa => 'Str',
15 lazy_build => 1,
16);
17
18has sugar => (
19 is => 'ro',
20 isa => 'Int',
21);
22
95dda84f 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
850a9bbe 33sub 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
17f0d82d 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');
850a9bbe 43
fe3f6b6d 44 {
a0475a83 45 # Foo ACCEPT_CONTEXT called - total: 1
fe3f6b6d 46 ok(my ($res, $c) = ctx_request('/get_model_baz'), 'request');
47 ok($res->is_success, 'request 2xx');
48 is($res->content, 'TestAppCustomContainer::Model::Baz', 'content is expected');
49
a0475a83 50 ok(my $baz = $c->container->get_sub_container('model')->resolve(service => 'Baz', parameters => { ctx => $c, accept_context_args => [$c] } ), 'fetching Baz');
fe3f6b6d 51 isa_ok($baz, 'TestAppCustomContainer::Model::Baz');
52 is($baz->accept_context_called, 1, 'ACCEPT_CONTEXT called');
53 isa_ok($baz->foo, 'TestAppCustomContainer::Model::Foo', 'Baz got Foo ok');
54
b32d8169 55# ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
56# isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
57# is($foo->baz_got_it, 1, 'Baz accessed Foo once');
e9af4f7d 58
a0475a83 59 # Foo ACCEPT_CONTEXT called - total: 2
e9af4f7d 60 ok(get('/get_model_baz'), 'another request');
a0475a83 61 is($baz->accept_context_called, 1, 'ACCEPT_CONTEXT not called again (instance per context)');
b32d8169 62# is($foo->baz_got_it, 2, 'Baz accessed Foo again');
e9af4f7d 63 }
64
65 {
a0475a83 66 # Foo ACCEPT_CONTEXT called - total: 3
e9af4f7d 67 ok(my ($res, $c) = ctx_request('/get_model_bar'), 'request');
68 ok($res->is_success, 'request 2xx');
69 is($res->content, 'TestAppCustomContainer::Model::Bar', 'content is expected');
70
71 ok(my $bar = $c->container->get_sub_container('component')->resolve(service => 'model_Bar'), 'fetching Bar');
72 isa_ok($bar, 'TestAppCustomContainer::Model::Bar');
73
74 # FIXME - is this expected behavior?
75 is($bar->accept_context_called, 1, 'ACCEPT_CONTEXT called');
76 isa_ok($bar->foo, 'TestAppCustomContainer::Model::Foo', 'Bar got Foo ok');
77
b32d8169 78# ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
79# isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
80# is($foo->bar_got_it, 1, 'Bar accessed Foo once');
e9af4f7d 81
a0475a83 82 # Foo ACCEPT_CONTEXT *not* called - total: 3
e9af4f7d 83 ok(get('/get_model_bar'), 'another request');
84 is($bar->accept_context_called, 1, 'ACCEPT_CONTEXT not called again (lifecycle is Singleton)');
b32d8169 85# is($foo->bar_got_it, 1, 'Bar didn\'t access Foo again');
fe3f6b6d 86 }
87
17f0d82d 88 {
a0475a83 89 # Foo ACCEPT_CONTEXT called - total: 4
17f0d82d 90 ok(my ($res, $c) = ctx_request('/get_model_foo'), 'request');
91 ok($res->is_success, 'request 2xx');
92 is($res->content, 'TestAppCustomContainer::Model::Foo', 'content is expected');
93
b32d8169 94# ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
95# isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
96# is($foo->accept_context_called, 4, 'ACCEPT_CONTEXT called');
97# is($foo->bar_got_it, 1, 'Bar accessed Foo once');
98# is($foo->baz_got_it, 2, 'Baz accessed Foo twice');
17f0d82d 99 }
100
850a9bbe 101 done_testing;
102}
103
104sub _build_container_class {
105 my $self = shift;
106
107 my $sugar = $self->sugar ? '' : 'No';
108
109 return $self->app_name . "::${sugar}SugarContainer";
110}
111
112__PACKAGE__->meta->make_immutable;
113
1141;