f58222c3d3d8e9dd34780d5240f2d56ad18a96dd
[catagits/Catalyst-Plugin-Session.git] / t / 03_flash.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 12;
7 use Test::MockObject::Extends;
8 use Test::Exception;
9 use Test::Deep;
10
11 my $m;
12 BEGIN { use_ok( $m = "Catalyst::Plugin::Session" ) }
13
14 my $c = Test::MockObject::Extends->new($m);
15
16 my $flash = {};
17 $c->mock(
18     get_session_data => sub {
19         my ( $c, $key ) = @_;
20         return $key =~ /expire/ ? time() + 1000 : $flash;
21     },
22 );
23 $c->mock("debug" => sub { 0 });
24 $c->mock("store_session_data" => sub { $flash = $_[2] });
25 $c->mock("delete_session_data" => sub { $flash = {} });
26 $c->set_always( _sessionid => "deadbeef" );
27 $c->set_always( config     => { session => { expires => 1000 } } );
28 $c->set_always( stash      => {} );
29
30 is_deeply( $c->session, {}, "nothing in session" );
31
32 is_deeply( $c->flash, {}, "nothing in flash" );
33
34 $c->flash->{foo} = "moose";
35
36 $c->finalize_body;
37
38 is_deeply( $c->flash, { foo => "moose" }, "one key in flash" );
39
40 cmp_deeply( $c->session, { __updated => re('^\d+$'), __flash => $c->flash }, "session has __flash with flash data" );
41
42 $c->flash(bar => "gorch");
43
44 is_deeply( $c->flash, { foo => "moose", bar => "gorch" }, "two keys in flash" );
45
46 cmp_deeply( $c->session, { __updated => re('^\d+$'), __flash => $c->flash }, "session still has __flash with flash data" );
47
48 $c->finalize_body;
49
50 is_deeply( $c->flash, { bar => "gorch" }, "one key in flash" );
51
52 $c->finalize_body;
53
54 $c->flash->{test} = 'clear_flash';
55
56 $c->finalize_body;
57
58 $c->clear_flash();
59
60 is_deeply( $c->flash, {}, "nothing in flash after clear_flash" );
61
62 $c->finalize_body;
63
64 is_deeply( $c->flash, {}, "nothing in flash after finalize after clear_flash" );
65
66 cmp_deeply( $c->session, { __updated => re('^\d+$'), }, "session has empty __flash after clear_flash + finalize" );
67
68 $c->flash->{bar} = "gorch";
69
70 $c->config->{session}{flash_to_stash} = 1;
71
72 $c->finalize_body;
73 $c->prepare_action;
74
75 is_deeply( $c->stash, { bar => "gorch" }, "flash copied to stash" );