1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
use 5.008;
use strict;
use warnings;
use Test::More;
use DBD::Mock;
use DBD::Mock::dr;
use DBI;
my ( $dsn, $user, $password, $attributes );
DBD::Mock::dr::set_connect_callbacks( sub {
( my $dbh, $dsn, $user, $password, $attributes ) = @_;
$dbh->{mock_add_resultset} = {
sql => 'SELECT foo FROM bar',
results => [[ 'foo' ], [ 10 ]]
};
} );
{
my $dbh = DBI->connect('dbi:Mock:', '', '');
isa_ok($dbh, 'DBI::db');
my $sth = $dbh->prepare('SELECT foo FROM bar');
isa_ok($sth, 'DBI::st');
my $rows = $sth->execute();
is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
my ($result) = $sth->fetchrow_array();
is($result, 10, '... got the result we expected');
$sth->finish();
}
# now let's check that we can reset the callbacks
DBD::Mock::dr::set_connect_callbacks( sub {
( my $dbh, $dsn, $user, $password, $attributes ) = @_;
$dbh->{mock_add_resultset} = {
sql => 'SELECT bar FROM foo',
results => [[ 'bar' ], [ 50 ]]
};
} );
{
my $dbh = DBI->connect('dbi:Mock:', '', '');
isa_ok($dbh, 'DBI::db');
my $sth = $dbh->prepare('SELECT bar FROM foo');
isa_ok($sth, 'DBI::st');
my $rows = $sth->execute();
is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
my ($result) = $sth->fetchrow_array();
is($result, 50, '... got the result we expected');
$sth->finish();
$sth = $dbh->prepare('SELECT foo FROM bar');
isa_ok($sth, 'DBI::st');
$rows = $sth->execute();
is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
($result) = $sth->fetchrow_array();
is($result, undef, "... as we have reset the callbacks this SELECT shouldn't match a result set ");
$sth->finish();
}
# add_connect_callbacks adds a new callback to the list
DBD::Mock::dr::add_connect_callbacks( sub {
( my $dbh, $dsn, $user, $password, $attributes ) = @_;
$dbh->{mock_add_resultset} = {
sql => 'SELECT foo FROM bar',
results => [[ 'foo' ], [ 10 ]]
};
} );
{
my $dbh = DBI->connect('dbi:Mock:', '', '');
isa_ok($dbh, 'DBI::db');
my $sth = $dbh->prepare('SELECT bar FROM foo');
isa_ok($sth, 'DBI::st');
my $rows = $sth->execute();
is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
my ($result) = $sth->fetchrow_array();
is($result, 50, '... got the result we expected');
$sth->finish();
$sth = $dbh->prepare('SELECT foo FROM bar');
isa_ok($sth, 'DBI::st');
$rows = $sth->execute();
is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
($result) = $sth->fetchrow_array();
is($result, 10, "... this should return a value as we've added its connect callback in");
$sth->finish();
}
DBD::Mock::dr::set_connect_callbacks( sub {
( my $dbh, $dsn, $user, $password, $attributes ) = @_;
} );
{
my $dbh = DBI->connect('dbi:Mock:database=TEST_DATABASE;hostname=localhost', 'TEST_USER', 'TEST_PASSWORD', { customAttribute => 1 });
isa_ok($dbh, 'DBI::db');
is ( $dsn, "database=TEST_DATABASE;hostname=localhost", "The database from the DSN should be passed through to the callback" );
is ( $user, "TEST_USER", "The username should be passed through to the callback" );
is ( $password, "TEST_PASSWORD", "The password should be passed through to the callback" );
is ( ref $attributes, "HASH", "The attributes passed through to the callback should be a hash reference" );
is ( $attributes->{customAttribute}, 1, "The custom attribute should be passed through to the callback" );
}
done_testing();
|