Version 0.35
[catagits/Catalyst-Plugin-Session.git] / lib / Catalyst / Plugin / Session / Test / Store.pm
index 32851ae..78e42ee 100644 (file)
@@ -5,131 +5,163 @@ package Catalyst::Plugin::Session::Test::Store;
 use strict;
 use warnings;
 
-use Test::More tests => 15;
+use utf8;
+
+use Test::More;
 use File::Temp;
 use File::Spec;
 
 use Catalyst ();
 
 sub import {
-       shift;
-       my %args = @_;
+    shift;
+    my %args = @_;
+
+    plan tests => 19 + ($args{extra_tests} || 0);
+
+    my $backend = $args{backend};
+    my $cfg     = $args{config};
+
+    my $p = "Session::Store::$backend";
+    use_ok( my $m = "Catalyst::Plugin::$p" );
+
+    isa_ok( bless( {}, $m ), "Catalyst::Plugin::Session::Store" );
+
+    {
+        package # Hide from PAUSE
+            Catalyst::Plugin::SessionStateTest;
+        use base qw/Catalyst::Plugin::Session::State/;
+
+        no strict 'refs';
+
+        sub get_session_id {
+            my $c = shift;
+            ${ ref($c) . "::session_id" };
+        }
 
-       my $backend = $args{backend};
-       my $cfg     = $args{config};
+        sub set_session_id {
+            my ( $c, $sid ) = @_;
+            ${ ref($c) . "::session_id" } = $sid;
+        }
 
-       my $p = "Session::Store::$backend";
-       use_ok(my $m = "Catalyst::Plugin::$p");
+        sub delete_session_id {
+            my $c = shift;
+            undef ${ ref($c) . "::session_id" };
+        }
+    }
 
-       isa_ok(bless({}, $m), "Catalyst::Plugin::Session::Store");
+    {
 
-       our $restored_session_id;
+        package # Hide from PAUSE
+            SessionStoreTest;
+        use Catalyst qw/Session SessionStateTest/;
+        push our (@ISA), $m;
 
-       {
-               package SessionStoreTest;
-               use Catalyst qw/-Engine=Test Session Session::State/;
-               push our(@ISA), $m;
+        use strict;
+        use warnings;
 
-               our $VERSION = "0.01";
+        use Test::More;
 
-               use Test::More;
+        sub create_session : Global {
+            my ( $self, $c ) = @_;
+            ok( !$c->session_is_valid, "no session id yet" );
+            ok( $c->session,           "session created" );
+            ok( $c->session_is_valid,  "with a session id" );
 
-               sub prepare_cookies {
-                       my $c = shift;
-                       $c->sessionid($restored_session_id) if defined $restored_session_id;
-                       $c->NEXT::prepare_cookies(@_);
-               }
+            $c->session->{magic} = "møøse";
+        }
 
-               sub create_session : Global {
-                       my ( $self, $c ) = @_;
-                       ok(!$c->sessionid, "no session id yet");
-                       ok($c->session, "session created");
-                       ok($c->sessionid, "with a session id");
+        sub recover_session : Global {
+            my ( $self, $c ) = @_;
+            ok( $c->session_is_valid, "session id exists" );
+            is( $c->sessionid, our $session_id,
+                "and is the one we saved in the last action" );
+            ok( $c->session, "a session exists" );
+            is( $c->session->{magic},
+                "møøse",
+                "and it contains what we put in on the last attempt" );
+            $c->delete_session("user logout");
+        }
 
-                       $restored_session_id = $c->sessionid;
+        sub after_session : Global {
+            my ( $self, $c ) = @_;
+            ok( !$c->session_is_valid,      "no session id" );
+            ok( !$c->session->{magic},      "session data not restored" );
+            ok( !$c->session_delete_reason, "no reason for deletion" );
+        }
 
-                       $c->session->{magic} = "møøse";
-               }
+        @{ __PACKAGE__->config->{'Plugin::Session'} }{ keys %$cfg } = values %$cfg;
 
-               sub recover_session : Global {
-                       my ( $self, $c ) = @_;
-                       ok($c->sessionid, "session id exists");
-                       is($c->sessionid, $restored_session_id, "and is the one we saved in the last action");
-                       ok($c->session, "a session exists");
-                       is($c->session->{magic}, "møøse", "and it contains what we put in on the last attempt");
-                       $c->delete_session("user logout");
-                       $restored_session_id = undef;
-               }
+        { __PACKAGE__->setup; }; # Extra block here is an INSANE HACK to get inlined constructor
+                                 # (i.e. to make B::Hooks::EndOfScope fire)
+    }
 
-               sub after_session : Global {
-                       my ( $self, $c ) = @_;
-                       ok(!$c->sessionid, "no session id");
-                       ok(!$c->session->{magic}, "session data not restored");
-                       ok(!$c->session_delete_reason, "no reason for deletion");
-               }
+    {
 
-               @{ __PACKAGE__->config->{session} }{ keys %$cfg } = values %$cfg;
-               
-               __PACKAGE__->setup;
-       }
+        package # Hide from PAUSE
+            SessionStoreTest2;
+        use Catalyst qw/Session SessionStateTest/;
+        push our (@ISA), $m;
 
-       {
-               package SessionStoreTest2;
-               use Catalyst qw/-Engine=Test Session Session::State/;
-               push our(@ISA), $m;
+        use Test::More;
 
-               our $VERSION = "123";
+        sub create_session : Global {
+            my ( $self, $c ) = @_;
 
-               use Test::More;
+            $c->session->{magic} = "møøse";
+        }
 
-               sub prepare_cookies {
-                       my $c = shift;
-                       $c->sessionid($restored_session_id) if defined $restored_session_id;
-                       $c->NEXT::prepare_cookies(@_);
-               }
+        sub recover_session : Global {
+            my ( $self, $c ) = @_;
 
-               sub create_session : Global {
-                       my ( $self, $c ) = @_;
+            ok( !$c->session_is_valid, "session is gone" );
 
-                       $c->session->{magic} = "møøse";
+            is(
+                $c->session_delete_reason,
+                "session expired",
+                "reason is that the session expired"
+            );
 
-                       $restored_session_id = $c->sessionid;
-               }
+            ok( !$c->session->{magic}, "no saved data" );
+        }
 
-               sub recover_session : Global {
-                       my ( $self, $c ) = @_;
+        __PACKAGE__->config->{'Plugin::Session'}{expires} = 0;
 
-                       ok(!$c->sessionid, "no session id");
+        @{ __PACKAGE__->config->{'Plugin::Session'} }{ keys %$cfg } = values %$cfg;
 
-                       is($c->session_delete_reason, "session expired", "reason is that the session expired");
+        { __PACKAGE__->setup; }; # INSANE HACK (the block - as above)
+    }
 
-                       ok(!$c->session->{magic}, "no saved data");
-               }
+    use Test::More;
 
-               __PACKAGE__->config->{session}{expires} = 0;
+    can_ok( $m, "get_session_data" );
+    can_ok( $m, "store_session_data" );
+    can_ok( $m, "delete_session_data" );
+    can_ok( $m, "delete_expired_sessions" );
 
-               @{ __PACKAGE__->config->{session} }{ keys %$cfg } = values %$cfg;
+    {
 
-               __PACKAGE__->setup;
-       }
+        package # Hide from PAUSE
+            t1;
+        use Catalyst::Test "SessionStoreTest";
 
-       {
-               package t1; 
-               use Catalyst::Test "SessionStoreTest";
+        # idiotic void context warning workaround
+        
+        my $x = get("/create_session");
+        $x = get("/recover_session");
+        $x = get("/after_session");
+    }
 
-               get("/create_session");
-               get("/recover_session");
-               get("/after_session");
-       }
+    {
 
-       {
-               package t2;
-               use Catalyst::Test "SessionStoreTest2";
+        package # Hide fram PAUSE
+            t2;
+        use Catalyst::Test "SessionStoreTest2";
 
-               get("/create_session");
-               sleep 1; # let the session expire
-               get("/recover_session");
-       }
+        my $x = get("/create_session");
+        sleep 1;    # let the session expire
+        $x = get("/recover_session");
+    }
 }
 
 __PACKAGE__;
@@ -145,14 +177,14 @@ engines.
 
 =head1 SYNOPSIS
 
-       #!/usr/bin/perl
+    #!/usr/bin/perl
 
-       use Catalyst::Plugin::Session::Test::Store (
-               backend => "FastMmap",
-               config => {
-                       storage => "/tmp/foo",
-               },
-       );
+    use Catalyst::Plugin::Session::Test::Store (
+        backend => "FastMmap",
+        config => {
+            storage => "/tmp/foo",
+        },
+    );
 
 =head1 DESCRIPTION