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