Port to new session config key.
[catagits/Catalyst-Plugin-Session-State-Stash.git] / t / 04-basic.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 10;
7 use Test::MockObject;
8 use Test::MockObject::Extends;
9
10 # Get a stash
11 my $m;
12 BEGIN { use_ok( $m = "Catalyst::Plugin::Session::State::Stash" ) }
13
14 # Mock a catalyst context
15 {
16     package MockCtx;
17     use base qw/
18         Catalyst::Plugin::Session
19         Catalyst::Plugin::Session::State::Stash
20     /;
21 }
22
23 my $cxt = Test::MockObject::Extends->new("MockCtx");
24
25 $cxt->set_always( config   => {} );
26 $cxt->set_always( session  => {} );
27 $cxt->set_always( stash    => {} );
28 $cxt->set_false("debug");
29
30 my $sessionid;
31 $cxt->mock( sessionid => sub { shift; $sessionid = shift if @_; $sessionid } );
32
33 can_ok( $m, "setup_session" );
34
35 $cxt->setup_session;
36
37 is( $cxt->config->{'Plugin::Session'}{stash_key},
38     '_session', "default cookie name is set" );
39
40 can_ok( $m, "get_session_id" );
41
42 ok( !$cxt->get_session_id, "no session id yet");
43
44 $cxt->set_always( stash => { '_session' => {id => 1}, 'session_id' => {id => 2}, 'other_thing' => { id => 3 } } );
45
46 is( $cxt->get_session_id, "1", "Pull newfound session id" );
47
48 $cxt->config->{'Plugin::Session'}{stash_key} = "session_id";
49
50 is( $cxt->get_session_id, "2", "Pull session id from second key" );
51
52 can_ok( $m, "set_session_id" );
53
54 # Check forwards config compatibility..
55 $cxt->config->{'Plugin::Session'} = {};
56 $cxt->setup_session;
57
58 is( $cxt->config->{'Plugin::Session'}{stash_key},
59     '_session', "default cookie name is set when new stash key used" );
60
61 $cxt->config->{'Plugin::Session'}{stash_key} = "other_thing";
62
63 is( $cxt->get_session_id, "3", "Pull session id from key in new config" );