session.t 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!perl -T
  2. # vi: set tabstop=4 expandtab shiftwidth=4:
  3. use Test::More tests => 11;
  4. use strict;
  5. use warnings;
  6. my $namespace = 'TestCMS';
  7. my $cache;
  8. my $def_root;
  9. BEGIN {
  10. use_ok('Cache::FileCache') || print "Bail out!\n";
  11. use_ok('CMS::Session') || print "Bail out!\n";
  12. $cache = new Cache::FileCache({
  13. namespace => $namespace,
  14. }
  15. );
  16. $def_root = $cache->get_cache_root();
  17. }
  18. END {
  19. $cache->clear() if $cache;
  20. }
  21. ok(!new CMS::Session('no_valid_id', undef, $namespace), 'Invalid session.');
  22. my $session = new CMS::Session(undef, undef, $namespace);
  23. ok(ref($session) eq 'CMS::Session', 'Valid session.');
  24. my $id = $session->id();
  25. ok($id, 'Got session id.');
  26. $session->set('test', '1,2,3 Test');
  27. undef $session;
  28. $session = new CMS::Session($id, $def_root, $namespace);
  29. ok(ref($session) eq 'CMS::Session', 'Reattach session.');
  30. ok($session->get('test') eq '1,2,3 Test', 'Retrieve value.');
  31. my $data = { 'test' => 'X' };
  32. $session->data($data);
  33. undef $session;
  34. $session = new CMS::Session($id, $def_root, $namespace);
  35. ok($session && $session->get('test') eq 'X', 'Set hashref.');
  36. eval {
  37. $session->data(\'Invalid Argument');
  38. };
  39. ok($@, 'Die by passing wrong argument to data()');
  40. like($@, qr/^Require a hash reference./, '... and throws the right exception.');
  41. $session->remove();
  42. # Delete twice to cover the condition of a valid session id
  43. $session->remove();
  44. undef $session;
  45. $session = new CMS::Session($id, $def_root, $namespace);
  46. ok(!defined($session), 'Session removed.');