Commit | Line | Data |
c28ee69c |
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 | |
33002c69 |
11 | use 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 | |
e9154310 |
19 | ### must register a backend class as of 0.06 |
20 | ### the previous code simply ignored a croak |
21 | ### this is still in line with the documentation. |
22 | my %config = ( |
23 | cache => { |
24 | backend => { |
25 | class => 'Catalyst::Plugin::Cache::Backend::Memory', |
26 | } |
27 | } |
28 | ); |
2e4bde89 |
29 | sub config { \%config }; |
c28ee69c |
30 | } |
31 | |
32 | MockApp->setup; |
33 | my $c = bless {}, "MockApp"; |
34 | |
35 | can_ok( $c, "register_cache_backend" ); |
36 | can_ok( $c, "unregister_cache_backend" ); |
37 | |
33002c69 |
38 | MockApp->register_cache_backend( default => Catalyst::Plugin::Cache::Backend::Memory->new ); |
39 | MockApp->register_cache_backend( moose => Catalyst::Plugin::Cache::Backend::Memory->new ); |
c28ee69c |
40 | |
41 | can_ok( $c, "cache" ); |
42 | |
43 | ok( $c->cache, "->cache returns a value" ); |
44 | |
45 | can_ok( $c->cache, "get" ); #, "rv from cache" ); |
46 | can_ok( $c->cache("default"), "get" ); #, "default backend" ); |
47 | can_ok( $c->cache("moose"), "get" ); #, "moose backend" ); |
48 | |
49 | ok( !$c->cache("lalalala"), "no lalala backend"); |
50 | |
51 | MockApp->unregister_cache_backend( "moose" ); |
52 | |
53 | ok( !$c->cache("moose"), "moose backend unregistered"); |
54 | |
55 | |
56 | dies_ok { |
57 | MockApp->register_cache_backend( ding => undef ); |
58 | } "can't register invalid backend"; |
59 | |
60 | dies_ok { |
61 | MockApp->register_cache_backend( ding => bless {}, "SomeClass" ); |
62 | } "can't register invalid backend"; |
63 | |
64 | |
65 | |
66 | can_ok( $c, "default_cache_backend" ); |
c28ee69c |
67 | |
68 | can_ok( $c, "choose_cache_backend_wrapper" ); |
69 | can_ok( $c, "choose_cache_backend" ); |
70 | |
71 | can_ok( $c, "cache_set" ); |
72 | can_ok( $c, "cache_get" ); |
aed484da |
73 | can_ok( $c, "cache_remove" ); |
c28ee69c |
74 | |
75 | $c->cache_set( foo => "bar" ); |
76 | is( $c->cache_get("foo"), "bar", "set" ); |
77 | |
aed484da |
78 | $c->cache_remove( "foo" ); |
79 | is( $c->cache_get("foo"), undef, "remove" ); |
c28ee69c |
80 | |
33002c69 |
81 | MockApp->register_cache_backend( elk => Catalyst::Plugin::Cache::Backend::Memory->new ); |
c28ee69c |
82 | |
83 | is( $c->choose_cache_backend_wrapper( key => "foo" ), $c->default_cache_backend, "choose default" ); |
84 | is( $c->choose_cache_backend_wrapper( key => "foo", backend => "elk" ), $c->get_cache_backend("elk"), "override choice" ); |
85 | |
86 | |
87 | $c->cache_set( foo => "gorch", backend => "elk" ); |
88 | is( $c->cache_get("foo"), undef, "set to custom backend (get from non custom)" ); |
89 | is( $c->cache_get("foo", backend => "elk"), "gorch", "set to custom backend (get from custom)" ); |
90 | |
2e4bde89 |
91 | my $cache_elk = $c->cache( backend => "elk" ); |
92 | my $cache_norm = $c->cache(); |
93 | |
94 | is( $cache_norm->get("foo"), undef, "default curried cache has no foo"); |
95 | is( $cache_elk->get("foo"), "gorch", "curried custom backend has foo" ); |