#!/usr/bin/perl use strict; use warnings; use Test::More tests => 13; BEGIN { use_ok('EventLogger'); } use DBI; my $dbh = DBI->connect('DBI:Mock:', '', ''); ## ---------------------------------------------- ## configure DBD::Mock for these tests ## ---------------------------------------------- # add the results for the inserts in logEvent $dbh->{mock_add_resultset} = [[ 1 ]]; $dbh->{mock_add_resultset} = [[ 2 ]]; $dbh->{mock_add_resultset} = [[ 3 ]]; # add results for the selects in getEventReport $dbh->{mock_add_resultset} = [[ "count(*)" ], [ 10 ]]; $dbh->{mock_add_resultset} = [[ "count(*)" ], [ 5 ]]; $dbh->{mock_add_resultset} = [[ "count(*)" ], [ 8 ]]; ## ---------------------------------------------- ## end configure DBD::Mock ## ---------------------------------------------- can_ok("EventLogger", 'new'); my $logger = EventLogger->new($dbh); isa_ok($logger, 'EventLogger'); my $session_id = '0123456789'; can_ok($logger, 'logEvent'); # test each one of our even types $logger->logEvent($session_id, 'Successful Login'); $logger->logEvent($session_id, 'Standard Report'); $logger->logEvent($session_id, 'Custom Report'); can_ok($logger, 'getEventReport'); my %report = $logger->getEventReport(8, 2004); # check the report that was # generated by the mock results is_deeply( \%report, { 'Successful Logins' => 10, 'Standard Reports' => 5, 'Custom Reports' => 8, 'Total Reports' => 13, 'Reports Per Login' => 1.3 }, '... this is our report' ); ## ---------------------------------------------- ## now check DBD::Mock's query history ## ---------------------------------------------- # now we get the history of our driver my $history = $dbh->{mock_all_history}; # check how many statements we executed cmp_ok(scalar @{$history}, '==', 6, '... we executed 6 statements'); # check each of the statements we # expected to execute is($history->[0]->statement(), "INSERT INTO event_table (session_id, datestamp, event) VALUES('0123456789', NOW(), 'Successful Login')", '... this the expected statement'); is($history->[1]->statement(), "INSERT INTO event_table (session_id, datestamp, event) VALUES('0123456789', NOW(), 'Standard Report')", '... this the expected statement'); is($history->[2]->statement(), "INSERT INTO event_table (session_id, datestamp, event) VALUES('0123456789', NOW(), 'Custom Report')", '... this the expected statement'); is($history->[3]->statement(), "SELECT COUNT(*) FROM event_table WHERE event = 'Successful Login' AND MONTH(date_created) = 8 AND YEAR(date_created) = 2004", '... this the expected statement'); is($history->[4]->statement(), "SELECT COUNT(*) FROM event_table WHERE event = 'Standard Report' AND MONTH(date_created) = 8 AND YEAR(date_created) = 2004", '... this the expected statement'); is($history->[5]->statement(), "SELECT COUNT(*) FROM event_table WHERE event = 'Custom Report' AND MONTH(date_created) = 8 AND YEAR(date_created) = 2004", '... this the expected statement'); 1;