Mock/db.pm

filter: Statementstmt 96.4% 241 / 250 Branchbran 87.6% 128 / 146 Conditioncond 70.8% 34 / 48 MC/DCmcdc 50.0% 13 / 26 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 10 totaltotal 87.1% 434 / 498 CC 93 SCAR 30.2
CC
93
Cov
91%
STORE
40.6
prepare
20.1
FETCH
9.4
CRAP 20.5
↑ Summary Mock/st.pm »
1 package DBD::Mock::db;
2  
3 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 41 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 41 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 41 our $imp_data_size = 0;
10  
11 8 ▶sub ping {
Subroutine ping: called CC=1 SCAR=0.0
Pod undocumented
12 8 my ($dbh) = @_;
13 8 return $dbh->{mock_can_connect};
14 }
15  
16 14 ▶sub last_insert_id {
Subroutine last_insert_id: called CC=1 SCAR=0.0
Pod undocumented
17 14 my ($dbh) = @_;
18 14 return $dbh->{mock_last_insert_id};
19 }
20  
21 1 ▶sub get_info {
Subroutine get_info: called CC=2 SCAR=7.1
Pod undocumented
22 1 my ( $dbh, $attr ) = @_;
23 1 ▶ $dbh->{mock_get_info} ||= {};
Condition50%1/2
$dbh->{"mock_get_info"} ||= {}
l!l
10
MC/DC0%0/1
$dbh->{"mock_get_info"} ||= {}
$dbh->{"mock_get_info"}
Truth table50%1/2
A
A= $dbh->{"mock_get_info"}
Aresult
11
00
24 1 return $dbh->{mock_get_info}{$attr};
25 }
26  
27 5 ▶sub table_info {
Subroutine table_info: called CC=7 SCAR=19.5
Pod undocumented
28 5 my ( $dbh, @params ) = @_;
29  
30 5 ▶ my ($cataloge, $schema, $table, $type) = map { $_ || '' } @params[0..4];
Condition100%2/2
$_ || ''
l!l
817
MC/DC100%1/1
$_ || ''
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
31  
32 5 ▶ $dbh->{mock_table_info} ||= {};
Condition100%2/2
$dbh->{"mock_table_info"} ||= {}
l!l
41
MC/DC100%1/1
$dbh->{"mock_table_info"} ||= {}
$dbh->{"mock_table_info"}
Truth table100%2/2
A
A= $dbh->{"mock_table_info"}
Aresult
11
00
33  
34 5 ▶ my @tables = @{ $dbh->{mock_table_info}->{ $cataloge }->{ $schema }->{ $table }->{ $type } || [] };
Condition100%2/2
$dbh->{'mock_table_info'}{$cataloge}{$schema}{$table}{$type} || []
l!l
32
MC/DC100%1/1
$dbh->{'mock_table_info'}{$cataloge}{$schema}{$table}{$type} || []
$dbh->{'mock_table_info'}{$cataloge}{$schema}{$table}{$type}
Truth table100%2/2
A
A= $dbh->{'mock_table_info'}{$cataloge}{$schema}{$table}{$type}
Aresult
11
00
35  
36 5 my ($fieldNames, @rows) = map { [ @$_ ] } @tables;
37  
38 5 ▶ $fieldNames ||= [];
Condition100%2/2
$fieldNames ||= []
l!l
32
MC/DC100%1/1
$fieldNames ||= []
$fieldNames
Truth table100%2/2
A
A= $fieldNames
Aresult
11
00
39  
40 5 ▶ my $sponge = DBI->connect('dbi:Sponge:', '', '' )
Branch: 1 branch
BranchTrueFalseTotal
my $sponge = "DBI"->connect("dbi:Sponge:", "", "") or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr") 5 0 5
41 or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr");
42  
43 5 ▶ my $sth = $sponge->prepare("table_info", {
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $sponge->prepare("table_info", {"rows", \@rows, "NUM_OF_FIELDS", scalar @$fieldNames, "NAME", $fieldNames}) or return $dbh->DBI::set_err($sponge->err, $sponge->errstr) 5 0 5
44 rows => \@rows,
45 NUM_OF_FIELDS => scalar @$fieldNames,
46 NAME => $fieldNames
47 }) or return $dbh->DBI::set_err( $sponge->err(), $sponge->errstr() );
48  
49 5 return $sth;
50 }
51  
52 159 ▶sub prepare {
Subroutine prepare: called CC=20 SCAR=30.0
Pod undocumented
53 159 my ( $dbh, $statement ) = @_;
54  
55 159 ▶ unless ( $dbh->{mock_can_connect} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->{"mock_can_connect"}) 2 157 159
56 2 $dbh->set_err( 1, "No connection present" );
57 2 return;
58 }
59 157 ▶ unless ( $dbh->{mock_can_prepare} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->{"mock_can_prepare"}) 2 155 157
60 2 $dbh->set_err( 1, "Cannot prepare" );
61 2 return;
62 }
63 155 ▶ $dbh->{mock_can_prepare}++ if $dbh->{mock_can_prepare} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"mock_can_prepare"} < 0 3 152 155
64  
65 155 eval {
66 155 foreach my $parser ( @{ $dbh->{mock_parser} } )
67 {
68 4 ▶ if ( ref($parser) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parser eq 'CODE') { } 2 2 4
69 2 $parser->($statement);
70 }
71 else {
72 2 $parser->parse($statement);
73 }
74 }
75 };
76 155 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 153 155
77 2 my $parser_error = $@;
78 2 chomp $parser_error;
79 2 $dbh->set_err( 1,
80 "Failed to parse statement. Error: ${parser_error}. Statement: ${statement}"
81 );
82 2 return;
83 }
84  
85 153 my $sth = DBI::_new_sth( $dbh, { Statement => $statement } );
86 153 $sth->trace_msg( "Preparing statement '${statement}'\n", 1 );
87 153 my %track_params = ( statement => $statement );
88  
89 153 ▶ if ( my $session = $dbh->{mock_session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $dbh->{'mock_session'}) { } 54 99 153
90 54 eval {
91 54 my $rs = $session->results_for($statement);
92 49 ▶ if ( ref($rs) eq 'ARRAY' && scalar( @{$rs} ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rs eq 'ARRAY' and scalar @{$rs;} > 0) { } 37 12 49
Condition66%2/3
ref $rs eq "ARRAY" and scalar @{$rs;} > 0
!ll&&!rl&&r
01237
MC/DC50%1/2
ref $rs eq "ARRAY" and scalar @{$rs;} > 0
ref $rs eq "ARRAY" scalar @{$rs;} > 0
Truth table66%2/3
A and B
A= ref $rs eq "ARRAY"B= scalar @{$rs;} > 0
ABresult
0X0
100
111
93 37 my $fields = @{$rs}[0];
94 37 $track_params{return_data} = $rs;
95 37 $track_params{fields} = $fields;
96 37 $sth->STORE( NAME => $fields );
97 37 $sth->STORE( NUM_OF_FIELDS => scalar @{$fields} );
98 }
99 else {
100 12 $sth->trace_msg( "No return data set in DBH\n", 1 );
101 }
102 };
103  
104 54 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 5 49 54
105 5 $dbh->DBI::set_err( 1, "Session Error: $@. Statement: $statement" );
106 }
107  
108 }
109  
110 else {
111 # If we have available resultsets seed the tracker with one
112  
113 99 my ($rs, $callback, $failure, $prepare_attributes, $execute_attributes);
114  
115 99 ▶ if ( my $all_rs = $dbh->{mock_rs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $all_rs = $dbh->{"mock_rs"}) 60 39 99
116 60 ▶ if ( my $by_name = defined $all_rs->{named}{$statement} ? $all_rs->{named}{$statement} : first { $statement =~ m/$_->{regexp}/ } @{ $all_rs->{matching} } ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if (my $by_name = defined $all_rs->{'named'}{$statement} ? $all_rs->{'named'}{$statement} : &first(sub { $statement =~ /$_->{'regexp'}/; } , @{$$all_rs{'matching'};})) { } 49 11 60
defined $all_rs->{'named'}{$statement} ? : 46 14 60
117 # We want to copy this, because it is meant to be reusable
118 49 $rs = [ @{ $by_name->{results} } ];
119 49 $callback = $by_name->{callback};
120 49 $failure = $by_name->{failure};
121 49 $prepare_attributes = $by_name->{prepare_attributes};
122 49 $execute_attributes = $by_name->{execute_attributes};
123 }
124 else {
125 11 $rs = shift @{ $all_rs->{ordered} };
126 11 ▶ if (ref($rs) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rs eq "HASH") 1 10 11
127 1 $callback = $rs->{callback};
128 1 $failure = $rs->{failure};
129 1 $prepare_attributes = $rs->{prepare_attributes};
130 1 $execute_attributes = $rs->{execute_attributes};
131 1 $rs = [ @{ $rs->{results} } ];
132 }
133 }
134 }
135  
136 99 ▶ if ( ref($rs) eq 'ARRAY' && ( scalar( @{$rs} ) > 0 || $callback ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rs eq 'ARRAY' and scalar @{$rs;} > 0 || $callback) { } 57 42 99
Condition66%4/6
ref $rs eq "ARRAY" and scalar @{$rs;} > 0 || $callback
!ll&&!rl&&r
42057
scalar @{$rs;} > 0 or $callback
l!l&&r!l&&!r
5250
MC/DC33%1/3
ref $rs eq "ARRAY" and scalar @{$rs;} > 0 || $callback
ref $rs eq "ARRAY" scalar @{$rs;} > 0 $callback
Truth table75%3/4
A and B or C
A= ref $rs eq "ARRAY"B= scalar @{$rs;} > 0C= $callback
ABCresult
0XX0
1000
11X1
1011
137 57 my $fields = shift @{$rs};
138 57 $track_params{return_data} = $rs;
139 57 $track_params{fields} = $fields;
140 57 $track_params{callback} = $callback;
141 57 $track_params{failure} = $failure;
142 57 $track_params{driver_attributes} = $prepare_attributes;
143 57 $track_params{execute_attributes} = $execute_attributes;
144  
145 57 ▶ if( $fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields) 52 5 57
146 52 $sth->STORE( NAME => $fields );
147 52 $sth->STORE( NUM_OF_FIELDS => scalar @{$fields});
148 }
149  
150 }
151 else {
152 42 $sth->trace_msg( "No return data set in DBH\n", 1 );
153 }
154  
155 }
156  
157 # do not allow a statement handle to be created if there is no
158 # connection present.
159  
160 153 ▶ unless ( $dbh->FETCH('Active') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->FETCH("Active")) 0 153 153
161 0 $dbh->set_err( 1, "No connection present" );
162 0 return;
163 }
164  
165 # This history object will track everything done to the statement
166 153 my $history = DBD::Mock::StatementTrack->new(%track_params);
167 153 $sth->STORE( mock_my_history => $history );
168  
169 # ...now associate the history object with the database handle so
170 # people can browse the entire history at once, even for
171 # statements opened and closed in a black box
172  
173 153 my $all_history = $dbh->FETCH('mock_statement_history');
174 153 push @{$all_history}, $history;
175  
176 153 return $sth;
177 }
178  
179 41 *prepare_cached = \&prepare;
180  
181 {
182 41 my $begin_work_commit;
183  
184 6 ▶ sub begin_work {
Subroutine begin_work: called CC=4 SCAR=13.9
Pod undocumented
185 6 my $dbh = shift;
186 6 ▶ if ( $dbh->FETCH('AutoCommit') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->FETCH('AutoCommit')) { } 5 1 6
187 5 $dbh->STORE( 'AutoCommit', 0 );
188 5 $begin_work_commit = 1;
189 5 ▶ my $sth = $dbh->prepare('BEGIN WORK')
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("BEGIN WORK") or return $dbh->set_err(1, $DBI::errstr) 5 0 5
190 or return $dbh->set_err( 1, $DBI::errstr );
191 5 ▶ my $rc = $sth->execute()
Branch: 1 branch
BranchTrueFalseTotal
my $rc = $sth->execute or return $dbh->set_err(1, $DBI::errstr) 4 1 5
192 or return $dbh->set_err( 1, $DBI::errstr );
193 4 $sth->finish();
194 4 return $rc;
195 }
196 else {
197 1 return $dbh->set_err( 1,
198 'AutoCommit is off, you are already within a transaction' );
199 }
200 }
201  
202 4 ▶ sub commit {
Subroutine commit: called CC=6 SCAR=18.0
Pod undocumented
203 4 my $dbh = shift;
204 4 ▶ if ( $dbh->FETCH('AutoCommit') && $dbh->FETCH('Warn') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->FETCH("AutoCommit") and $dbh->FETCH("Warn")) 1 3 4
Condition66%2/3
$dbh->FETCH("AutoCommit") and $dbh->FETCH("Warn")
!ll&&!rl&&r
301
MC/DC50%1/2
$dbh->FETCH("AutoCommit") and $dbh->FETCH("Warn")
$dbh->FETCH("AutoCommit") $dbh->FETCH("Warn")
Truth table66%2/3
A and B
A= $dbh->FETCH("AutoCommit")B= $dbh->FETCH("Warn")
ABresult
0X0
100
111
205 1 return $dbh->set_err( 1, "commit ineffective with AutoCommit" );
206 }
207  
208 3 ▶ my $sth = $dbh->prepare('COMMIT')
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("COMMIT") or return $dbh->set_err(1, $DBI::errstr) 3 0 3
209 or return $dbh->set_err( 1, $DBI::errstr );
210 3 ▶ my $rc = $sth->execute()
Branch: 1 branch
BranchTrueFalseTotal
my $rc = $sth->execute or return $dbh->set_err(1, $DBI::errstr) 2 1 3
211 or return $dbh->set_err( 1, $DBI::errstr );
212 2 $sth->finish();
213  
214 2 ▶ if ($begin_work_commit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin_work_commit) 1 1 2
215 1 $dbh->STORE( 'AutoCommit', 1 );
216 1 $begin_work_commit = 0;
217 }
218  
219 2 return $rc;
220 }
221  
222 4 ▶ sub rollback {
Subroutine rollback: called CC=6 SCAR=18.0
Pod undocumented
223 4 my $dbh = shift;
224 4 ▶ if ( $dbh->FETCH('AutoCommit') && $dbh->FETCH('Warn') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->FETCH("AutoCommit") and $dbh->FETCH("Warn")) 1 3 4
Condition66%2/3
$dbh->FETCH("AutoCommit") and $dbh->FETCH("Warn")
!ll&&!rl&&r
301
MC/DC50%1/2
$dbh->FETCH("AutoCommit") and $dbh->FETCH("Warn")
$dbh->FETCH("AutoCommit") $dbh->FETCH("Warn")
Truth table66%2/3
A and B
A= $dbh->FETCH("AutoCommit")B= $dbh->FETCH("Warn")
ABresult
0X0
100
111
225 1 return $dbh->set_err( 1, "rollback ineffective with AutoCommit" );
226 }
227  
228 3 ▶ my $sth = $dbh->prepare('ROLLBACK')
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("ROLLBACK") or return $dbh->set_err(1, $DBI::errstr) 3 0 3
229 or return $dbh->set_err( 1, $DBI::errstr );
230 3 ▶ my $rc = $sth->execute()
Branch: 1 branch
BranchTrueFalseTotal
my $rc = $sth->execute or return $dbh->set_err(1, $DBI::errstr) 2 1 3
231 or return $dbh->set_err( 1, $DBI::errstr );
232 2 $sth->finish();
233  
234 2 ▶ if ($begin_work_commit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin_work_commit) 1 1 2
235 1 $dbh->STORE( 'AutoCommit', 1 );
236 1 $begin_work_commit = 0;
237 }
238  
239 2 return $rc;
240 }
241 }
242  
243 # NOTE:
244 # this method should work in most cases, however it does
245 # not exactly follow the DBI spec in the case of error
246 # handling. I am not sure if that level of detail is
247 # really nessecary since it is a weird error conditon
248 # which causes it to fail anyway. However if you find you do need it,
249 # then please email me about it. I think it would be possible
250 # to mimic it by accessing the DBD::Mock::StatementTrack
251 # object directly.
252 41 ▶sub selectcol_arrayref {
Subroutine selectcol_arrayref: called CC=4 SCAR=14.0
Pod undocumented
253 2 my ( $dbh, $query, $attrib, @bindvalues ) = @_;
254  
255 # get all the columns ...
256 2 my $a_ref = $dbh->selectall_arrayref( $query, $attrib, @bindvalues );
257  
258 # if we get nothing back, or dont get an
259 # ARRAY ref back, then we can assume
260 # something went wrong, and so return undef.
261 2 ▶ return undef unless defined $a_ref || ref($a_ref) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a_ref or ref $a_ref ne "ARRAY" 0 2 2
Condition33%1/3
defined $a_ref or ref $a_ref ne "ARRAY"
l!l&&r!l&&!r
200
MC/DC0%0/2
defined $a_ref or ref $a_ref ne "ARRAY"
defined $a_ref ref $a_ref ne "ARRAY"
Truth table33%1/3
A or B
A= defined $a_refB= ref $a_ref ne "ARRAY"
ABresult
1X1
011
000
262  
263 2 my @cols = 0;
264 2 ▶ if ( ref $attrib->{Columns} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrib->{"Columns"} eq "ARRAY") 1 1 2
265 1 @cols = map { $_ - 1 } @{ $attrib->{Columns} };
266 }
267  
268 # if we do get something then we
269 # grab all the columns out of it.
270 2 return [ map { @$_[@cols] } @{$a_ref} ];
271 }
272  
273 236 ▶sub FETCH {
Subroutine FETCH: called CC=9 SCAR=22.5
274 236 my ( $dbh, $attrib, $value ) = @_;
275 236 $dbh->trace_msg("Fetching DB attrib '$attrib'\n");
276  
277 236 ▶ if ( $attrib eq 'Active' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($attrib eq 'Active') { } 161 75 236
elsif ($attrib eq 'mock_all_history') { } 12 63 75
elsif ($attrib eq 'mock_all_history_iterator') { } 1 62 63
elsif ($attrib =~ /^mock/) { } 6 56 62
elsif ($attrib =~ /^(private_|dbi_|dbd_|[A-Z])/) { } 50 6 56
278 161 return $dbh->{mock_can_connect};
279 }
280 elsif ( $attrib eq 'mock_all_history' ) {
281 12 return $dbh->{mock_statement_history};
282 }
283 elsif ( $attrib eq 'mock_all_history_iterator' ) {
284 return DBD::Mock::StatementTrack::Iterator->new(
285 1 $dbh->{mock_statement_history} );
286 }
287 elsif ( $attrib =~ /^mock/ ) {
288 6 return $dbh->{$attrib};
289 }
290 elsif ( $attrib =~ /^(private_|dbi_|dbd_|[A-Z])/ ) {
291 50 $dbh->trace_msg(
292 "... fetching non-driver attribute ($attrib) that DBI handles\n");
293 50 return $dbh->SUPER::FETCH($attrib);
294 }
295 else {
296 6 ▶ if ( $dbh->{mock_attribute_aliases} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"mock_attribute_aliases"}) 6 0 6
297 6 ▶ if ( exists ${ $dbh->{mock_attribute_aliases}->{db} }{$attrib} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$dbh{"mock_attribute_aliases"}{"db"} }->{$attrib}) 6 0 6
298 my $mock_attrib =
299 6 $dbh->{mock_attribute_aliases}->{db}->{$attrib};
300 6 ▶ if ( ref($mock_attrib) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mock_attrib eq 'CODE') { } 0 6 6
301 0 return $mock_attrib->($dbh);
302 }
303 else {
304 6 return $dbh->FETCH($mock_attrib);
305 }
306 }
307 }
308 $dbh->trace_msg(
309 0 "... fetching non-driver attribute ($attrib) that DBI doesn't handle\n"
310 );
311 0 return $dbh->{$attrib};
312 }
313 }
314  
315 1122 ▶sub STORE {
Subroutine STORE: called CC=39 SCAR=37.0
316 1122 my ( $dbh, $attrib, $value ) = @_;
317  
318 1122 ▶ my $printed_value = $value || 'undef';
Condition100%2/2
$value || 'undef'
l!l
961161
MC/DC100%1/1
$value || 'undef'
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
319 1122 $dbh->trace_msg("Storing DB attribute '$attrib' with '$printed_value'\n");
320  
321 1122 ▶ if ( $attrib eq 'AutoCommit' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrib eq "AutoCommit") 107 1015 1122
322  
323 # These are magic DBI values that say we can handle AutoCommit
324 # internally as well
325 107 ▶ $value = ($value) ? -901 : -900;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 98 9 107
326 }
327  
328 1122 ▶ if ( $attrib eq 'mock_clear_history' ) {
Branch: 10 branches
BranchTrueFalseTotal
if ($attrib eq 'mock_clear_history') { } 5 1117 1122
elsif ($attrib eq 'mock_add_parser') { } 6 1111 1117
elsif ($attrib eq 'mock_add_resultset') { } 35 1076 1111
elsif ($attrib eq 'mock_start_insert_id') { } 3 1073 1076
elsif ($attrib eq 'mock_session') { } 47 1026 1073
elsif ($attrib =~ /^mock_(add_)?data_sources/) { } 1 1025 1026
elsif ($attrib =~ /^mock_add_table_info$/) { } 4 1021 1025
elsif ($attrib =~ /^mock_clear_table_info$/) { } 1 1020 1021
elsif ($attrib =~ /^mock/) { } 580 440 1020
elsif ($attrib =~ /^(private_|dbi_|dbd_|[A-Z])/) { } 438 2 440
329 5 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 5 0 5
330 5 $dbh->{mock_statement_history} = [];
331 }
332 5 return [];
333 }
334 elsif ( $attrib eq 'mock_add_parser' ) {
335 6 my $parser_type = ref($value);
336 6 my $is_valid_parser;
337  
338 6 ▶ if ( $parser_type eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parser_type eq 'CODE') { } 1 5 6
elsif ($parser_type and not $parser_type =~ /^(ARRAY|HASH|SCALAR)$/) { } 1 5 6
339 1 $is_valid_parser++;
340 }
341 ▶ elsif ( $parser_type && $parser_type !~ /^(ARRAY|HASH|SCALAR)$/ ) {
Condition100%3/3
$parser_type and not $parser_type =~ /^(ARRAY|HASH|SCALAR)$/
!ll&&!rl&&r
131
MC/DC100%2/2
$parser_type and not $parser_type =~ /^(ARRAY|HASH|SCALAR)$/
$parser_type not $parser_type =~ /^(ARRAY|HASH|SCALAR)$/
Truth table100%3/3
A and not(B)
A= $parser_typeB= not $parser_type =~ /^(ARRAY|HASH|SCALAR)$/
ABresult
0X0
100
111
342 1 $is_valid_parser = eval { $parser_type->can('parse') };
343 }
344  
345 6 ▶ unless ($is_valid_parser) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_valid_parser) 4 2 6
346 4 my $error =
347 "Parser must be a code reference or object with 'parse()' "
348 . "method (Given type: '$parser_type')";
349 4 $dbh->set_err( 1, $error );
350 4 return;
351 }
352 2 push @{ $dbh->{mock_parser} }, $value;
353 2 return $value;
354 }
355 elsif ( $attrib eq 'mock_add_resultset' ) {
356 35 my @copied_values;
357  
358 $dbh->{mock_rs} ||= {
359 35 ▶ named => {},
Condition100%2/2
$dbh->{"mock_rs"} ||= {"named", {}, "ordered", [], "matching", []}
l!l
1817
MC/DC100%1/1
$dbh->{"mock_rs"} ||= {"named", {}, "ordered", [], "matching", []}
$dbh->{"mock_rs"}
Truth table100%2/2
A
A= $dbh->{"mock_rs"}
Aresult
11
00
360 ordered => [],
361 matching => [],
362 };
363  
364 35 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 7 28 35
elsif (ref $value eq 'HASH') { } 28 0 28
365 7 @copied_values = @{$value};
366 7 push @{ $dbh->{mock_rs}{ordered} }, \@copied_values;
367 }
368 elsif ( ref $value eq 'HASH' ) {
369 28 my $name = $value->{sql};
370  
371 28 ▶ @copied_values = @{ $value->{results} ? $value->{results} : [] };
Branch: 1 branch
BranchTrueFalseTotal
$value->{'results'} ? : 24 4 28
372  
373 28 ▶ if (not defined $name) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $name) { } 1 27 28
elsif (ref $name eq 'Regexp') { } 4 23 27
374 1 ▶ push @{ $dbh->{mock_rs}{ordered} }, {
Branch: 1 branch
BranchTrueFalseTotal
ref $value->{'failure'} ? : 1 0 1
375 results => \@copied_values,
376 callback => $value->{callback},
377 1 failure => ref($value->{failure}) ? [ @{ $value->{failure} } ] : undef,
378 prepare_attributes => $value->{prepare_attributes},
379 execute_attributes => $value->{execute_attributes},
380 1 };
381 }
382 elsif ( ref $name eq "Regexp" ) {
383 my $matching = {
384 regexp => $name,
385 results => \@copied_values,
386 callback => $value->{callback},
387 1 failure => ref($value->{failure}) ? [ @{ $value->{failure} } ] : undef,
388 prepare_attributes => $value->{prepare_attributes},
389 execute_attributes => $value->{execute_attributes},
390 4 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
ref $value->{'failure'} ? : 1 3 4
391 # either replace existing match or push
392 3 ▶ grep { $_->{regexp} eq $name && ($_ = $matching) } @{ $dbh->{mock_rs}{matching} }
Condition66%2/3
$_->{'regexp'} eq $name && ($_ = $matching)
!ll&&!rl&&r
201
MC/DC50%1/2
$_->{'regexp'} eq $name && ($_ = $matching)
$_->{'regexp'} eq $name ($_ = $matching)
Truth table66%2/3
A && B
A= $_->{'regexp'} eq $nameB= ($_ = $matching)
ABresult
0X0
100
111
393 4 ▶ or push @{ $dbh->{mock_rs}{matching} }, $matching;
Branch: 1 branch
BranchTrueFalseTotal
grep {$_->{"regexp"} eq $name and $_ = $matching;} @{$$dbh{"mock_rs"}{"matching"};} or push @{$$dbh{"mock_rs"}{"matching"};}, $matching 1 3 4
394 }
395 else {
396 $dbh->{mock_rs}{named}{$name} = {
397 results => \@copied_values,
398 callback => $value->{callback},
399 1 failure => ref($value->{failure}) ? [ @{ $value->{failure} } ] : undef,
400 prepare_attributes => $value->{prepare_attributes},
401 execute_attributes => $value->{execute_attributes},
402 23 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
ref $value->{'failure'} ? : 1 22 23
403 }
404 }
405 else {
406 0 die "Must provide an arrayref or hashref when adding ",
407 "resultset via 'mock_add_resultset'.\n";
408 }
409  
410 35 return \@copied_values;
411 }
412 elsif ( $attrib eq 'mock_start_insert_id' ) {
413 3 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 3 0 3
414 $dbh->{mock_last_insert_ids} = {}
415 3 ▶ unless $dbh->{mock_last_insert_ids};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"mock_last_insert_ids"} 2 1 3
416 3 $dbh->{mock_last_insert_ids}{ $value->[0] } = $value->[1];
417 }
418 else {
419  
420 # we start at one minus the start id
421 # so that the increment works
422 0 $dbh->{mock_last_insert_id} = $value - 1;
423 }
424  
425 }
426 elsif ( $attrib eq 'mock_session' ) {
427 47 ▶ ( ref($value) && UNIVERSAL::isa( $value, 'DBD::Mock::Session' ) )
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 30 17 47
Condition50%4/8
defined $value && (ref $value && UNIVERSAL::isa($value, 'DBD::Mock::Session') || die("Only DBD::Mock::Session objects can be placed into the 'mock_session' slot\n"))
!ll&&!rl&&r
17300
ref $value and UNIVERSAL::isa($value, "DBD::Mock::Session") or die "Only DBD::Mock::Session objects can be placed into the 'mock_session' slot\n"
l!l
300
ref $value and UNIVERSAL::isa($value, "DBD::Mock::Session")
!ll&&!rl&&r
0030
MC/DC0%0/3
defined $value && (ref $value && UNIVERSAL::isa($value, 'DBD::Mock::Session') || die("Only DBD::Mock::Session objects can be placed into the 'mock_session' slot\n"))
defined $value ref $value UNIVERSAL::isa($value, "DBD::Mock::Session")
Truth table0%0/4
A && B and C
A= defined $valueB= ref $valueC= UNIVERSAL::isa($value, "DBD::Mock::Session")
ABCresult
0XX0
10X0
1100
1111
428 || die
429 "Only DBD::Mock::Session objects can be placed into the 'mock_session' slot\n"
430 if defined $value;
431 47 $dbh->{mock_session} = $value;
432 }
433 elsif ( $attrib =~ /^mock_(add_)?data_sources/ ) {
434 1 $dbh->{Driver}->STORE( $attrib, $value );
435 }
436 elsif ( $attrib =~ /^mock_add_table_info$/ ) {
437 4 ▶ $dbh->{mock_table_info} ||= {};
Condition50%1/2
$dbh->{"mock_table_info"} ||= {}
l!l
40
MC/DC0%0/1
$dbh->{"mock_table_info"} ||= {}
$dbh->{"mock_table_info"}
Truth table50%1/2
A
A= $dbh->{"mock_table_info"}
Aresult
11
00
438  
439 4 ▶ if ( ref $value ne "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "HASH") 0 4 4
440 0 die "mock_add_table_info needs a hash reference"
441 }
442  
443 4 ▶ my ( $cataloge, $schema, $table, $type ) = map { defined $_ ? $_ : '' } @$value{qw( cataloge schema table type )};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 6 10 16
444  
445 4 $dbh->{mock_table_info}->{ $cataloge }->{ $schema }->{ $table }->{ $type } = $value->{table_info};
446 }
447 elsif ( $attrib =~ /^mock_clear_table_info$/ ) {
448 1 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 1 0 1
449 1 $dbh->{mock_table_info} = {};
450 }
451  
452 1 return {};
453 }
454 elsif ( $attrib =~ /^mock/ ) {
455 580 return $dbh->{$attrib} = $value;
456 }
457 elsif ( $attrib =~ /^(private_|dbi_|dbd_|[A-Z])/ ) {
458 438 $dbh->trace_msg(
459 "... storing non-driver attribute ($attrib) with value ($printed_value) that DBI handles\n"
460 );
461 438 return $dbh->SUPER::STORE( $attrib, $value );
462 }
463 else {
464 2 $dbh->trace_msg(
465 "... storing non-driver attribute ($attrib) with value ($printed_value) that DBI won't handle\n"
466 );
467 2 return $dbh->{$attrib} = $value;
468 }
469 }
470  
471 87 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.4
472 87 my ($dbh) = @_;
473 87 ▶ if ( my $session = $dbh->{mock_session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $dbh->{"mock_session"}) 13 74 87
474 13 ▶ if ( $session->has_states_left ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->has_states_left) 0 13 13
475 0 die "DBH->finish called when session still has states left\n";
476 }
477 }
478 }
479  
480 11 ▶sub disconnect {
Subroutine disconnect: called CC=3 SCAR=11.0
Pod undocumented
481 11 my ($dbh) = @_;
482 11 ▶ if ( my $session = $dbh->{mock_session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $dbh->{"mock_session"}) 1 10 11
483 1 ▶ if ( $session->has_states_left ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->has_states_left) 1 0 1
484 1 die "DBH->finish called when session still has states left\n";
485 }
486 }
487 }
488  
489 41 1;
↑ Summary Mock/st.pm »