Catalyst::Plugin::Cache draft
[catagits/Catalyst-Plugin-Cache.git] / t / basic.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More 'no_plan';
7 use Test::Exception;
8
9 use ok "Catalyst::Plugin::Cache";
10
11 {
12     package MockApp;
13     use base qw/Catalyst::Plugin::Cache/;
14
15     package MemoryCache;
16     use Storable qw/freeze thaw/;
17     
18     sub new { bless {}, shift }
19     sub get { ${thaw($_[0]{$_[1]}) || return} };
20     sub set { $_[0]{$_[1]} = freeze(\$_[2]) };
21     sub delete { delete $_[0]{$_[1]} };
22 }
23
24 MockApp->setup;
25 my $c = bless {}, "MockApp";
26
27 can_ok( $c, "register_cache_backend" );
28 can_ok( $c, "unregister_cache_backend" );
29
30 MockApp->register_cache_backend( default => MemoryCache->new );
31 MockApp->register_cache_backend( moose => MemoryCache->new );
32
33 can_ok( $c, "cache" );
34
35 ok( $c->cache, "->cache returns a value" );
36
37 can_ok( $c->cache, "get" ); #, "rv from cache" );
38 can_ok( $c->cache("default"), "get" ); #, "default backend" );
39 can_ok( $c->cache("moose"), "get" ); #, "moose backend" );
40
41 ok( !$c->cache("lalalala"), "no lalala backend");
42
43 MockApp->unregister_cache_backend( "moose" );
44
45 ok( !$c->cache("moose"), "moose backend unregistered");
46
47
48 dies_ok {
49     MockApp->register_cache_backend( ding => undef );
50 } "can't register invalid backend";
51
52 dies_ok {
53     MockApp->register_cache_backend( ding => bless {}, "SomeClass" );
54 } "can't register invalid backend";
55
56
57
58 can_ok( $c, "default_cache_backend" );
59 is( $c->default_cache_backend, $c->cache, "cache with no args retrurns default" );
60
61 can_ok( $c, "choose_cache_backend_wrapper" );
62 can_ok( $c, "choose_cache_backend" );
63
64 can_ok( $c, "cache_set" );
65 can_ok( $c, "cache_get" );
66 can_ok( $c, "cache_delete" );
67
68 $c->cache_set( foo => "bar" );
69 is( $c->cache_get("foo"), "bar", "set" );
70
71 $c->cache_delete( "foo" );
72 is( $c->cache_get("foo"), undef, "delete" );
73
74 MockApp->register_cache_backend( elk => MemoryCache->new );
75
76 is( $c->choose_cache_backend_wrapper( key => "foo" ), $c->default_cache_backend, "choose default" );
77 is( $c->choose_cache_backend_wrapper( key => "foo", backend => "elk" ), $c->get_cache_backend("elk"), "override choice" );
78
79
80 $c->cache_set( foo => "gorch", backend => "elk" );
81 is( $c->cache_get("foo"), undef, "set to custom backend (get from non custom)" );
82 is( $c->cache_get("foo", backend => "elk"), "gorch", "set to custom backend (get from custom)" );
83