blib/lib/DBD/Mock/db.pm

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