1 package TestCustomContainer;
3 use namespace::autoclean;
9 default => 'TestAppCustomContainer',
12 has container_class => (
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.
36 $ENV{TEST_APP_CURRENT_CONTAINER} = $self->container_class;
38 require Catalyst::Test;
39 Catalyst::Test->import($self->app_name);
41 is(get('/container_class'), $self->container_class);
42 is(get('/container_isa'), $self->container_class);
45 ok(my ($res, $c) = ctx_request('/get_model_baz'), 'request');
46 ok($res->is_success, 'request 2xx');
47 is($res->content, 'TestAppCustomContainer::Model::Baz', 'content is expected');
49 ok(my $baz = $c->container->get_sub_container('component')->resolve(service => 'model_Baz'), 'fetching Baz');
50 isa_ok($baz, 'TestAppCustomContainer::Model::Baz');
51 is($baz->accept_context_called, 1, 'ACCEPT_CONTEXT called');
52 isa_ok($baz->foo, 'TestAppCustomContainer::Model::Foo', 'Baz got Foo ok');
54 ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
55 isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
56 is($foo->baz_got_it, 1, 'Baz accessed Foo once');
58 ok(get('/get_model_baz'), 'another request');
59 is($baz->accept_context_called, 2, 'ACCEPT_CONTEXT called again');
60 is($foo->baz_got_it, 2, 'Baz accessed Foo again');
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');
68 ok(my $bar = $c->container->get_sub_container('component')->resolve(service => 'model_Bar'), 'fetching Bar');
69 isa_ok($bar, 'TestAppCustomContainer::Model::Bar');
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');
75 ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
76 isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
77 is($foo->bar_got_it, 1, 'Bar accessed Foo once');
79 ok(get('/get_model_bar'), 'another request');
80 is($bar->accept_context_called, 1, 'ACCEPT_CONTEXT not called again (lifecycle is Singleton)');
81 is($foo->bar_got_it, 1, 'Bar didn\'t access Foo again');
87 sub _build_container_class {
90 my $sugar = $self->sugar ? '' : 'No';
92 return $self->app_name . "::${sugar}SugarContainer";
95 __PACKAGE__->meta->make_immutable;