use strict; use warnings; use Plack::Test; use Plack::Builder; use HTTP::Request::Common; use Test::More; use Test::MockObject; my $KEYS = { 'coy:knows:pseudonoise:codes' => [ 'string', 9000 ], 'six:slimy:snails:sailed:silently' => [ 'list', 35 ], 'eleven:benevolent:elephants' => [ 'hash', 17 ], 'two:tried:and:true:tridents' => [ 'set', 101 ], 'tie:twine:to:three:tree:twigs' => [ 'zset', 66 ], }; sub generic_len { exists $KEYS->{$_[1]} ? $KEYS->{$_[1]}->[1] : undef }; my $fakeredis = Test::MockObject->new; Test::MockObject->fake_module('Redis', new => sub { $fakeredis }, VERSION => sub { '1.955' }); $fakeredis->set_true('select', 'quit', 'ping'); $fakeredis->mock('keys', sub { keys %$KEYS }); $fakeredis->mock('type', sub { exists $KEYS->{$_[1]} ? $KEYS->{$_[1]}->[0] : undef }); map { $fakeredis->mock($_, sub { &generic_len(@_) }) } qw/strlen hlen llen scard zcard/; { my $app = builder { enable 'Debug', panels => [ [ 'Redis::Keys', server => 'localhost:6379', db => 0 ], ]; sub { [ 200, [ 'Content-Type' => 'text/html' ], [ 'OK' ] ]; }; }; test_psgi $app, sub { my ($cb) = @_; my $res = $cb->(GET '/'); is $res->code, 200, 'Redis-Keys: response code 200'; like $res->content, qr||, 'Redis-Keys: panel found'; like $res->content, qr|DB #0 \(5\)|, 'Redis-Keys: subtitle points to 5 keys in database 0'; like $res->content, qr|coy:knows:pseudonoise:codes[.\s\n\r]*STRING \(9000\)|m, 'Redis-Keys: has string key (9000 bytes)'; like $res->content, qr|six:slimy:snails:sailed:silently[.\s\n\r]*LIST \(35\)|m, 'Redis-Keys: has list key (35 elements)'; like $res->content, qr|eleven:benevolent:elephants[.\s\n\r]*HASH \(17\)|m, 'Redis-Keys: has hash key (17 fields)'; like $res->content, qr|two:tried:and:true:tridents[.\s\n\r]*SET \(101\)|m, 'Redis-Keys: has set key (101 members)'; like $res->content, qr|tie:twine:to:three:tree:twigs[.\s\n\r]*ZSET \(66\)|m, 'Redis-Keys: has sorted set key (66 members)'; }; } done_testing();