Fix VERSIOON, eugh
[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
24c10398 60 our $VERSION = "123"; # Do not remove
61
ec299c02 62 use strict;
63 use warnings;
69da5f6e 64
45c0711b 65 use Test::More;
69da5f6e 66
45c0711b 67 sub create_session : Global {
68 my ( $self, $c ) = @_;
ec299c02 69 ok( !$c->session_is_valid, "no session id yet" );
70 ok( $c->session, "session created" );
71 ok( $c->session_is_valid, "with a session id" );
69da5f6e 72
45c0711b 73 $c->session->{magic} = "møøse";
74 }
69da5f6e 75
45c0711b 76 sub recover_session : Global {
77 my ( $self, $c ) = @_;
ec299c02 78 ok( $c->session_is_valid, "session id exists" );
79 is( $c->sessionid, our $session_id,
45c0711b 80 "and is the one we saved in the last action" );
81 ok( $c->session, "a session exists" );
82 is( $c->session->{magic},
83 "møøse",
84 "and it contains what we put in on the last attempt" );
85 $c->delete_session("user logout");
45c0711b 86 }
69da5f6e 87
45c0711b 88 sub after_session : Global {
89 my ( $self, $c ) = @_;
ec299c02 90 ok( !$c->session_is_valid, "no session id" );
45c0711b 91 ok( !$c->session->{magic}, "session data not restored" );
92 ok( !$c->session_delete_reason, "no reason for deletion" );
93 }
69da5f6e 94
9a50355f 95 @{ __PACKAGE__->config->{'Plugin::Session'} }{ keys %$cfg } = values %$cfg;
69da5f6e 96
7febc064 97 { __PACKAGE__->setup; }; # Extra block here is an INSANE HACK to get inlined constructor
98 # (i.e. to make B::Hooks::EndOfScope fire)
45c0711b 99 }
69da5f6e 100
45c0711b 101 {
69da5f6e 102
66017cbc 103 package # Hide from PAUSE
104 SessionStoreTest2;
ec299c02 105 use Catalyst qw/Session SessionStateTest/;
45c0711b 106 push our (@ISA), $m;
69da5f6e 107
24c10398 108 our $VERSION = "123";
109
45c0711b 110 use Test::More;
69da5f6e 111
45c0711b 112 sub create_session : Global {
113 my ( $self, $c ) = @_;
69da5f6e 114
45c0711b 115 $c->session->{magic} = "møøse";
45c0711b 116 }
69da5f6e 117
45c0711b 118 sub recover_session : Global {
119 my ( $self, $c ) = @_;
69da5f6e 120
ec299c02 121 ok( !$c->session_is_valid, "session is gone" );
69da5f6e 122
45c0711b 123 is(
124 $c->session_delete_reason,
125 "session expired",
126 "reason is that the session expired"
127 );
69da5f6e 128
45c0711b 129 ok( !$c->session->{magic}, "no saved data" );
130 }
69da5f6e 131
9a50355f 132 __PACKAGE__->config->{'Plugin::Session'}{expires} = 0;
c80e9f04 133
9a50355f 134 @{ __PACKAGE__->config->{'Plugin::Session'} }{ keys %$cfg } = values %$cfg;
c80e9f04 135
7febc064 136 { __PACKAGE__->setup; }; # INSANE HACK (the block - as above)
45c0711b 137 }
69da5f6e 138
45c0711b 139 use Test::More;
69da5f6e 140
45c0711b 141 can_ok( $m, "get_session_data" );
142 can_ok( $m, "store_session_data" );
143 can_ok( $m, "delete_session_data" );
144 can_ok( $m, "delete_expired_sessions" );
69da5f6e 145
45c0711b 146 {
147
66017cbc 148 package # Hide from PAUSE
149 t1;
45c0711b 150 use Catalyst::Test "SessionStoreTest";
151
124488fb 152 # idiotic void context warning workaround
153
154 my $x = get("/create_session");
155 $x = get("/recover_session");
156 $x = get("/after_session");
45c0711b 157 }
158
159 {
160
66017cbc 161 package # Hide fram PAUSE
162 t2;
45c0711b 163 use Catalyst::Test "SessionStoreTest2";
164
124488fb 165 my $x = get("/create_session");
45c0711b 166 sleep 1; # let the session expire
124488fb 167 $x = get("/recover_session");
45c0711b 168 }
69da5f6e 169}
170
171__PACKAGE__;
172
173__END__
174
175=pod
176
177=head1 NAME
178
179Catalyst::Plugin::Session::Test::Store - Reusable sanity for session storage
180engines.
181
182=head1 SYNOPSIS
183
45c0711b 184 #!/usr/bin/perl
69da5f6e 185
45c0711b 186 use Catalyst::Plugin::Session::Test::Store (
187 backend => "FastMmap",
188 config => {
189 storage => "/tmp/foo",
190 },
191 );
69da5f6e 192
193=head1 DESCRIPTION
194
195=cut
196
197