NAME

Class::DBI::MockDBD - Mocked database interface for Class::DBI

SYNOPSIS

use base qw(Class::DBI::MockDBD);

or

# probably nicer ways to do this .. but classes won't need a single line of code changed (with any luck)

use ClassName;

unshift(@ClassName::ISA,'Class::DBI::MockDBD');

# set up result

ClassName->next_result([ [qw/foo_id foo_name foo_bar/],[1,'aaaa','bbbb',]...]);

# run query

my $iterator = ClassName->search(...);

# get sql and params for query

my $sql = ClassName->last_query_info('statement');

my $params = ClassName->last_query_info('params');

DESCRIPTION

A Class::DBI subclass allowing you to 'Mock' a database for testing and/or debugging purposes, using DBD::Mock, via some additional API methods.

METHODS

Calling a method that touches the database without specifying the results with next_result or next_result_session method first will result in a fatal error.

next_result

This class method prepares the set of results to be provided to the next query made to the mocked database.

# set up result before calling method that will interact with mocked database

ClassName->next_result([ [qw/foo_id foo_name foo_bar/],[1,'aaaa','bbbb',]...]);

ClassName->search(foo_bar => 'bbbb');

next_result_session

ClassName->next_result_session([ { statement => 'select * from tablename where field = ?', results => [ .. ], bound_params => [ 10, qr/\d+/ ], }, { statement => 'select * from tablename where field = ?', results => [ .. ], bound_params => [ 10, qr/\d+/ ], }, { statement => 'select * from tablename where field = ?', results => [ .. ], bound_params => [ 10, qr/\d+/ ], }, ]);

ClassName->search(foo_bar => 'bbbb');

last_query_info

This class method provides the statement and params of the last query to the mocked database.

my $sql = ClassName->last_query_info('statement');

my $params = ClassName->last_query_info('params');

It takes an argument specifying what information you want back : 'params' or 'statement'.

Query parameters are returned as an arrayref, SQL statement is returned as a string.

METHODS OVER-RIDDEN/REDEFINED

MockDBD over-rides and/or redefines the following class and object methods :

  • sth_to_objects : Class method over-rides that inherited from Class::DBI

  • update : Class method over-rides that inherited from Class::DBI

  • _insert_row : Class method over-rides that inherited from Class::DBI

BUGS AND CAVEATS

* rv return value from execute is not correct (DBD::Mock issue)

SEE ALSO

* Class::DBI

* Mock::DBD

* DBI::Mock

* Website : http://www.aarontrevena.co.uk/opensource/

AUTHOR

aaron trevena, <aaron.trevena@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2007 by aaron trevena

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available.