Permalink
Browse files

Merge branch 'fix-verify-bound-params'

* fix-verify-bound-params:
  Fix DBD::Mock::Session->verify_bound_params to check for state exhaustion
  A test showing DBD::Mock::Session->verify_bound_params doesn't check for session exhaustion
  Add DBD::Mock::Session->current_state method
  • Loading branch information...
2 parents 4dcccc7 + 179e83a commit 09b2b02b56ba23f67fe617358bf1a841788bb10f wu-lee committed Mar 30, 2011
Showing with 62 additions and 4 deletions.
  1. +17 −4 lib/DBD/Mock.pm
  2. +45 −0 t/bug_0003.t
View
@@ -622,9 +622,12 @@ sub execute {
if (my $session = $dbh->{mock_session}) {
eval {
+ my $state = $session->current_state;
$session->verify_bound_params($dbh, $tracker->bound_params());
- my $idx = $session->{state_index} - 1;
- my @results = @{$session->{states}->[$idx]->{results}};
+
+ # Load a copy of the results to return (minus the field
+ # names) into the tracker
+ my @results = @{$state->{results}};
shift @results;
$tracker->{return_data} = \@results;
};
@@ -1193,6 +1196,12 @@ sub name { (shift)->{name} }
sub reset { (shift)->{state_index} = 0 }
sub num_states { scalar( @{ (shift)->{states} } ) }
+sub current_state {
+ my $self = shift;
+ my $idx = $self->{state_index};
+ return $self->{states}[$idx];
+}
+
sub has_states_left {
my $self = shift;
return $self->{state_index} < scalar(@{$self->{states}});
@@ -1204,7 +1213,7 @@ sub verify_statement {
($self->has_states_left)
|| die "Session states exhausted, only '" . scalar(@{$self->{states}}) . "' in DBD::Mock::Session (" . $self->{name} . ")";
- my $current_state = $self->{states}->[$self->{state_index}];
+ my $current_state = $self->current_state;
# make sure our state is good
(exists ${$current_state}{statement} && exists ${$current_state}{results})
|| die "Bad state '" . $self->{state_index} . "' in DBD::Mock::Session (" . $self->{name} . ")";
@@ -1236,7 +1245,11 @@ sub verify_statement {
sub verify_bound_params {
my ($self, $dbh, $params) = @_;
- my $current_state = $self->{states}->[$self->{state_index}];
+
+ ($self->has_states_left)
+ || die "Session states exhausted, only '" . scalar(@{$self->{states}}) . "' in DBD::Mock::Session (" . $self->{name} . ")";
+
+ my $current_state = $self->current_state;
if (exists ${$current_state}{bound_params}) {
my $expected = $current_state->{bound_params};
(scalar(@{$expected}) == scalar(@{$params}))
View
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+use Test::More tests => 3;
+use strict;
+use warnings;
+use Test::Exception;
+use DBI;
+use DBD::Mock;
+
+# This tests that spurious extra ->execute invocations fail with a
+# useful message. This is because there was a bug in which
+# DBD::Mock->verify_bound_params didn't check that the session had run
+# out, and on return out-of-bounds element of the state array is
+# accessed, causing an unhelpful error "Can't use an undefined value
+# as an ARRAY reference at ../lib/DBD/Mock.pm line 635."
+
+my @session = (
+ {
+ 'statement' => 'INSERT INTO foo (bar) values (?);',
+ 'results' => [],
+ 'bound_params' => [1]
+ },
+);
+
+my $dbh = DBI->connect('dbi:Mock:', '', '', { PrintError => 0, RaiseError => 1});
+
+# Add just part of the expected session, such that the next step would be a 'BEGIN WORK'
+$dbh->{mock_session} = DBD::Mock::Session->new(@session);
+
+# now execute the steps in the session
+my $step = $session[0];
+
+my $sth = $dbh->prepare($step->{statement});
+ok $sth,
+ "prepare statement";
+
+my $params = $step->{bound_params} || [];
+ok $sth->execute(@$params),
+ "execute statement";
+
+# Session expects that to be all. So let's surprise it with another
+# ->execute. It should fail appropriately.
+throws_ok {
+ ok $sth->execute(@$params),
+} qr/\QSession states exhausted, only '1' in DBD::Mock::Session\E/,
+ "fails on executing one too many times";

0 comments on commit 09b2b02

Please sign in to comment.