Copied and adapted tests from Catalyst::Plugin::ConfigLoader
André Walker [Wed, 8 Jun 2011 14:33:09 +0000 (11:33 -0300)]
12 files changed:
lib/Catalyst.pm
t/aggregate/unit_core_container_live_auto.t [new file with mode: 0644]
t/aggregate/unit_core_container_mock_load.t [new file with mode: 0644]
t/aggregate/unit_core_container_mock_load_env.t [new file with mode: 0644]
t/aggregate/unit_core_container_path_env.t [new file with mode: 0644]
t/aggregate/unit_core_container_suffix_env.t [new file with mode: 0644]
t/lib/TestAppContainer.pm [new file with mode: 0644]
t/lib/TestAppContainer/Controller/Config.pm [new file with mode: 0644]
t/lib/TestAppContainer/Controller/Root.pm [new file with mode: 0644]
t/lib/TestAppContainer/testappcontainer.pl [new file with mode: 0644]
t/mockapp/mockapp.pl [new file with mode: 0644]
t/mockapp/mockapp_local.pl [new file with mode: 0644]

index f2a9228..2dd7d5d 100644 (file)
@@ -2453,7 +2453,9 @@ sub setup_actions { my $c = shift; $c->dispatcher->setup_actions( $c, @_ ) }
 sub setup_config {
     my $class = shift;
 
-    my %args = %{$class->config || {} };
+    my %args = %{ $class->config || {} };
+
+# FIXME: what is this 'MyApp' doing here?
     my @container_classes = qw/MyApp::Container Catalyst::Container/;
     unshift @container_classes, delete $args{container_class} if exists $args{container_class};
 
diff --git a/t/aggregate/unit_core_container_live_auto.t b/t/aggregate/unit_core_container_live_auto.t
new file mode 100644 (file)
index 0000000..74f5c3e
--- /dev/null
@@ -0,0 +1,30 @@
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/../lib";
+
+use Test::More tests => 5;
+
+use Catalyst::Test 'TestAppContainer';
+
+{
+    my $response;
+    ok( $response = request( 'http://localhost/config/' ), 'request ok' );
+    is( $response->content, 'foo', 'config ok' );
+
+    $response = request( 'http://localhost/appconfig/cache' );
+    ok( $response->content && $response->content !~ /^__HOME__/,
+        'home dir substituted in config var'
+    );
+
+    $response = request( 'http://localhost/appconfig/foo' );
+    is( $response->content, 'bar', 'app finalize_config works' );
+
+    $response = request( 'http://localhost/appconfig/multi' );
+    my $home = TestAppContainer->config->{ home };
+    my $path = join( ',',
+        $home, TestAppContainer->path_to( 'x' ),
+        $home, TestAppContainer->path_to( 'y' ) );
+    is( $response->content, $path, 'vars substituted in config var, twice' );
+}
diff --git a/t/aggregate/unit_core_container_mock_load.t b/t/aggregate/unit_core_container_mock_load.t
new file mode 100644 (file)
index 0000000..695b2ab
--- /dev/null
@@ -0,0 +1,29 @@
+package MockApp;
+
+use Test::More tests => 10;
+use Cwd;
+
+# Remove all relevant env variables to avoid accidental fail
+foreach my $name ( grep { m{^(CATALYST)} } keys %ENV ) {
+    delete $ENV{ $name };
+}
+
+$ENV{ CATALYST_HOME } = cwd . '/t/mockapp';
+
+use_ok( 'Catalyst' );
+
+__PACKAGE__->config->{ substitutions } = {
+    foo => sub { shift; join( '-', @_ ); }
+};
+
+__PACKAGE__->setup;
+
+ok( my $conf = __PACKAGE__->config );
+is( $conf->{ 'Controller::Foo' }->{ foo }, 'bar' );
+is( $conf->{ 'Controller::Foo' }->{ new }, 'key' );
+is( $conf->{ 'Model::Baz' }->{ qux },      'xyzzy' );
+is( $conf->{ 'Model::Baz' }->{ another },  'new key' );
+is( $conf->{ 'view' },                     'View::TT::New' );
+is( $conf->{ 'foo_sub' },                  'x-y' );
+is( $conf->{ 'literal_macro' },            '__DATA__' );
+is( $conf->{ 'Plugin::Zot' }->{ zoot },    'zooot');
diff --git a/t/aggregate/unit_core_container_mock_load_env.t b/t/aggregate/unit_core_container_mock_load_env.t
new file mode 100644 (file)
index 0000000..55e9015
--- /dev/null
@@ -0,0 +1,30 @@
+package MockApp;
+
+use Test::More tests => 10;
+use Cwd;
+
+# Remove all relevant env variables to avoid accidental fail
+foreach my $name ( grep { m{^(CATALYST|MOCKAPP)} } keys %ENV ) {
+    delete $ENV{ $name };
+}
+
+$ENV{ CATALYST_HOME }  = cwd . '/t/mockapp';
+$ENV{ MOCKAPP_CONFIG } = $ENV{ CATALYST_HOME } . '/mockapp.pl';
+
+use_ok( 'Catalyst' );
+
+__PACKAGE__->config->{substitutions} = {
+    foo => sub { shift; join( '-', @_ ); }
+};
+
+__PACKAGE__->setup;
+
+ok( my $conf = __PACKAGE__->config );
+is( $conf->{ 'Controller::Foo' }->{ foo }, 'bar' );
+is( $conf->{ 'Controller::Foo' }->{ new }, 'key' );
+is( $conf->{ 'Model::Baz' }->{ qux },      'xyzzy' );
+is( $conf->{ 'Model::Baz' }->{ another },  'new key' );
+is( $conf->{ 'view' },                     'View::TT::New' );
+is( $conf->{ 'foo_sub' },                  'x-y' );
+is( $conf->{ 'literal_macro' },            '__DATA__' );
+is( $conf->{ 'environment_macro' },        $ENV{ CATALYST_HOME }.'/mockapp.pl' );
diff --git a/t/aggregate/unit_core_container_path_env.t b/t/aggregate/unit_core_container_path_env.t
new file mode 100644 (file)
index 0000000..b91d185
--- /dev/null
@@ -0,0 +1,15 @@
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/../lib";
+
+use Test::More tests => 3;
+
+$ENV{ TESTAPPCONTAINER_CONFIG } = 'test.perl';
+
+use_ok 'Catalyst::Test', 'TestAppContainer';
+
+ok my ( $res, $c ) = ctx_request( '/' ), 'context object';
+
+is_deeply $c->container->resolve( service => 'config_path' ), [ qw( test.perl perl ) ], 'path is "test.perl"';
diff --git a/t/aggregate/unit_core_container_suffix_env.t b/t/aggregate/unit_core_container_suffix_env.t
new file mode 100644 (file)
index 0000000..589e266
--- /dev/null
@@ -0,0 +1,14 @@
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/../lib";
+
+use Test::More tests => 3;
+
+$ENV{ TESTAPPCONTAINER_CONFIG_LOCAL_SUFFIX } = 'test';
+use_ok 'Catalyst::Test', 'TestAppContainer';
+
+ok my ( $res, $c ) = ctx_request( '/' ), 'context object';
+
+is $c->container->resolve( service => 'config_local_suffix' ), 'test', 'suffix is "test"';
diff --git a/t/lib/TestAppContainer.pm b/t/lib/TestAppContainer.pm
new file mode 100644 (file)
index 0000000..55975da
--- /dev/null
@@ -0,0 +1,20 @@
+package TestAppContainer;
+
+use strict;
+use warnings;
+
+use MRO::Compat;
+
+use Catalyst;
+
+our $VERSION = '0.01';
+
+__PACKAGE__->setup;
+
+sub finalize_config {
+    my $c = shift;
+    $c->config( foo => 'bar' );
+    $c->next::method( @_ );
+}
+
+1;
diff --git a/t/lib/TestAppContainer/Controller/Config.pm b/t/lib/TestAppContainer/Controller/Config.pm
new file mode 100644 (file)
index 0000000..a5b8264
--- /dev/null
@@ -0,0 +1,18 @@
+package TestAppContainer::Controller::Config;
+
+use strict;
+use warnings;
+
+use base qw( Catalyst::Controller );
+
+sub index : Private {
+    my ( $self, $c ) = @_;
+    $c->res->output( $self->{ foo } );
+}
+
+sub appconfig : Global {
+    my ( $self, $c, $var ) = @_;
+    $c->res->body( $c->config->{ $var } );
+}
+
+1;
diff --git a/t/lib/TestAppContainer/Controller/Root.pm b/t/lib/TestAppContainer/Controller/Root.pm
new file mode 100644 (file)
index 0000000..b628056
--- /dev/null
@@ -0,0 +1,16 @@
+package TestAppContainer::Controller::Root;
+
+use strict;
+use warnings;
+
+use base 'Catalyst::Controller';
+
+__PACKAGE__->config->{namespace} = '';
+
+sub default :Path {
+    my ( $self, $c ) = @_;
+    $c->response->body( 'Page not found' );
+    $c->response->status(404);
+}
+
+1;
diff --git a/t/lib/TestAppContainer/testappcontainer.pl b/t/lib/TestAppContainer/testappcontainer.pl
new file mode 100644 (file)
index 0000000..e3856d2
--- /dev/null
@@ -0,0 +1,6 @@
+{
+    name                 => 'TestAppContainer',
+    'Controller::Config' => { foo => 'foo' },
+    cache                => '__HOME__/cache',
+    multi                => '__HOME__,__path_to(x)__,__HOME__,__path_to(y)__',
+}
diff --git a/t/mockapp/mockapp.pl b/t/mockapp/mockapp.pl
new file mode 100644 (file)
index 0000000..73d17f9
--- /dev/null
@@ -0,0 +1,10 @@
+{
+    name              => 'TestAppContainer',
+    view              => 'View::TT',
+    'Controller::Foo' => { foo => 'bar' },
+    'Model::Baz'      => { qux => 'xyzzy' },
+    foo_sub           => '__foo(x,y)__',
+    literal_macro     => '__literal(__DATA__)__',
+    environment_macro => '__ENV(CATALYST_HOME)__/mockapp.pl',
+    Plugin            => { Zot => { zoot => 'zooot' } },
+}
diff --git a/t/mockapp/mockapp_local.pl b/t/mockapp/mockapp_local.pl
new file mode 100644 (file)
index 0000000..81660fe
--- /dev/null
@@ -0,0 +1,5 @@
+{
+    view              => 'View::TT::New',
+    'Controller::Foo' => { new => 'key' },
+    Component         => { 'Model::Baz' => { 'another' => 'new key' } },
+}