Apache-SWIT

 view release on metacpan or  search on metacpan

t/100_session.t  view on Meta::CPAN

use strict;
use warnings FATAL => 'all';

use Test::More tests => 25;
use CGI::Cookie;
use Package::Alias 'Apache2::Cookie' => 'CGI::Cookie'
	, 'Apache2::Const::OK' => sub { 200; };

use_ok('Apache::SWIT::Session');

use Data::Dumper;
use Storable qw(thaw);

package Obj;

sub retrieve { return bless({}, shift()); }
sub id { return 17; }
sub value { return "hi"; }

package MySession;
use base 'Apache::SWIT::Session';

__PACKAGE__->add_var('hello');
__PACKAGE__->add_var('bye', depends_on => [ 'hello' ]);
__PACKAGE__->add_var('bye_bye', depends_on => [ 'bye' ]);
__PACKAGE__->add_var('infdef', depends_on => [ 'bye' ], 
		inflate => sub { shift() . '_foooooo'; },
		deflate => sub { $_[0] =~ s/_foooooo//; return $_[0]; });
__PACKAGE__->add_class_dbi_var('obj', 'Obj');

package main;

HTML::Tested::Seal->instance('aaa');

my $s = MySession->new(_stash => { hello => 'world' });
is($s->get_hello, 'world');

is($s->delete_hello, 'world');
is_deeply($s->{_stash}, {});

$s->set_hello('life');
is_deeply($s->{_stash}, { hello => 'life' });

$s->set_bye('world');
is_deeply($s->{_stash}, { hello => 'life', bye => 'world' });
$s->set_hello('planet');
is_deeply($s->{_stash}, { hello => 'planet' });

$s->set_bye('world');
$s->set_bye_bye('earth');
is_deeply($s->{_stash}, { hello => 'planet', bye => 'world', 
				bye_bye => 'earth', });
is($s->delete_hello, 'planet');
is_deeply($s->{_stash}, {});

$s->set_hello('planet');
$s->set_bye('world');
$s->set_infdef('defme_foooooo');
$s->set_obj(Obj->retrieve);
is_deeply($s->{_stash}, { hello => 'planet', bye => 'world', 
		infdef => 'defme_foooooo', obj => {} });
is($s->get_infdef, 'defme_foooooo');

$s->write_stash;

isnt($s->session_value, undef);
unlike($s->session_value, qr/[^\w]/);
eval { thaw($s->session_value); };
isnt($@, '');

my $s2 = MySession->new(session_value => $s->session_value);



( run in 1.897 second using v1.01-cache-2.11-cpan-39bf76dae61 )