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