Bump versions for dev release. Clean up other mentions of config->{session}
[catagits/Catalyst-Plugin-Session.git] / lib / Catalyst / Plugin / Session / Test / Store.pm
CommitLineData
fff59d60 1#!/usr/bin/perl
69da5f6e 2
3package Catalyst::Plugin::Session::Test::Store;
4
5use strict;
6use warnings;
7
ec299c02 8use utf8;
9
8f236527 10use Test::More;
69da5f6e 11use File::Temp;
12use File::Spec;
13
14use Catalyst ();
15
16sub import {
45c0711b 17 shift;
18 my %args = @_;
69da5f6e 19
8f236527 20 plan tests => 19 + ($args{extra_tests} || 0);
21
45c0711b 22 my $backend = $args{backend};
23 my $cfg = $args{config};
69da5f6e 24
45c0711b 25 my $p = "Session::Store::$backend";
26 use_ok( my $m = "Catalyst::Plugin::$p" );
69da5f6e 27
45c0711b 28 isa_ok( bless( {}, $m ), "Catalyst::Plugin::Session::Store" );
69da5f6e 29
ec299c02 30 {
66017cbc 31 package # Hide from PAUSE
32 Catalyst::Plugin::SessionStateTest;
ec299c02 33 use base qw/Catalyst::Plugin::Session::State/;
34
35 no strict 'refs';
36
37 sub get_session_id {
38 my $c = shift;
39 ${ ref($c) . "::session_id" };
40 }
41
42 sub set_session_id {
43 my ( $c, $sid ) = @_;
44 ${ ref($c) . "::session_id" } = $sid;
45 }
46
47 sub delete_session_id {
48 my $c = shift;
49 undef ${ ref($c) . "::session_id" };
50 }
51 }
69da5f6e 52
45c0711b 53 {
69da5f6e 54
66017cbc 55 package # Hide from PAUSE
56 SessionStoreTest;
ec299c02 57 use Catalyst qw/Session SessionStateTest/;
45c0711b 58 push our (@ISA), $m;
69da5f6e 59
ec299c02 60 use strict;
61 use warnings;
69da5f6e 62
45c0711b 63 use Test::More;
69da5f6e 64
45c0711b 65 sub create_session : Global {
66 my ( $self, $c ) = @_;
ec299c02 67 ok( !$c->session_is_valid, "no session id yet" );
68 ok( $c->session, "session created" );
69 ok( $c->session_is_valid, "with a session id" );
69da5f6e 70
45c0711b 71 $c->session->{magic} = "møøse";
72 }
69da5f6e 73
45c0711b 74 sub recover_session : Global {
75 my ( $self, $c ) = @_;
ec299c02 76 ok( $c->session_is_valid, "session id exists" );
77 is( $c->sessionid, our $session_id,
45c0711b 78 "and is the one we saved in the last action" );
79 ok( $c->session, "a session exists" );
80 is( $c->session->{magic},
81 "møøse",
82 "and it contains what we put in on the last attempt" );
83 $c->delete_session("user logout");
45c0711b 84 }
69da5f6e 85
45c0711b 86 sub after_session : Global {
87 my ( $self, $c ) = @_;
ec299c02 88 ok( !$c->session_is_valid, "no session id" );
45c0711b 89 ok( !$c->session->{magic}, "session data not restored" );
90 ok( !$c->session_delete_reason, "no reason for deletion" );
91 }
69da5f6e 92
9a50355f 93 @{ __PACKAGE__->config->{'Plugin::Session'} }{ keys %$cfg } = values %$cfg;
69da5f6e 94
3253438d 95 { __PACKAGE__->setup; }; # INSANE HACK
45c0711b 96 }
69da5f6e 97
45c0711b 98 {
69da5f6e 99
66017cbc 100 package # Hide from PAUSE
101 SessionStoreTest2;
ec299c02 102 use Catalyst qw/Session SessionStateTest/;
45c0711b 103 push our (@ISA), $m;
69da5f6e 104
45c0711b 105 our $VERSION = "123";
69da5f6e 106
45c0711b 107 use Test::More;
69da5f6e 108
45c0711b 109 sub create_session : Global {
110 my ( $self, $c ) = @_;
69da5f6e 111
45c0711b 112 $c->session->{magic} = "møøse";
45c0711b 113 }
69da5f6e 114
45c0711b 115 sub recover_session : Global {
116 my ( $self, $c ) = @_;
69da5f6e 117
ec299c02 118 ok( !$c->session_is_valid, "session is gone" );
69da5f6e 119
45c0711b 120 is(
121 $c->session_delete_reason,
122 "session expired",
123 "reason is that the session expired"
124 );
69da5f6e 125
45c0711b 126 ok( !$c->session->{magic}, "no saved data" );
127 }
69da5f6e 128
9a50355f 129 __PACKAGE__->config->{'Plugin::Session'}{expires} = 0;
c80e9f04 130
9a50355f 131 @{ __PACKAGE__->config->{'Plugin::Session'} }{ keys %$cfg } = values %$cfg;
c80e9f04 132
3253438d 133 { __PACKAGE__->setup; }; # INSANE HACK
45c0711b 134 }
69da5f6e 135
45c0711b 136 use Test::More;
69da5f6e 137
45c0711b 138 can_ok( $m, "get_session_data" );
139 can_ok( $m, "store_session_data" );
140 can_ok( $m, "delete_session_data" );
141 can_ok( $m, "delete_expired_sessions" );
69da5f6e 142
45c0711b 143 {
144
66017cbc 145 package # Hide from PAUSE
146 t1;
45c0711b 147 use Catalyst::Test "SessionStoreTest";
148
124488fb 149 # idiotic void context warning workaround
150
151 my $x = get("/create_session");
152 $x = get("/recover_session");
153 $x = get("/after_session");
45c0711b 154 }
155
156 {
157
66017cbc 158 package # Hide fram PAUSE
159 t2;
45c0711b 160 use Catalyst::Test "SessionStoreTest2";
161
124488fb 162 my $x = get("/create_session");
45c0711b 163 sleep 1; # let the session expire
124488fb 164 $x = get("/recover_session");
45c0711b 165 }
69da5f6e 166}
167
168__PACKAGE__;
169
170__END__
171
172=pod
173
174=head1 NAME
175
176Catalyst::Plugin::Session::Test::Store - Reusable sanity for session storage
177engines.
178
179=head1 SYNOPSIS
180
45c0711b 181 #!/usr/bin/perl
69da5f6e 182
45c0711b 183 use Catalyst::Plugin::Session::Test::Store (
184 backend => "FastMmap",
185 config => {
186 storage => "/tmp/foo",
187 },
188 );
69da5f6e 189
190=head1 DESCRIPTION
191
192=cut
193
194