Checking in changes prior to tagging of version 0.22. Changelog diff is:
[catagits/Catalyst-Plugin-Session.git] / lib / Catalyst / Plugin / Session / Test / Store.pm
1 #!/usr/bin/perl
2
3 package Catalyst::Plugin::Session::Test::Store;
4
5 use strict;
6 use warnings;
7
8 use utf8;
9
10 use Test::More;
11 use File::Temp;
12 use File::Spec;
13
14 use Catalyst ();
15
16 sub import {
17     shift;
18     my %args = @_;
19
20     plan tests => 19 + ($args{extra_tests} || 0);
21
22     my $backend = $args{backend};
23     my $cfg     = $args{config};
24
25     my $p = "Session::Store::$backend";
26     use_ok( my $m = "Catalyst::Plugin::$p" );
27
28     isa_ok( bless( {}, $m ), "Catalyst::Plugin::Session::Store" );
29
30     {
31         package # Hide from PAUSE
32             Catalyst::Plugin::SessionStateTest;
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     }
52
53     {
54
55         package # Hide from PAUSE
56             SessionStoreTest;
57         use Catalyst qw/Session SessionStateTest/;
58         push our (@ISA), $m;
59
60         use strict;
61         use warnings;
62
63         use Test::More;
64
65         sub create_session : Global {
66             my ( $self, $c ) = @_;
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" );
70
71             $c->session->{magic} = "møøse";
72         }
73
74         sub recover_session : Global {
75             my ( $self, $c ) = @_;
76             ok( $c->session_is_valid, "session id exists" );
77             is( $c->sessionid, our $session_id,
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");
84         }
85
86         sub after_session : Global {
87             my ( $self, $c ) = @_;
88             ok( !$c->session_is_valid,      "no session id" );
89             ok( !$c->session->{magic},      "session data not restored" );
90             ok( !$c->session_delete_reason, "no reason for deletion" );
91         }
92
93         @{ __PACKAGE__->config->{session} }{ keys %$cfg } = values %$cfg;
94
95         { __PACKAGE__->setup; }; # INSANE HACK 
96     }
97
98     {
99
100         package # Hide from PAUSE
101             SessionStoreTest2;
102         use Catalyst qw/Session SessionStateTest/;
103         push our (@ISA), $m;
104
105         our $VERSION = "123";
106
107         use Test::More;
108
109         sub create_session : Global {
110             my ( $self, $c ) = @_;
111
112             $c->session->{magic} = "møøse";
113         }
114
115         sub recover_session : Global {
116             my ( $self, $c ) = @_;
117
118             ok( !$c->session_is_valid, "session is gone" );
119
120             is(
121                 $c->session_delete_reason,
122                 "session expired",
123                 "reason is that the session expired"
124             );
125
126             ok( !$c->session->{magic}, "no saved data" );
127         }
128
129         __PACKAGE__->config->{session}{expires} = 0;
130
131         @{ __PACKAGE__->config->{session} }{ keys %$cfg } = values %$cfg;
132
133         { __PACKAGE__->setup; }; # INSANE HACK
134     }
135
136     use Test::More;
137
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" );
142
143     {
144
145         package # Hide from PAUSE
146             t1;
147         use Catalyst::Test "SessionStoreTest";
148
149         # idiotic void context warning workaround
150         
151         my $x = get("/create_session");
152         $x = get("/recover_session");
153         $x = get("/after_session");
154     }
155
156     {
157
158         package # Hide fram PAUSE
159             t2;
160         use Catalyst::Test "SessionStoreTest2";
161
162         my $x = get("/create_session");
163         sleep 1;    # let the session expire
164         $x = get("/recover_session");
165     }
166 }
167
168 __PACKAGE__;
169
170 __END__
171
172 =pod
173
174 =head1 NAME
175
176 Catalyst::Plugin::Session::Test::Store - Reusable sanity for session storage
177 engines.
178
179 =head1 SYNOPSIS
180
181     #!/usr/bin/perl
182
183     use Catalyst::Plugin::Session::Test::Store (
184         backend => "FastMmap",
185         config => {
186             storage => "/tmp/foo",
187         },
188     );
189
190 =head1 DESCRIPTION
191
192 =cut
193
194