ac7dab696795d508a0c2493e85af44a7c5fe22e2
[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     my %config;
16     sub config { \%config };
17
18     package MemoryCache;
19     use Storable qw/freeze thaw/;
20     
21     sub new { bless {}, shift }
22     sub get { ${thaw($_[0]{$_[1]}) || return} };
23     sub set { $_[0]{$_[1]} = freeze(\$_[2]) };
24     sub delete { delete $_[0]{$_[1]} };
25 }
26
27 MockApp->setup;
28 my $c = bless {}, "MockApp";
29
30 can_ok( $c, "register_cache_backend" );
31 can_ok( $c, "unregister_cache_backend" );
32
33 MockApp->register_cache_backend( default => MemoryCache->new );
34 MockApp->register_cache_backend( moose => MemoryCache->new );
35
36 can_ok( $c, "cache" );
37
38 ok( $c->cache, "->cache returns a value" );
39
40 can_ok( $c->cache, "get" ); #, "rv from cache" );
41 can_ok( $c->cache("default"), "get" ); #, "default backend" );
42 can_ok( $c->cache("moose"), "get" ); #, "moose backend" );
43
44 ok( !$c->cache("lalalala"), "no lalala backend");
45
46 MockApp->unregister_cache_backend( "moose" );
47
48 ok( !$c->cache("moose"), "moose backend unregistered");
49
50
51 dies_ok {
52     MockApp->register_cache_backend( ding => undef );
53 } "can't register invalid backend";
54
55 dies_ok {
56     MockApp->register_cache_backend( ding => bless {}, "SomeClass" );
57 } "can't register invalid backend";
58
59
60
61 can_ok( $c, "default_cache_backend" );
62
63 can_ok( $c, "choose_cache_backend_wrapper" );
64 can_ok( $c, "choose_cache_backend" );
65
66 can_ok( $c, "cache_set" );
67 can_ok( $c, "cache_get" );
68 can_ok( $c, "cache_delete" );
69
70 $c->cache_set( foo => "bar" );
71 is( $c->cache_get("foo"), "bar", "set" );
72
73 $c->cache_delete( "foo" );
74 is( $c->cache_get("foo"), undef, "delete" );
75
76 MockApp->register_cache_backend( elk => MemoryCache->new );
77
78 is( $c->choose_cache_backend_wrapper( key => "foo" ), $c->default_cache_backend, "choose default" );
79 is( $c->choose_cache_backend_wrapper( key => "foo", backend => "elk" ), $c->get_cache_backend("elk"), "override choice" );
80
81
82 $c->cache_set( foo => "gorch", backend => "elk" );
83 is( $c->cache_get("foo"), undef, "set to custom backend (get from non custom)" );
84 is( $c->cache_get("foo", backend => "elk"), "gorch", "set to custom backend (get from custom)" );
85
86 my $cache_elk = $c->cache( backend => "elk" );
87 my $cache_norm = $c->cache();
88
89 is( $cache_norm->get("foo"), undef, "default curried cache has no foo");
90 is( $cache_elk->get("foo"), "gorch", "curried custom backend has foo" );