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