Merge branch 'master' into gsoc_breadboard
[catagits/Catalyst-Runtime.git] / t / lib / TestCustomContainer.pm
index 266b267..8481b0c 100644 (file)
@@ -32,70 +32,50 @@ has sugar => (
 
 sub BUILD {
     my $self = shift;
+    my $app  = $self->app_name;
 
     $ENV{TEST_APP_CURRENT_CONTAINER} = $self->container_class;
 
     require Catalyst::Test;
-    Catalyst::Test->import($self->app_name);
+    Catalyst::Test->import($app);
 
-    is(get('/container_class'), $self->container_class, 'config is set properly');
-    is(get('/container_isa'), $self->container_class,   'and container isa our container class');
+    is($app->config->{container_class}, $self->container_class, 'config is set properly');
+    isa_ok($app->container, $self->container_class, 'and container isa our container class');
 
+    # RequestLifeCycle
     {
-        # Foo ACCEPT_CONTEXT called - total: 1
-        ok(my ($res, $c) = ctx_request('/get_model_baz'), 'request');
+        # just to be sure the app is not broken
+        ok(my ($res, $ctx) = ctx_request('/'), 'request');
         ok($res->is_success, 'request 2xx');
-        is($res->content, 'TestAppCustomContainer::Model::Baz', 'content is expected');
+        is($res->content, 'foo', 'content is expected');
 
-        ok(my $baz = $c->container->get_sub_container('model')->resolve(service => 'Baz', parameters => { ctx => $c, accept_context_args => [$c] } ), 'fetching Baz');
-        isa_ok($baz, 'TestAppCustomContainer::Model::Baz');
-        is($baz->accept_context_called, 1, 'ACCEPT_CONTEXT called');
-        isa_ok($baz->foo, 'TestAppCustomContainer::Model::Foo', 'Baz got Foo ok');
+        ok(my $model = $ctx->container->get_sub_container('model')->resolve(service => 'RequestLifeCycle', parameters => { ctx => $ctx, accept_context_args => [$ctx] } ), 'fetching RequestLifeCycle');
+        isa_ok($model, 'TestAppCustomContainer::Model::RequestLifeCycle');
 
-        ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
-        isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
-        is($foo->baz_got_it, 1, 'Baz accessed Foo once');
+        ok(my $model2 = $ctx->model('RequestLifeCycle'), 'fetching RequestLifeCycle again');
+        is($model, $model2, 'object is not recreated during the same request');
 
-        # Foo ACCEPT_CONTEXT called - total: 2
-        ok(get('/get_model_baz'), 'another request');
-        is($baz->accept_context_called, 1, 'ACCEPT_CONTEXT not called again (instance per context)');
-        is($foo->baz_got_it, 2, 'Baz accessed Foo again');
+        # another request
+        my ($res2, $ctx2) = ctx_request('/');
+        ok($model2 = $ctx2->model('RequestLifeCycle'), 'fetching RequestLifeCycle again');
+        isnt($model, $model2, 'object is recreated in a different request');
     }
 
+    # SingletonLifeCycle
     {
-        # Foo ACCEPT_CONTEXT called - total: 3
-        ok(my ($res, $c) = ctx_request('/get_model_bar'), 'request');
-        ok($res->is_success, 'request 2xx');
-        is($res->content, 'TestAppCustomContainer::Model::Bar', 'content is expected');
-
-        ok(my $bar = $c->container->get_sub_container('component')->resolve(service => 'model_Bar'), 'fetching Bar');
-        isa_ok($bar, 'TestAppCustomContainer::Model::Bar');
+        # already tested, I only need the $ctx
+        my ($res, $ctx) = ctx_request('/');
 
-        # FIXME - is this expected behavior?
-        is($bar->accept_context_called, 1, 'ACCEPT_CONTEXT called');
-        isa_ok($bar->foo, 'TestAppCustomContainer::Model::Foo', 'Bar got Foo ok');
+        ok(my $model = $ctx->container->get_sub_container('model')->resolve(service => 'SingletonLifeCycle', parameters => { ctx => $ctx, accept_context_args => [$ctx] } ), 'fetching SingletonLifeCycle');
+        isa_ok($model, 'TestAppCustomContainer::Model::SingletonLifeCycle');
 
-        ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
-        isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
-        is($foo->bar_got_it, 1, 'Bar accessed Foo once');
-
-        # Foo ACCEPT_CONTEXT *not* called - total: 3
-        ok(get('/get_model_bar'), 'another request');
-        is($bar->accept_context_called, 1, 'ACCEPT_CONTEXT not called again (lifecycle is Singleton)');
-        is($foo->bar_got_it, 1, 'Bar didn\'t access Foo again');
-    }
-
-    {
-        # Foo ACCEPT_CONTEXT called - total: 4
-        ok(my ($res, $c) = ctx_request('/get_model_foo'), 'request');
-        ok($res->is_success, 'request 2xx');
-        is($res->content, 'TestAppCustomContainer::Model::Foo', 'content is expected');
+        ok(my $model2 = $ctx->model('SingletonLifeCycle'), 'fetching SingletonLifeCycle again');
+        is($model, $model2, 'object is not recreated during the same request');
 
-        ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
-        isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
-        is($foo->accept_context_called, 4, 'ACCEPT_CONTEXT called');
-        is($foo->bar_got_it, 1, 'Bar accessed Foo once');
-        is($foo->baz_got_it, 2, 'Baz accessed Foo twice');
+        # another request
+        my ($res2, $ctx2) = ctx_request('/');
+        ok($model2 = $ctx2->model('SingletonLifeCycle'), 'fetching SingletonLifeCycle again');
+        is($model, $model2, 'object is not recreated in a different request');
     }
 
     done_testing;