| 1 |
use 5.008; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More; |
| 7 |
use Test::Exception; |
| 8 |
use DBI; |
| 9 |
use DBD::Mock; |
| 10 |
|
| 11 |
# This tests that spurious extra ->execute invocations fail with a |
| 12 |
# useful message (RT #117162). This is because there was a bug in which |
| 13 |
# DBD::Mock->verify_bound_params didn't check that the session had run |
| 14 |
# out, and on return out-of-bounds element of the state array is |
| 15 |
# accessed, causing an unhelpful error "Can't use an undefined value |
| 16 |
# as an ARRAY reference at ../lib/DBD/Mock.pm line 635." |
| 17 |
|
| 18 |
my @session = ( |
| 19 |
{ |
| 20 |
'statement' => 'INSERT INTO foo (bar) values (?);', |
| 21 |
'results' => [], |
| 22 |
'bound_params' => [1] |
| 23 |
}, |
| 24 |
); |
| 25 |
|
| 26 |
my $dbh = DBI->connect('dbi:Mock:', '', '', { PrintError => 0, RaiseError => 1}); |
| 27 |
|
| 28 |
# Add just part of the expected session, such that the next step would be a 'BEGIN WORK' |
| 29 |
$dbh->{mock_session} = DBD::Mock::Session->new(@session); |
| 30 |
|
| 31 |
# now execute the steps in the session |
| 32 |
my $step = $session[0]; |
| 33 |
|
| 34 |
my $sth = $dbh->prepare($step->{statement}); |
| 35 |
ok $sth, |
| 36 |
"prepare statement"; |
| 37 |
|
| 38 |
my $params = $step->{bound_params} || []; |
| 39 |
ok $sth->execute(@$params), |
| 40 |
"execute statement"; |
| 41 |
|
| 42 |
# Session expects that to be all. So let's surprise it with another |
| 43 |
# ->execute. It should fail appropriately. |
| 44 |
throws_ok { |
| 45 |
ok $sth->execute(@$params), |
| 46 |
} qr/\QSession states exhausted, only '1' in DBD::Mock::Session\E/, |
| 47 |
"fails on executing one too many times"; |
| 48 |
|
| 49 |
done_testing(); |