Checking in changes prior to tagging of version 0.26. Changelog diff is:
[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::Exception;
8 use Test::Deep;
9
10 my $m;
11 BEGIN { use_ok( $m = "Catalyst::Plugin::Session" ) }
12
13 my $c_meta = Class::MOP::Class->create_anon_class(
14     superclasses => [ $m, 'Moose::Object', ],
15 );
16 my $c = $c_meta->name->new;
17
18 my $flash = {};
19 $c_meta->add_method(
20     get_session_data => sub {
21         my ( $c, $key ) = @_;
22         return $key =~ /expire/ ? time() + 1000 : $flash;
23     },
24 );
25 $c->meta->add_method("debug" => sub { 0 });
26 $c->meta->add_method("store_session_data" => sub { $flash = $_[2] });
27 $c->meta->add_method("delete_session_data" => sub { $flash = {} });
28 $c->meta->add_method( _sessionid => sub { "deadbeef" });
29 my $config = { expires => 1000 };
30 $c->meta->add_method( config     => sub { { session => $config } });
31 my $stash = {};
32 $c->meta->add_method( stash      => sub { $stash } );
33
34 is_deeply( $c->session, {}, "nothing in session" );
35
36 is_deeply( $c->flash, {}, "nothing in flash" );
37
38 $c->flash->{foo} = "moose";
39
40 $c->finalize_body;
41
42 is_deeply( $c->flash, { foo => "moose" }, "one key in flash" );
43
44 cmp_deeply( $c->session, { __updated => re('^\d+$'), __flash => $c->flash }, "session has __flash with flash data" );
45
46 $c->flash(bar => "gorch");
47
48 is_deeply( $c->flash, { foo => "moose", bar => "gorch" }, "two keys in flash" );
49
50 cmp_deeply( $c->session, { __updated => re('^\d+$'), __flash => $c->flash }, "session still has __flash with flash data" );
51
52 $c->finalize_body;
53
54 is_deeply( $c->flash, { bar => "gorch" }, "one key in flash" );
55
56 $c->finalize_body;
57
58 $c->flash->{test} = 'clear_flash';
59
60 $c->finalize_body;
61
62 $c->clear_flash();
63
64 is_deeply( $c->flash, {}, "nothing in flash after clear_flash" );
65
66 $c->finalize_body;
67
68 is_deeply( $c->flash, {}, "nothing in flash after finalize after clear_flash" );
69
70 cmp_deeply( $c->session, { __updated => re('^\d+$'), }, "session has empty __flash after clear_flash + finalize" );
71
72 $c->flash->{bar} = "gorch";
73
74 $config->{flash_to_stash} = 1;
75
76 $c->finalize_body;
77 $c->prepare_action;
78
79 is_deeply( $c->stash, { bar => "gorch" }, "flash copied to stash" );