Merge branch 'master' into gsoc_breadboard
[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     my $app  = $self->app_name;
36
37     $ENV{TEST_APP_CURRENT_CONTAINER} = $self->container_class;
38
39     require Catalyst::Test;
40     Catalyst::Test->import($app);
41
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');
44
45     # RequestLifeCycle
46     {
47         # just to be sure the app is not broken
48         ok(my ($res, $ctx) = ctx_request('/'), 'request');
49         ok($res->is_success, 'request 2xx');
50         is($res->content, 'foo', 'content is expected');
51
52         ok(my $model = $ctx->container->get_sub_container('model')->resolve(service => 'RequestLifeCycle', parameters => { ctx => $ctx, accept_context_args => [$ctx] } ), 'fetching RequestLifeCycle');
53         isa_ok($model, 'TestAppCustomContainer::Model::RequestLifeCycle');
54
55         ok(my $model2 = $ctx->model('RequestLifeCycle'), 'fetching RequestLifeCycle again');
56         is($model, $model2, 'object is not recreated during the same request');
57
58         # another request
59         my ($res2, $ctx2) = ctx_request('/');
60         ok($model2 = $ctx2->model('RequestLifeCycle'), 'fetching RequestLifeCycle again');
61         isnt($model, $model2, 'object is recreated in a different request');
62     }
63
64     # SingletonLifeCycle
65     {
66         # already tested, I only need the $ctx
67         my ($res, $ctx) = ctx_request('/');
68
69         ok(my $model = $ctx->container->get_sub_container('model')->resolve(service => 'SingletonLifeCycle', parameters => { ctx => $ctx, accept_context_args => [$ctx] } ), 'fetching SingletonLifeCycle');
70         isa_ok($model, 'TestAppCustomContainer::Model::SingletonLifeCycle');
71
72         ok(my $model2 = $ctx->model('SingletonLifeCycle'), 'fetching SingletonLifeCycle again');
73         is($model, $model2, 'object is not recreated during the same request');
74
75         # another request
76         my ($res2, $ctx2) = ctx_request('/');
77         ok($model2 = $ctx2->model('SingletonLifeCycle'), 'fetching SingletonLifeCycle again');
78         is($model, $model2, 'object is not recreated in a different request');
79     }
80
81     done_testing;
82 }
83
84 sub _build_container_class {
85     my $self = shift;
86
87     my $sugar = $self->sugar ? '' : 'No';
88
89     return $self->app_name . "::${sugar}SugarContainer";
90 }
91
92 __PACKAGE__->meta->make_immutable;
93
94 1;