some playing
[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;
a5f37ed6 35 my $app = $self->app_name;
850a9bbe 36
37 $ENV{TEST_APP_CURRENT_CONTAINER} = $self->container_class;
38
39 require Catalyst::Test;
a5f37ed6 40 Catalyst::Test->import($app);
850a9bbe 41
a5f37ed6 42 is($app->config('container_class'), $self->container_class, 'config is set properly');
43 isa_ok($app->container, $self->container_class, 'and container isa our container class');
850a9bbe 44
fe3f6b6d 45 {
04189a0f 46 # DefaultSetup ACCEPT_CONTEXT called - total: 1
287df7b2 47 ok(my ($res, $c) = ctx_request('/'), 'request');
fe3f6b6d 48 ok($res->is_success, 'request 2xx');
287df7b2 49 is($res->content, 'foo', 'content is expected');
fe3f6b6d 50
54f4b8d1 51 ok(my $baz = $c->container->get_sub_container('model')->resolve(service => 'RequestLifeCycle', parameters => { ctx => $c, accept_context_args => [$c] } ), 'fetching RequestLifeCycle');
52 isa_ok($baz, 'TestAppCustomContainer::Model::RequestLifeCycle');
fe3f6b6d 53 is($baz->accept_context_called, 1, 'ACCEPT_CONTEXT called');
54f4b8d1 54 isa_ok($baz->foo, 'TestAppCustomContainer::Model::Foo', 'RequestLifeCycle got Foo ok');
fe3f6b6d 55
04189a0f 56 # DefaultSetup ACCEPT_CONTEXT called - total: 2
e9af4f7d 57 ok(get('/get_model_baz'), 'another request');
a0475a83 58 is($baz->accept_context_called, 1, 'ACCEPT_CONTEXT not called again (instance per context)');
b32d8169 59# is($foo->baz_got_it, 2, 'Baz accessed Foo again');
e9af4f7d 60 }
61
62 {
04189a0f 63 # DefaultSetup ACCEPT_CONTEXT called - total: 3
e9af4f7d 64 ok(my ($res, $c) = ctx_request('/get_model_bar'), 'request');
65 ok($res->is_success, 'request 2xx');
66 is($res->content, 'TestAppCustomContainer::Model::Bar', 'content is expected');
67
68 ok(my $bar = $c->container->get_sub_container('component')->resolve(service => 'model_Bar'), 'fetching Bar');
69 isa_ok($bar, 'TestAppCustomContainer::Model::Bar');
70
71 # FIXME - is this expected behavior?
72 is($bar->accept_context_called, 1, 'ACCEPT_CONTEXT called');
73 isa_ok($bar->foo, 'TestAppCustomContainer::Model::Foo', 'Bar got Foo ok');
74
04189a0f 75 # DefaultSetup ACCEPT_CONTEXT *not* called - total: 3
e9af4f7d 76 ok(get('/get_model_bar'), 'another request');
77 is($bar->accept_context_called, 1, 'ACCEPT_CONTEXT not called again (lifecycle is Singleton)');
b32d8169 78# is($foo->bar_got_it, 1, 'Bar didn\'t access Foo again');
fe3f6b6d 79 }
80
17f0d82d 81 {
04189a0f 82 # DefaultSetup ACCEPT_CONTEXT called - total: 4
17f0d82d 83 ok(my ($res, $c) = ctx_request('/get_model_foo'), 'request');
84 ok($res->is_success, 'request 2xx');
85 is($res->content, 'TestAppCustomContainer::Model::Foo', 'content is expected');
86
b32d8169 87# ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
88# isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
89# is($foo->accept_context_called, 4, 'ACCEPT_CONTEXT called');
90# is($foo->bar_got_it, 1, 'Bar accessed Foo once');
91# is($foo->baz_got_it, 2, 'Baz accessed Foo twice');
17f0d82d 92 }
93
850a9bbe 94 done_testing;
95}
96
97sub _build_container_class {
98 my $self = shift;
99
100 my $sugar = $self->sugar ? '' : 'No';
101
102 return $self->app_name . "::${sugar}SugarContainer";
103}
104
105__PACKAGE__->meta->make_immutable;
106
1071;