improved Store::File based on stuff in Store::CHI, ... added Store::Null and tests
Stevan Little [Sun, 13 Dec 2009 01:11:42 +0000 (20:11 -0500)]
lib/Plack/Session/Store/File.pm
lib/Plack/Session/Store/Null.pm [new file with mode: 0644]
t/004_basic_file_w_customs.t
t/006_basic_w_null_store.t [new file with mode: 0644]

index 7741c74..c2926da 100644 (file)
@@ -23,12 +23,14 @@ sub new {
     $params{'serializer'}   ||= sub { Storable::nstore( @_ ) };
     $params{'deserializer'} ||= sub { Storable::retrieve( @_ ) };
 
-    $class->SUPER::new( %params );
+    bless { %params } => $class;
 }
 
 sub fetch {
     my ($self, $session_id, $key) = @_;
-    $self->_deserialize( $session_id )->{ $key };
+    my $store = $self->_deserialize( $session_id );
+    return unless exists $store->{ $key };
+    return $store->{ $key };
 }
 
 sub store {
@@ -41,6 +43,7 @@ sub store {
 sub delete {
     my ($self, $session_id, $key) = @_;
     my $store = $self->_deserialize( $session_id );
+    return unless exists $store->{ $key };
     delete $store->{ $key };
     $self->_serialize( $session_id, $store );
 }
diff --git a/lib/Plack/Session/Store/Null.pm b/lib/Plack/Session/Store/Null.pm
new file mode 100644 (file)
index 0000000..3ab6ccb
--- /dev/null
@@ -0,0 +1,48 @@
+package Plack::Session::Store::Null;
+use strict;
+use warnings;
+
+sub new     { bless {} => shift }
+sub fetch   {}
+sub store   {}
+sub delete  {}
+sub cleanup {}
+sub persist {}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Plack::Session::Store::Null - Null store
+
+=head1 DESCRIPTION
+
+Sometimes you don't want to store anything in your sessions, but
+L<Plack::Session> requires a C<store> instance, so you can use this
+one and all methods will return null.
+
+=head1 BUGS
+
+All complex software has bugs lurking in it, and this module is no
+exception. If you find a bug please either email me, or add the bug
+to cpan-RT.
+
+=head1 AUTHOR
+
+Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2009 Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
index 0683a79..de79650 100644 (file)
@@ -3,14 +3,10 @@
 use strict;
 use warnings;
 use File::Spec;
+use Test::Requires 'YAML';
 
 use Test::More;
 
-BEGIN {
-    eval "use YAML";
-    plan skip_all => "This test requires YAML" if $@;
-}
-
 use Plack::Request;
 use Plack::Session;
 use Plack::Session::State::Cookie;
diff --git a/t/006_basic_w_null_store.t b/t/006_basic_w_null_store.t
new file mode 100644 (file)
index 0000000..2efba7a
--- /dev/null
@@ -0,0 +1,66 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Exception;
+
+use Plack::Request;
+use Plack::Session;
+use Plack::Session::State;
+use Plack::Session::Store::Null;
+
+my $storage         = Plack::Session::Store::Null->new;
+my $state           = Plack::Session::State->new;
+my $request_creator = sub {
+    open my $in, '<', \do { my $d };
+    my $env = {
+        'psgi.version'    => [ 1, 0 ],
+        'psgi.input'      => $in,
+        'psgi.errors'     => *STDERR,
+        'psgi.url_scheme' => 'http',
+        SERVER_PORT       => 80,
+        REQUEST_METHOD    => 'GET',
+    };
+    my $r = Plack::Request->new( $env );
+    $r->parameters( @_ );
+    $r;
+};
+
+{
+    my $r = $request_creator->();
+
+    my $s = Plack::Session->new(
+        state   => $state,
+        store   => $storage,
+        request => $r,
+    );
+
+    ok($s->id, '... got a session id');
+
+    ok(!$s->get('foo'), '... no value stored in foo for session');
+
+    lives_ok {
+        $s->set( foo => 'bar' );
+    } '... set the value successfully in session';
+
+    ok(!$s->get('foo'), '... still no value stored in foo for session (null store)');
+
+    lives_ok {
+        $s->remove('foo');
+    } '... removed the value successfully in session';
+
+    lives_ok {
+        $s->expire;
+    } '... expire session successfully';
+
+    my $resp = $r->new_response;
+
+    lives_ok {
+        $s->finalize( $resp );
+    } '... finalized session successfully';
+}
+
+
+done_testing;