copyright boilerplate
[catagits/Catalyst-Plugin-Cache.git] / t / basic.t
CommitLineData
c28ee69c 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More 'no_plan';
7use Test::Exception;
8
9use ok "Catalyst::Plugin::Cache";
10
33002c69 11use Catalyst::Plugin::Cache::Backend::Memory;
12
c28ee69c 13{
14 package MockApp;
15 use base qw/Catalyst::Plugin::Cache/;
23b2d59b 16
17 sub registered_plugins {}
c28ee69c 18
2e4bde89 19 my %config;
20 sub config { \%config };
c28ee69c 21}
22
23MockApp->setup;
24my $c = bless {}, "MockApp";
25
26can_ok( $c, "register_cache_backend" );
27can_ok( $c, "unregister_cache_backend" );
28
33002c69 29MockApp->register_cache_backend( default => Catalyst::Plugin::Cache::Backend::Memory->new );
30MockApp->register_cache_backend( moose => Catalyst::Plugin::Cache::Backend::Memory->new );
c28ee69c 31
32can_ok( $c, "cache" );
33
34ok( $c->cache, "->cache returns a value" );
35
36can_ok( $c->cache, "get" ); #, "rv from cache" );
37can_ok( $c->cache("default"), "get" ); #, "default backend" );
38can_ok( $c->cache("moose"), "get" ); #, "moose backend" );
39
40ok( !$c->cache("lalalala"), "no lalala backend");
41
42MockApp->unregister_cache_backend( "moose" );
43
44ok( !$c->cache("moose"), "moose backend unregistered");
45
46
47dies_ok {
48 MockApp->register_cache_backend( ding => undef );
49} "can't register invalid backend";
50
51dies_ok {
52 MockApp->register_cache_backend( ding => bless {}, "SomeClass" );
53} "can't register invalid backend";
54
55
56
57can_ok( $c, "default_cache_backend" );
c28ee69c 58
59can_ok( $c, "choose_cache_backend_wrapper" );
60can_ok( $c, "choose_cache_backend" );
61
62can_ok( $c, "cache_set" );
63can_ok( $c, "cache_get" );
aed484da 64can_ok( $c, "cache_remove" );
c28ee69c 65
66$c->cache_set( foo => "bar" );
67is( $c->cache_get("foo"), "bar", "set" );
68
aed484da 69$c->cache_remove( "foo" );
70is( $c->cache_get("foo"), undef, "remove" );
c28ee69c 71
33002c69 72MockApp->register_cache_backend( elk => Catalyst::Plugin::Cache::Backend::Memory->new );
c28ee69c 73
74is( $c->choose_cache_backend_wrapper( key => "foo" ), $c->default_cache_backend, "choose default" );
75is( $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" );
79is( $c->cache_get("foo"), undef, "set to custom backend (get from non custom)" );
80is( $c->cache_get("foo", backend => "elk"), "gorch", "set to custom backend (get from custom)" );
81
2e4bde89 82my $cache_elk = $c->cache( backend => "elk" );
83my $cache_norm = $c->cache();
84
85is( $cache_norm->get("foo"), undef, "default curried cache has no foo");
86is( $cache_elk->get("foo"), "gorch", "curried custom backend has foo" );