Convert to Moose
[catagits/Catalyst-Plugin-Session-State-Stash.git] / t / 04-basic.t
CommitLineData
643399c7 1#!/usr/bin/perl
642b19cf 2
3use strict;
4use warnings;
5
effd6244 6use Test::More tests => 10;
642b19cf 7use Test::MockObject;
8use Test::MockObject::Extends;
9
10# Get a stash
11my $m;
12BEGIN { use_ok( $m = "Catalyst::Plugin::Session::State::Stash" ) }
13
643399c7 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
23my $cxt = Test::MockObject::Extends->new("MockCtx");
642b19cf 24
25$cxt->set_always( config => {} );
26$cxt->set_always( session => {} );
27$cxt->set_always( stash => {} );
28$cxt->set_false("debug");
29
30my $sessionid;
31$cxt->mock( sessionid => sub { shift; $sessionid = shift if @_; $sessionid } );
32
33can_ok( $m, "setup_session" );
34
35$cxt->setup_session;
36
643399c7 37is( $cxt->config->{'Plugin::Session'}{stash_key},
642b19cf 38 '_session', "default cookie name is set" );
39
40can_ok( $m, "get_session_id" );
41
42ok( !$cxt->get_session_id, "no session id yet");
43
effd6244 44$cxt->set_always( stash => { '_session' => {id => 1}, 'session_id' => {id => 2}, 'other_thing' => { id => 3 } } );
642b19cf 45
46is( $cxt->get_session_id, "1", "Pull newfound session id" );
47
643399c7 48$cxt->config->{'Plugin::Session'}{stash_key} = "session_id";
642b19cf 49
50is( $cxt->get_session_id, "2", "Pull session id from second key" );
51
effd6244 52can_ok( $m, "set_session_id" );
53
54# Check forwards config compatibility..
55$cxt->config->{'Plugin::Session'} = {};
56$cxt->setup_session;
57
58is( $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
63is( $cxt->get_session_id, "3", "Pull session id from key in new config" );