session.t 1.5 KB

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