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