Skip to content
Browse files

A test showing DBD::Mock::Session->verify_bound_params doesn't check …

…for session exhaustion

Basically it succeeds despite there being no states left to verify,
and on return the ->execute method fails with an unhelpful error
"Can't use an undefined value # as an ARRAY reference at
../lib/DBD/Mock.pm line 635."
  • Loading branch information...
1 parent 23af0c7 commit 3a1d41bbb90b5ed0ad148533224cac46326d7484 wu-lee committed Mar 30, 2011
Showing with 45 additions and 0 deletions.
  1. +45 −0 t/bug_0003.t
View
45 t/bug_0003.t
@@ -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 3a1d41b

Please sign in to comment.
Something went wrong with that request. Please try again.