lib/DBD/Mock/Session/GenerateFixtures.pm

filter: Statementstmt 96.4% 512 / 531 Branchbran 82.9% 73 / 88 Conditioncond 54.9% 28 / 51 MC/DCmcdc 34.4% 10 / 29 Subroutinesub 96.1% 74 / 77 Podpod 100% 4 / 4 totaltotal 89.8% 701 / 780 CC 71 SCAR 14.7
CC
71
Cov
91%
__ANON__
14.7
__ANON__
11.4
_write_to_file
7.6
CRAP 4.4
↑ Summary
1 package DBD::Mock::Session::GenerateFixtures;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use DBD::Mock;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 18 ▶use Sub::Override;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use English qw ( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use File::Slurper qw (read_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use Data::Walk;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 18 our $VERSION = 1.16;
23  
24 18 our $override;
25 18 my $JSON_OBJ = Cpanel::JSON::XS->new()->utf8->pretty();
26 18 my $cursor = 1;
27 18 my $ref_cursor = \$cursor;
28  
29 18 Readonly::Hash my %MOCKED_DBI_METHODS => (
30 execute => 'DBI::st::execute',
31 bind_param => 'DBI::st::bind_param',
32 fetchrow_hashref => 'DBI::st::fetchrow_hashref',
33 fetchrow_arrayref => 'DBI::st::fetchrow_arrayref',
34 fetchrow_array => 'DBI::st::fetchrow_array',
35 selectall_arrayref => 'DBI::db::selectall_arrayref',
36 selectall_hashref => 'DBI::db::selectall_hashref',
37 selectcol_arrayref => 'DBI::db::selectcol_arrayref',
38 selectrow_array => 'DBI::db::selectrow_array',
39 selectrow_arrayref => 'DBI::db::selectrow_arrayref',
40 selectrow_hashref => 'DBI::db::selectrow_hashref',
41 fetch => 'DBI::st::fetch',
42 prepare_cached => 'DBI::db::prepare_cached',
43 prepare => 'DBI::db::prepare',
44 mocked_prepare => 'DBD::Mock::db::prepare',
45 mocked_execute => 'DBD::Mock::st::execute',
46 mocked_bind_param => 'DBD::Mock::st::bind_param',
47 mocked_bind_param_in_out => 'DBD::Mock::st::bind_param_inout',
48 begin_work => 'DBI::db::begin_work',
49 commit => 'DBI::db::commit',
50 rollback => 'DBI::db::rollback',
51 bind_param_in_out => 'DBI::st::bind_param_inout',
52 );
53  
54 25 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
55 25 my ( $class, $args_for ) = @_;
56 25 my $self = bless {}, $class;
57  
58 25 ▶ if ($args_for) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args_for) { } 23 2 25
59 23 $self->_validate_args($args_for);
60 20 $self->_initialize($args_for);
61 }
62 else {
63 2 $self->_initialize();
64 }
65  
66 21 return $self;
67 }
68  
69 22 ▶sub _initialize {
Subroutine _initialize: called CC=5 SCAR=16.1
70 22 my $self = shift;
71 22 my $args_for = shift;
72  
73 22 my %args_for = ();
74  
75 22 ▶ if ($args_for) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args_for) 20 2 22
76 20 %args_for = %{$args_for};
77 }
78  
79 22 $self->_set_fixtures_file( $args_for{file} );
80 22 $self->{override_flag} = 0;
81 22 $override = Sub::Override->new();
82 22 $self->{override} = $override;
83  
84 22 ▶ if ( my $dbh = $args_for{dbh} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $dbh = $args_for{'dbh'}) { } 9 13 22
elsif (my $fixtures = $args_for{'data'}) { } 3 10 13
elsif (-e $self->{'fixture_file'}) { } 9 1 10
85 9 $self->{dbh} = $dbh;
86 9 $self->{bind_params} = [];
87 9 $self->{override_flag} = 1;
88 9 $self->_override_dbi_methods();
89 9 $self->{result} = [];
90 }
91 elsif ( my $fixtures = $args_for{data} ) {
92 3 $self->_process_mock_data($fixtures);
93 3 $self->_set_mock_dbh($fixtures);
94 }
95 elsif ( -e $self->{fixture_file} ) {
96 9 my $data = $JSON_OBJ->decode( read_text( $self->{fixture_file} ) );
97 9 $self->_process_mock_data($data);
98 9 $self->_set_mock_dbh($data);
99 }
100 else {
101 1 croak "No mocked data is available, you can resolve this by providing the 'dbh'
102 argument to the 'new' method to generate it. Alternatively, you can pass either
103 a file or data argument to the 'new' method";
104 }
105  
106 21 return $self;
107 }
108  
109 12 ▶sub _set_mock_dbh {
Subroutine _set_mock_dbh: called CC=1 SCAR=0.0
110 12 my ( $self, $data ) = @_;
111  
112 12 my $dbh = DBI->connect(
113 'dbi:Mock:',
114 '', '',
115 {
116 RaiseError => 1,
117 PrintError => 0
118 }
119 );
120  
121 12 my $dbh_session = DBD::Mock::Session->new( $PROGRAM_NAME => @{$data} );
122 12 $self->_override_dbi_mocked_prepare( $MOCKED_DBI_METHODS{mocked_prepare} );
123 12 $self->_override_dbi_mocked_bind_param( $MOCKED_DBI_METHODS{mocked_bind_param} );
124 12 $self->_override_dbi_mocked_bind_param_in_out( $MOCKED_DBI_METHODS{mocked_bind_param_in_out} );
125 12 $self->_override_dbi_mocked_execute( $MOCKED_DBI_METHODS{mocked_execute} );
126  
127 12 $dbh->{mock_session} = $dbh_session;
128 12 $self->{dbh} = $dbh;
129  
130 12 return $self;
131 }
132  
133 9 ▶sub _override_dbi_methods {
Subroutine _override_dbi_methods: called CC=1 SCAR=0.0
134 9 my $self = shift;
135  
136 9 $self->_override_dbi_execute( $MOCKED_DBI_METHODS{execute} );
137 9 $self->_override_dbi_bind_param( $MOCKED_DBI_METHODS{bind_param} );
138 9 $self->_override_dbi_fetchrow_hashref( $MOCKED_DBI_METHODS{fetchrow_hashref} );
139 9 $self->_override_dbi_fetchrow_arrayref( $MOCKED_DBI_METHODS{fetchrow_arrayref} );
140 9 $self->_override_dbi_fetchrow_array( $MOCKED_DBI_METHODS{fetchrow_array} );
141 9 $self->_override_dbi_selectall_arrayref( $MOCKED_DBI_METHODS{selectall_arrayref} );
142 9 $self->_override_dbi_selectall_hashref( $MOCKED_DBI_METHODS{selectall_hashref} );
143 9 $self->_override_dbi_selectcol_arrayref( $MOCKED_DBI_METHODS{selectcol_arrayref} );
144 9 $self->_override_dbi_selectrow_array( $MOCKED_DBI_METHODS{selectrow_array} );
145 9 $self->_override_dbi_selectrow_arrayref( $MOCKED_DBI_METHODS{selectrow_arrayref} );
146 9 $self->_override_dbi_selectrow_hashref( $MOCKED_DBI_METHODS{selectrow_hashref} );
147 9 $self->_override_dbi_fetch( $MOCKED_DBI_METHODS{fetch} );
148 9 $self->_override_dbi_prepare_cached( $MOCKED_DBI_METHODS{prepare_cached} );
149 9 $self->_override_dbi_prepare( $MOCKED_DBI_METHODS{prepare} );
150 9 $self->_override_dbi_begin_work( $MOCKED_DBI_METHODS{begin_work} );
151 9 $self->_override_dbi_commit( $MOCKED_DBI_METHODS{commit} );
152 9 $self->_override_dbi_rollback( $MOCKED_DBI_METHODS{rollback} );
153 9 $self->_override_bind_param_in_out( $MOCKED_DBI_METHODS{bind_param_in_out} );
154  
155 9 return $self;
156 }
157  
158 20 ▶sub get_dbh {
Subroutine get_dbh: called CC=1 SCAR=0.0
Pod documented
159 20 my $self = shift;
160  
161 20 return $self->{dbh};
162 }
163  
164 228 ▶sub get_override_object {
Subroutine get_override_object: called CC=1 SCAR=0.0
Pod documented
165 228 my $self = shift;
166  
167 228 return $self->{override};
168 }
169  
170 1 ▶sub restore_all {
Subroutine restore_all: called CC=3 SCAR=11.0
Pod documented
171 1 my $self = shift;
172  
173 1 foreach my $key ( keys %MOCKED_DBI_METHODS ) {
174 22 ▶ next if $key =~ m/^mocked/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^mocked/ 4 18 22
175 18 $self->get_override_object()->restore( $MOCKED_DBI_METHODS{$key} );
176 }
177  
178 1 return $self;
179 }
180  
181 9 ▶sub _override_dbi_execute {
Subroutine _override_dbi_execute: called CC=1 SCAR=0.1
182 9 my $self = shift;
183 9 my $dbi_execute = shift;
184  
185 9 my $orig_execute = \&$dbi_execute;
186  
187 $self->get_override_object()->replace(
188 $dbi_execute,
189 57 ▶ sub {
Subroutine __ANON__: called CC=13 SCAR=26.9
190 57 my ( $sth, @args ) = @_;
191  
192 57 ▶ my $sql = $sth->{Statement} // $sth->{Database}->{Statement} // '';
Condition40%2/5
($sth->{"Statement"} // $sth->{"Database"}{"Statement"}) // ""
l!l
570
$sth->{"Statement"} // $sth->{"Database"}{"Statement"}
l!l&&r!l&&!r
5700
MC/DC0%0/2
($sth->{"Statement"} // $sth->{"Database"}{"Statement"}) // ""
$sth->{"Statement"} $sth->{"Database"}{"Statement"}
Truth table33%1/3
A // B
A= $sth->{"Statement"}B= $sth->{"Database"}{"Statement"}
ABresult
1X1
011
000
193 57 $sql = $self->_normalize_sql($sql);
194  
195 57 my $col_names;
196 my $retval;
197 57 my $error;
198 57 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
199 57 $retval = $orig_execute->( $sth, @args );
200 $col_names = $sth->{NAME}
201 57 ▶ if $sql !~ m/^INSERT|^UPDATE|^DELETE/i;
Branch: 1 branch
BranchTrueFalseTotal
if not $sql =~ /^INSERT|^UPDATE|^DELETE/i 43 14 57
202 }
203 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 0 $error = $_;
205 57 };
206  
207 57 my $rows = $sth->rows();
208 57 my $query_data = {
209 statement => $sql,
210 bound_params => \@args,
211 col_names => $col_names,
212 };
213  
214 57 my $result = [];
215 57 ▶ if ( $sql =~ m/^INSERT|^UPDATE|^DELETE/i && $retval ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /^INSERT|^UPDATE|^DELETE/i and $retval) 14 43 57
Condition66%2/3
$sql =~ /^INSERT|^UPDATE|^DELETE/i and $retval
!ll&&!rl&&r
43014
MC/DC50%1/2
$sql =~ /^INSERT|^UPDATE|^DELETE/i and $retval
$sql =~ /^INSERT|^UPDATE|^DELETE/i $retval
Truth table66%2/3
A and B
A= $sql =~ /^INSERT|^UPDATE|^DELETE/iB= $retval
ABresult
0X0
100
111
216 14 push @$result, ['rows'];
217 14 foreach my $row ( 1 .. $rows ) {
218 16 push @{$result}, [];
219 }
220 14 $query_data->{results} = $result;
221 }
222  
223 $query_data->{bound_params} = $self->{bind_params}
224 if ref $self->{bind_params}
225 57 ▶ && scalar @{ $self->{bind_params} } > 0;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"bind_params"} and scalar @{$self->{"bind_params"};} > 0 0 57 57
Condition33%1/3
ref $self->{"bind_params"} and scalar @{$self->{"bind_params"};} > 0
!ll&&!rl&&r
0570
MC/DC0%0/2
ref $self->{"bind_params"} and scalar @{$self->{"bind_params"};} > 0
ref $self->{"bind_params"} scalar @{$self->{"bind_params"};} > 0
Truth table33%1/3
A and B
A= ref $self->{"bind_params"}B= scalar @{$self->{"bind_params"};} > 0
ABresult
0X0
100
111
226  
227 57 ▶ if ( $self->{bind_named_params}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bind_named_params"} and scalar keys %{$$self{"bind_named_params"};}) 3 54 57
Condition100%2/2
$self->{"bind_named_params"} and scalar keys %{$$self{"bind_named_params"};}
!ll
948
MC/DC100%1/1
$self->{"bind_named_params"} and scalar keys %{$$self{"bind_named_params"};}
$self->{"bind_named_params"}
Truth table100%2/2
A
A= $self->{"bind_named_params"}
Aresult
00
11
228 48 && scalar keys %{ $self->{bind_named_params} } )
229 {
230 3 my %seen = ();
231 3 my @order = grep { !$seen{$_}++ } ( $sql =~ m/(:\w+)/g );
232 $query_data->{bound_params} =
233 3 [ map { $self->{bind_named_params}->{$_} } @order ];
234 }
235  
236 # query failed:
237 57 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 57 57
238 0 ▶ $query_data->{failure} = [ $sth->err, $sth->errstr // $error ];
Condition0%0/3
$sth->errstr // $error
l!l&&r!l&&!r
000
MC/DC0%0/2
$sth->errstr // $error
$sth->errstr $error
Truth table0%0/3
A // B
A= $sth->errstrB= $error
ABresult
1X1
011
000
239 0 $query_data->{results} = DBD::Mock::NULL_RESULTSET;
240 }
241  
242 57 ▶ push @{ $self->{result} }, $query_data
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /BEGIN_WORK|COMMIT/ 56 1 57
243 if $sql !~ m/BEGIN_WORK|COMMIT/;
244 57 $self->_write_to_file();
245 57 $self->{bind_params} = [];
246 57 $self->{bind_named_params} = {};
247 57 $self->{sth} = $sth;
248 57 return $retval;
249 }
250 9 );
251  
252 9 return $self;
253 }
254  
255 12 ▶sub _override_dbi_mocked_execute {
Subroutine _override_dbi_mocked_execute: called CC=1 SCAR=0.2
256 12 my $self = shift;
257 12 my $mocked_dbi_execute = shift;
258  
259 12 my $orig_mocked_dbi_execute = \&$mocked_dbi_execute;
260 $self->get_override_object()->replace(
261 $mocked_dbi_execute,
262 53 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=24.3
263 53 my ( $sth, @params ) = @_;
264  
265 53 ▶ if ( $self->{bind_named_params}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bind_named_params"} and scalar keys %{$$self{"bind_named_params"};}) 5 48 53
Condition100%2/2
$self->{"bind_named_params"} and scalar keys %{$$self{"bind_named_params"};}
!ll
1142
MC/DC100%1/1
$self->{"bind_named_params"} and scalar keys %{$$self{"bind_named_params"};}
$self->{"bind_named_params"}
Truth table100%2/2
A
A= $self->{"bind_named_params"}
Aresult
00
11
266 42 && scalar keys %{ $self->{bind_named_params} } )
267 {
268 5 my %seen = ();
269 5 ▶ my $sql = $sth->{Statement} // $sth->{Database}->{Statement} // '';
Condition40%2/5
($sth->{"Statement"} // $sth->{"Database"}{"Statement"}) // ""
l!l
50
$sth->{"Statement"} // $sth->{"Database"}{"Statement"}
l!l&&r!l&&!r
500
MC/DC0%0/2
($sth->{"Statement"} // $sth->{"Database"}{"Statement"}) // ""
$sth->{"Statement"} $sth->{"Database"}{"Statement"}
Truth table33%1/3
A // B
A= $sth->{"Statement"}B= $sth->{"Database"}{"Statement"}
ABresult
1X1
011
000
270 5 my @order = grep { !$seen{$_}++ } ( $sql =~ m/(:\w+)/g );
271 5 @params = map { $self->{bind_named_params}->{$_} } @order;
272 }
273  
274 53 $self->{bind_named_params} = {};
275 53 $self->{bind_parmas} = [];
276  
277 53 my $session = $sth->{Database}->{mock_session};
278 53 my $current_state = undef;
279  
280 53 ▶ if ( $session && exists $session->{state_index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session and exists $session->{"state_index"}) 53 0 53
Condition33%1/3
$session and exists $session->{"state_index"}
!ll&&!rl&&r
0053
MC/DC0%0/2
$session and exists $session->{"state_index"}
$session exists $session->{"state_index"}
Truth table33%1/3
A and B
A= $sessionB= exists $session->{"state_index"}
ABresult
0X0
100
111
281 53 my $idx = $session->{state_index};
282 53 $current_state = $session->{states}->[$idx];
283 }
284 53 my $r = $orig_mocked_dbi_execute->( $sth, @params );
285  
286 53 ▶ if ( $current_state && $current_state->{failure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_state and $current_state->{"failure"}) 0 53 53
Condition33%1/3
$current_state and $current_state->{"failure"}
!ll&&!rl&&r
0530
MC/DC0%0/2
$current_state and $current_state->{"failure"}
$current_state $current_state->{"failure"}
Truth table33%1/3
A and B
A= $current_stateB= $current_state->{"failure"}
ABresult
0X0
100
111
287 0 my ( $err_code, $err_msg ) = @{ $current_state->{failure} };
288 0 $sth->set_err( $err_code, $err_msg );
289 0 return undef;
290 }
291  
292 53 return $r;
293 }
294 12 );
295 }
296  
297 12 ▶sub _override_dbi_mocked_bind_param {
Subroutine _override_dbi_mocked_bind_param: called CC=1 SCAR=0.0
298 12 my $self = shift;
299 12 my $mocked_bind_param = shift;
300  
301 12 my $orig_mocked_bind_param = \&$mocked_bind_param;
302  
303 $self->get_override_object()->replace(
304 $mocked_bind_param,
305 14 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
306 14 my ( $sth, $bind, $val, $attr ) = @_;
307  
308 14 ▶ if ( $bind =~ m/^:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bind =~ /^:/) { } 10 4 14
309 10 $self->{bind_named_params}->{$bind} = $val;
310 }
311 else {
312 4 $self->{bind_parmas}->[ $bind - 1 ] = $val;
313 }
314  
315 14 return $orig_mocked_bind_param->( $sth, $bind, $val, $attr );
316 }
317 12 );
318  
319 12 return $self;
320 }
321  
322 12 ▶sub _override_dbi_mocked_bind_param_in_out {
Subroutine _override_dbi_mocked_bind_param_in_out: called CC=1 SCAR=0.0
323 12 my $self = shift;
324 12 my $mocked_bind_param_in_out = shift;
325  
326 12 my $orig_mocked_bind_param = \&$mocked_bind_param_in_out;
327  
328 $self->get_override_object->replace(
329 $mocked_bind_param_in_out,
330 6 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
331 6 my ( $sth, $param_num, $val, $max_len ) = @_;
332  
333 6 ▶ if ( $param_num =~ m/^:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_num =~ /^:/) { } 2 4 6
334 2 $self->{bind_named_params}->{$param_num} = $ref_cursor;
335 }
336 else {
337 4 $self->{bind_parmas}->[ $param_num - 1 ] = $ref_cursor;
338 }
339  
340 6 return $orig_mocked_bind_param->( $sth, $param_num, $ref_cursor, $max_len );
341 }
342 12 );
343  
344 12 return $self;
345 }
346  
347 9 ▶sub _override_dbi_bind_param {
Subroutine _override_dbi_bind_param: called CC=1 SCAR=0.2
348 9 my $self = shift;
349 9 my $bind_param = shift;
350  
351 9 my $orig_execute = \&$bind_param;
352  
353 $self->get_override_object()->replace(
354 $bind_param,
355 6 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=15.5
356 6 my ( $sth, $bind, $val, $attr ) = @_;
357 6 ▶ return if !$bind && !$val;
Branch: 1 branch
BranchTrueFalseTotal
unless $bind or $val 0 6 6
Condition33%1/3
$bind or $val
l!l&&r!l&&!r
600
MC/DC0%0/2
$bind or $val
$bind $val
Truth table33%1/3
A or B
A= $bindB= $val
ABresult
1X1
011
000
358 6 ▶ if ( $bind =~ m/^:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bind =~ /^:/) { } 6 0 6
359 6 $self->{bind_named_params}->{$bind} = $val;
360 }
361 else {
362 0 $self->{bind_parmas}->[ $bind - 1 ] = $val;
363 }
364  
365 6 my $retval = $orig_execute->( $sth, $bind, $val, $attr );
366 6 return $retval;
367 }
368 9 );
369  
370 9 return $self;
371 }
372  
373 9 ▶sub _override_bind_param_in_out {
Subroutine _override_bind_param_in_out: called CC=1 SCAR=2.1
374 9 my $self = shift;
375 9 my $bind_param_in_out = shift;
376  
377 9 my $orig_bind_param_in_out = \&$bind_param_in_out;
378  
379 $self->get_override_object()->replace(
380 $bind_param_in_out,
381 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
382 0 my ( $sth, $bind, $val, $max_length, $attr ) = @_;
383  
384 0 ▶ if ( $bind =~ m/^:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bind =~ /^:/) { } 0 0 0
385 0 $self->{bind_named_params}->{$bind} = '<CURSOR>';
386 }
387 else {
388 0 $self->{bind_parmas}->[ $bind - 1 ] = '<CURSOR>';
389 }
390  
391 0 my $retval = $orig_bind_param_in_out->( $sth, $bind, $val, $max_length, $attr );
392 0 return $retval;
393 }
394 9 );
395  
396 9 return $self;
397 }
398  
399 9 ▶sub _override_dbi_fetchrow_hashref {
Subroutine _override_dbi_fetchrow_hashref: called CC=1 SCAR=0.0
400 9 my $self = shift;
401 9 my $fetchrow_hashref = shift;
402  
403 9 my $orig_selectrow_hashref = \&$fetchrow_hashref;
404  
405 $self->get_override_object()->replace(
406 $fetchrow_hashref,
407 17 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
408 17 my ($sth) = @_;
409  
410 17 my $retval;
411 17 ▶ if ( !$DBI::errstr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DBI::errstr) 17 0 17
412 17 $retval = $orig_selectrow_hashref->($sth);
413 }
414  
415 17 $self->{result}->[-1]->{col_names} = $sth->{NAME};
416 17 ▶ if ( ref $retval && !defined $self->{result}->[-1]->{results} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $retval and not defined $self->{"result"}[-1]{"results"}) 2 15 17
Condition100%3/3
ref $retval and not defined $self->{"result"}[-1]{"results"}
!ll&&!rl&&r
692
MC/DC100%2/2
ref $retval and not defined $self->{"result"}[-1]{"results"}
ref $retval not defined $self->{"result"}[-1]{"results"}
Truth table100%3/3
A and not(B)
A= ref $retvalB= not defined $self->{"result"}[-1]{"results"}
ABresult
0X0
100
111
417 2 my $query_results = $self->_set_hashref_response( $sth, $retval );
418 2 push @{ $self->{result}->[-1]->{results} }, $query_results;
419 2 $self->_write_to_file();
420 }
421  
422 17 return $retval;
423 }
424 9 );
425  
426 9 return $self;
427 }
428  
429 9 ▶sub _override_dbi_fetchrow_arrayref {
Subroutine _override_dbi_fetchrow_arrayref: called CC=1 SCAR=0.0
430 9 my $self = shift;
431 9 my $fetchrow_arrayref = shift;
432  
433 9 my $orig_selectrow_arrayref = \&$fetchrow_arrayref;
434  
435 $self->get_override_object()->replace(
436 $fetchrow_arrayref,
437 17 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
438 17 my ($sth) = @_;
439  
440 17 my $retval = $orig_selectrow_arrayref->($sth);
441 17 $self->{result}->[-1]->{col_names} = $sth->{NAME};
442 17 my @retval = ();
443 17 ▶ if ( ref $retval ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $retval) 12 5 17
444 12 @retval = @{$retval};
445 12 push @{ $self->{result}->[-1]->{results} }, \@retval;
446 12 $self->_write_to_file();
447 }
448  
449 17 return $retval;
450 }
451 9 );
452  
453 9 return $self;
454 }
455  
456 9 ▶sub _override_dbi_fetchrow_array {
Subroutine _override_dbi_fetchrow_array: called CC=1 SCAR=0.0
457 9 my $self = shift;
458 9 my $fetchrow_array = shift;
459  
460 9 my $orig_selectrow_array = \&$fetchrow_array;
461  
462 $self->get_override_object()->replace(
463 $fetchrow_array,
464 13 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
465 13 my ($sth) = @_;
466  
467 13 my @retval = $orig_selectrow_array->($sth);
468 13 $self->{result}->[-1]->{col_names} = $sth->{NAME};
469  
470 13 ▶ if ( scalar @retval ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @retval) 9 4 13
471 9 push @{ $self->{result}->[-1]->{results} }, \@retval;
472 9 $self->_write_to_file();
473 }
474  
475 13 return @retval;
476 }
477 9 );
478  
479 9 return $self;
480 }
481  
482 9 ▶sub _override_dbi_selectall_arrayref {
Subroutine _override_dbi_selectall_arrayref: called CC=1 SCAR=0.0
483 9 my $self = shift;
484 9 my $selectall_arrayref = shift;
485  
486 9 my $result = $self->{result};
487 9 my $orig_selectall_arrayref = \&$selectall_arrayref;
488  
489 $self->get_override_object()->replace(
490 $selectall_arrayref,
491 3 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
492 3 my ( $dbh, $sql, $slice, @parmas ) = @_;
493  
494 3 my $retval = $orig_selectall_arrayref->( $dbh, $sql, $slice, @parmas );
495 3 my $data = [];
496  
497 3 ▶ if ( ref $retval ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $retval) 3 0 3
498 3 my $col_names = $self->_get_current_record_column_names();
499  
500 3 foreach my $row_as_hash ( @{$retval} ) {
501 5 my $row_as_array = [];
502 5 foreach my $col_name ( @{$col_names} ) {
503 10 push @{$row_as_array}, $row_as_hash->{$col_name};
504 }
505  
506 5 push @{$data}, $row_as_array;
507 }
508 3 $self->{result}->[-1]->{results} = $data;
509 3 $self->_write_to_file();
510 }
511  
512 3 return $retval;
513 }
514 9 );
515  
516 9 return $self;
517 }
518  
519 9 ▶sub _override_dbi_selectall_hashref {
Subroutine _override_dbi_selectall_hashref: called CC=1 SCAR=0.0
520 9 my $self = shift;
521 9 my $selectall_hashref = shift;
522  
523 9 my $orig_selectall_hashref = \&$selectall_hashref;
524  
525 $self->get_override_object()->replace(
526 $selectall_hashref,
527 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
528 3 my ( $dbh, $statement, $key_field, $attr, @bind_values ) = @_;
529  
530 3 my $retval = $orig_selectall_hashref->( $dbh, $statement, $key_field, $attr, @bind_values );
531  
532 3 my $col_names = $self->_get_current_record_column_names();
533 3 my $mock_data = [];
534  
535 45 ▶ walk sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
536 45 my $rows = $_;
537 45 ▶ if ( ref $rows && scalar keys %{$rows} == scalar @{$col_names} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rows and scalar keys %{$rows;} == scalar @{$col_names;}) 6 39 45
Condition100%3/3
ref $rows and scalar keys %{$rows;} == scalar @{$col_names;}
!ll&&!rl&&r
3366
MC/DC100%2/2
ref $rows and scalar keys %{$rows;} == scalar @{$col_names;}
ref $rows scalar keys %{$rows;} == scalar @{$col_names;}
Truth table100%3/3
A and B
A= ref $rowsB= scalar keys %{$rows;} == scalar @{$col_names;}
ABresult
0X0
100
111
538 6 my %data = %$rows;
539 6 push @{$mock_data}, [ @data{ @{$col_names} } ];
540 6 $self->_write_to_file();
541 }
542  
543 45 return;
544 3 }, $retval;
545  
546 3 $self->{result}->[-1]->{results} = $mock_data;
547 3 return $retval;
548 }
549 9 );
550  
551 9 return $self;
552 }
553  
554 9 ▶sub _override_dbi_selectcol_arrayref {
Subroutine _override_dbi_selectcol_arrayref: called CC=1 SCAR=0.0
555 9 my $self = shift;
556 9 my $selectcol_arrayref = shift;
557  
558 9 my $orig_selectcol_arrayref = \&$selectcol_arrayref;
559  
560 $self->get_override_object()->replace(
561 $selectcol_arrayref,
562 3 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
563 3 my ( $dbh, $statement, $attr, @bind_values ) = @_;
564 3 my $mocked_data = [];
565  
566 3 my $retval = $orig_selectcol_arrayref->( $dbh, $statement, $attr, @bind_values );
567 3 my @db_data = @{$retval};
568  
569 3 my $length = 1;
570 3 ▶ $length = scalar @{ $attr->{Columns} }
Branch: 1 branch
BranchTrueFalseTotal
if $attr and ref $attr eq "HASH" 2 1 3
Condition66%2/3
$attr and ref $attr eq "HASH"
!ll&&!rl&&r
102
MC/DC50%1/2
$attr and ref $attr eq "HASH"
$attr ref $attr eq "HASH"
Truth table66%2/3
A and B
A= $attrB= ref $attr eq "HASH"
ABresult
0X0
100
111
571 if $attr && ref $attr eq 'HASH';
572  
573 3 foreach my $row ( 0 .. $#db_data ) {
574 10 my $query_data = [ splice( @db_data, 0, $length ) ];
575 10 ▶ last if scalar @{$query_data} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$query_data;} == 0 1 9 10
576 9 push @{$mocked_data}, $query_data;
577 }
578  
579 3 $self->{result}->[-1]->{results} = $mocked_data;
580 3 $self->_write_to_file();
581 3 return $retval;
582 }
583 9 );
584  
585 9 return $self;
586 }
587  
588 9 ▶sub _override_dbi_selectrow_array {
Subroutine _override_dbi_selectrow_array: called CC=1 SCAR=0.0
589 9 my $self = shift;
590 9 my $selectrow_array = shift;
591  
592 9 my $original_selectrow_array = \&$selectrow_array;
593  
594 $self->get_override_object()->replace(
595 $selectrow_array,
596 6 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
597 6 my ( $dbh, $statement, $attr, @bind_values ) = @_;
598 6 my $sth;
599  
600 6 ▶ if ( !ref $statement ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $statement) { } 4 2 6
601 4 $sth = $dbh->prepare($statement);
602 }
603 else {
604 2 $sth = $statement;
605 }
606  
607 6 my $sql = $sth->{Statement};
608 6 my @retval = $original_selectrow_array->( $dbh, $statement, $attr, @bind_values );
609  
610 my $query_data = {
611 statement => $sql,
612 bound_params => \@bind_values,
613 col_names => $sth->{NAME},
614 6 results => [ \@retval ],
615 };
616  
617 6 push @{ $self->{result} }, $query_data;
618  
619 6 $self->_write_to_file();
620 6 return @retval;
621 }
622 9 );
623  
624 9 return $self;
625 }
626  
627 9 ▶sub _override_dbi_selectrow_arrayref {
Subroutine _override_dbi_selectrow_arrayref: called CC=1 SCAR=0.0
628 9 my $self = shift;
629 9 my $selectrow_arrayref = shift;
630  
631 9 my $original_selectrow_arrayref = \&$selectrow_arrayref;
632  
633 $self->get_override_object()->replace(
634 $selectrow_arrayref,
635 6 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
636 6 my ( $dbh, $statement, $attr, @bind_values ) = @_;
637 6 my $sth;
638  
639 6 ▶ if ( !ref $statement ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $statement) { } 4 2 6
640 4 $sth = $dbh->prepare($statement);
641 }
642 else {
643 2 $sth = $statement;
644 }
645  
646 6 my $sql = $sth->{Statement};
647 6 my $retval = $original_selectrow_arrayref->( $dbh, $statement, $attr, @bind_values );
648  
649 my $query_data = {
650 statement => $sql,
651 bound_params => \@bind_values,
652 col_names => $sth->{NAME},
653 6 results => [$retval],
654 };
655  
656 6 push @{ $self->{result} }, $query_data;
657 6 $self->_write_to_file();
658  
659 6 return $retval;
660 }
661 9 );
662  
663 9 return $self;
664 }
665  
666 9 ▶sub _override_dbi_selectrow_hashref {
Subroutine _override_dbi_selectrow_hashref: called CC=1 SCAR=0.0
667 9 my $self = shift;
668 9 my $selectrow_hashref = shift;
669  
670 9 my $original_selectrow_hashref = \&$selectrow_hashref;
671  
672 $self->get_override_object()->replace(
673 $selectrow_hashref,
674 6 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
675 6 my ( $dbh, $statement, $attr, @bind_values ) = @_;
676 6 my $sth;
677  
678 6 ▶ if ( !ref $statement ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $statement) { } 4 2 6
679 4 $sth = $dbh->prepare($statement);
680 }
681 else {
682 2 $sth = $statement;
683 }
684  
685 6 my $sql = $sth->{Statement};
686 6 my $retval = $original_selectrow_hashref->( $dbh, $statement, $attr, @bind_values );
687  
688 $self->{result}->[-1]->{results} =
689 6 [ $self->_set_hashref_response( $sth, $retval ) ];
690  
691 6 return $retval;
692 }
693 9 );
694  
695 9 return $self;
696 }
697  
698 9 ▶sub _override_dbi_fetch {
Subroutine _override_dbi_fetch: called CC=1 SCAR=0.0
699 9 my $self = shift;
700 9 my $fetch = shift;
701  
702 9 my $original_fetch = \&$fetch;
703 9 my $result = [];
704 $self->get_override_object()->replace(
705 $fetch,
706 59 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
707 59 my ( $sth, @args ) = @_;
708 59 my $row = $original_fetch->( $sth, @args );
709 59 ▶ my $sql = $sth->{Statement} // $sth->{Database}->{Statement} // '';
Condition40%2/5
($sth->{"Statement"} // $sth->{"Database"}{"Statement"}) // ""
l!l
590
$sth->{"Statement"} // $sth->{"Database"}{"Statement"}
l!l&&r!l&&!r
5900
MC/DC0%0/2
($sth->{"Statement"} // $sth->{"Database"}{"Statement"}) // ""
$sth->{"Statement"} $sth->{"Database"}{"Statement"}
Truth table33%1/3
A // B
A= $sth->{"Statement"}B= $sth->{"Database"}{"Statement"}
ABresult
1X1
011
000
710 59 ▶ if ( ref $row ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row) 39 20 59
711 39 my @shallow_copy = @{$row};
712 39 push @{ $self->{result}->[-1]->{results} }, \@shallow_copy;
713 39 $self->_write_to_file();
714 }
715  
716 59 return $row;
717 }
718 9 );
719  
720 9 return $self;
721 }
722  
723 9 ▶sub _override_dbi_prepare_cached {
Subroutine _override_dbi_prepare_cached: called CC=1 SCAR=0.0
724 9 my $self = shift;
725 9 my $prepare_cached = shift;
726  
727 9 my $original_prepare_cached = \&$prepare_cached;
728 $self->get_override_object()->replace(
729 $prepare_cached,
730 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
731 4 my ( $dbh, $sql, $attr, $if_active ) = @_;
732  
733 4 $sql = $self->_normalize_sql($sql);
734 4 return $original_prepare_cached->( $dbh, $sql, $attr, $if_active );
735 }
736 9 );
737 9 return $self;
738 }
739  
740 9 ▶sub _override_dbi_prepare {
Subroutine _override_dbi_prepare: called CC=1 SCAR=0.0
741 9 my $self = shift;
742 9 my $prepare = shift;
743  
744 9 my $original_prepare = \&$prepare;
745 $self->get_override_object()->replace(
746 $prepare,
747 70 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
748 70 my ( $dbh, $sql, $attr ) = @_;
749  
750 70 $sql = $self->_normalize_sql($sql);
751 70 return $original_prepare->( $dbh, $sql, $attr );
752 }
753 9 );
754  
755 9 return $self;
756 }
757  
758 12 ▶sub _override_dbi_mocked_prepare {
Subroutine _override_dbi_mocked_prepare: called CC=1 SCAR=0.0
759 12 my $self = shift;
760 12 my $mocked_prepare = shift;
761  
762 12 my $original_mocked_prepare = \&$mocked_prepare;
763 $self->get_override_object()->replace(
764 $mocked_prepare,
765 56 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
766 56 my ( $dbh, $sql ) = @_;
767  
768 56 $sql = $self->_normalize_sql($sql);
769 56 return $original_mocked_prepare->( $dbh, $sql );
770 }
771 12 );
772  
773 12 return $self;
774 }
775  
776 9 ▶sub _override_dbi_begin_work {
Subroutine _override_dbi_begin_work: called CC=1 SCAR=0.0
777 9 my $self = shift;
778 9 my $mocked_begin_work = shift;
779  
780 9 my $original_begin_work = \&$mocked_begin_work,;
781 $self->get_override_object()->replace(
782 $mocked_begin_work,
783 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
784 2 my $dbh = shift;
785  
786 2 push @{ $self->{result} },
787 {
788 statement => 'BEGIN WORK',
789 col_names => undef,
790 results => [ [] ]
791 };
792 2 $self->_write_to_file();
793 2 return $original_begin_work->($dbh);
794 }
795 9 );
796 }
797  
798 9 ▶sub _override_dbi_commit {
Subroutine _override_dbi_commit: called CC=1 SCAR=0.0
799 9 my $self = shift;
800 9 my $mocked_commit = shift;
801  
802 9 my $original_commit = \&$mocked_commit;
803 $self->get_override_object()->replace(
804 $mocked_commit,
805 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
806 2 my $dbh = shift;
807  
808 2 push @{ $self->{result} }, { statement => 'COMMIT', col_names => undef, results => [ [] ] };
809 2 $self->_write_to_file();
810 2 return $original_commit->($dbh);
811 }
812 9 );
813 }
814  
815 9 ▶sub _override_dbi_rollback {
Subroutine _override_dbi_rollback: called CC=1 SCAR=1.6
816 9 my $self = shift;
817 9 my $mocked_rollback = shift;
818  
819 9 my $original_rollback = \&$mocked_rollback;
820 $self->get_override_object()->replace(
821 $mocked_rollback,
822 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
823 0 my $dbh = shift;
824  
825 0 push @{ $self->{result} },
826 {
827 statement => 'ROLLBACK',
828 col_names => undef,
829 results => [ [] ]
830 };
831 0 $self->_write_to_file();
832 0 return $original_rollback->($dbh);
833 }
834 9 );
835 }
836  
837 187 ▶sub _normalize_sql {
Subroutine _normalize_sql: called CC=1 SCAR=0.0
838 187 my ( $self, $sql ) = @_;
839  
840 # 1. remove multi-linie comments /* ... */
841 187 $sql =~ s/\/\*.*?\*\///gs;
842  
843 # 2. remove single-line comments -- ...
844 187 $sql =~ s/--.*$//gm;
845  
846 187 $sql =~ s/\s+/ /g;
847 187 $sql =~ s/^\s+|\s+$//g;
848  
849 187 return $sql;
850 }
851  
852 6 ▶sub _get_current_record_column_names {
Subroutine _get_current_record_column_names: called CC=1 SCAR=0.0
853 6 my $self = shift;
854  
855 6 return $self->{result}->[-1]->{col_names};
856 }
857  
858 12 ▶sub _process_mock_data {
Subroutine _process_mock_data: called CC=5 SCAR=16.1
859 12 my ( $self, $data ) = @_;
860  
861 12 ▶ while ( my ( $index, $row ) = each( @{$data} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @{$data;}) 48 12 60
862  
863 # load in the mocked session the a global scalar ref for the cursor
864 # use the same global scalar in the override for DBD::st::bind_param_inout
865 48 ▶ $row->{bound_params} = [ map { defined $_ && $_ eq '<CURSOR>' ? $ref_cursor : $_ } @{ $row->{bound_params} } ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && $_ eq '<CURSOR>' ? : 4 102 106
Condition100%3/3
defined $_ and $_ eq "<CURSOR>"
!ll&&!rl&&r
21004
MC/DC100%2/2
defined $_ and $_ eq "<CURSOR>"
defined $_ $_ eq "<CURSOR>"
Truth table100%3/3
A and B
A= defined $_B= $_ eq "<CURSOR>"
ABresult
0X0
100
111
866 48 ▶ if ( $row->{col_names} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"col_names"}) 37 11 48
867 37 my $cols = delete $row->{col_names};
868 37 unshift @{ $row->{results} }, $cols;
869 }
870 }
871  
872 12 return $self;
873 }
874  
875 22 ▶sub _set_fixtures_file {
Subroutine _set_fixtures_file: called CC=2 SCAR=6.9
876 22 my $self = shift;
877 22 my $file = shift;
878  
879 22 Readonly::Scalar my $FIXTURE_DIR => 'db_fixtures/';
880  
881 22 ▶ if ( defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) { } 8 14 22
882 8 $self->{fixture_file} = $file;
883 }
884 else {
885 14 my ( $volume, $directory, $test_file ) = File::Spec->splitpath($PROGRAM_NAME);
886 14 make_path( $directory . $FIXTURE_DIR );
887 14 my $default_fixture_file = $directory . $FIXTURE_DIR . "$test_file.json";
888 14 $self->{fixture_file} = $default_fixture_file;
889 }
890  
891 22 return $self;
892 }
893  
894 23 ▶sub _validate_args {
Subroutine _validate_args: called CC=5 SCAR=16.1
895 23 my $self = shift;
896 23 my $args_for = shift;
897  
898 23 ▶ croak 'arguments to new must be hashref'
Branch: 1 branch
BranchTrueFalseTotal
if ref $args_for ne "HASH" 1 22 23
899 if ref $args_for ne 'HASH';
900  
901 22 Readonly::Hash my %ALLOWED_KEYS => (
902 dbh => 1,
903 file => 1,
904 data => 1,
905 override => 1,
906 );
907  
908 22 ▶ croak 'to many args to new' if scalar keys %{$args_for} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$args_for;} > 1 1 21 22
909  
910 21 foreach my $key ( keys %{$args_for} ) {
911 croak "Key not allowed: $key"
912 21 ▶ unless $ALLOWED_KEYS{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $ALLOWED_KEYS{$key} 1 20 21
913 }
914  
915 20 return $self;
916 }
917  
918 147 ▶sub _write_to_file {
Subroutine _write_to_file: called CC=7 SCAR=20.3
919 147 my $self = shift;
920  
921 147 my $result = $self->{result};
922 147 my $override_flag = $self->{override_flag};
923 147 my $fixture_file = $self->{fixture_file};
924  
925 147 ▶ return unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 147 147
926 147 ▶ return unless $override_flag;
Branch: 1 branch
BranchTrueFalseTotal
unless $override_flag 0 147 147
927  
928 147 ▶ if ( $override_flag && scalar @{$result} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($override_flag and scalar @{$result;}) 147 0 147
Condition50%1/2
$override_flag and scalar @{$result;}
!ll
0147
MC/DC0%0/1
$override_flag and scalar @{$result;}
$override_flag
Truth table50%1/2
A
A= $override_flag
Aresult
00
11
929 147 my $json_data = $JSON_OBJ->encode($result);
930 147 ▶ my $fh = IO::File->new( $fixture_file, 'w' )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($fixture_file, "w") or croak("cannot open file:$fixture_file $!\n") 147 0 147
931 or croak "cannot open file:$fixture_file $!\n";
932 147 say $fh $json_data;
933 147 ▶ $fh->close or croak "cannot close file:$fixture_file $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or croak("cannot close file:$fixture_file $!\n") 147 0 147
934 147 undef $fh;
935 }
936  
937 147 return $self;
938 }
939  
940 8 ▶sub _set_hashref_response {
Subroutine _set_hashref_response: called CC=2 SCAR=6.9
941 8 my $self = shift;
942 8 my $sth = shift;
943 8 my $retval = shift;
944  
945 8 my $result = [];
946 8 my $cols = $sth->{NAME};
947 8 foreach my $col ( @{$cols} ) {
948 16 push @{$result}, $retval->{$col};
949 }
950  
951 8 return $result;
952 }
953  
954 5 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
955 5 my $self = shift;
956  
957 5 my $result = delete $self->{result};
958 5 my $override_flag = delete $self->{override_flag};
959  
960 5 $override = delete $self->{override};
961 5 my $dbh = delete $self->{dbh};
962 5 my $fixture_file = delete $self->{fixture_file};
963  
964 5 return $self;
965 }
966  
967 18 1;
968  
969 =head1 NAME
970  
971 DBD::Mock::Session::GenerateFixtures - A module to generate fixtures for DBD::Mock::Session
972  
973 =head1 SYNOPSIS
974  
975 # Case 1: Providing a pre-existing DBI database handle for genereting a mocked data files
976 # with the test name
977 my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new({ dbh => $dbh });
978 my $real_dbh = $mock_dumper->get_dbh();
979  
980 # Case 2: Read data from the same file as current test
981 my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new();
982 my $dbh = $mock_dumper->get_dbh();
983 # Your code using the mock DBD
984  
985 # Case 3: Read data from a coustom file
986 my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new({ file => 'path/to/fixture.json' });
987 my $dbh = $mock_dumper->get_dbh();
988 # Your code using the mock DBD
989  
990 # Case 4: Providing an array reference containing mock data
991 my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new({ data => \@mock_data });
992 my $dbh = $mock_dumper->get_dbh();
993 # Your code using the mock DBD
994  
995 # or with Rose::DB
996  
997 my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new();
998  
999 my $override = Sub::Override->new();
1000 my $dbh = $mock_dumper->get_dbh();
1001 $dbh->{mock_start_insert_id} = 3;
1002  
1003 $override->replace('Rose::DB::dbh' => sub {return $dbh});
1004 $override->inject('DBD::Mock::db::last_insert_rowid', sub {$dbh->{mock_last_insert_id}});
1005  
1006 my $num_rows_updated = DB::Media::Manager->update_media(
1007 set => {
1008 location => '/data/music/claire_de_lune.ogg',
1009 },
1010 where => [
1011 id => 2,
1012 ],
1013 );
1014  
1015 # support for begin_work, autocommit and rollback
1016 my $login_history = DB::UserLoginHistory->new( user_id => 1 );
1017 $login_history->db()->dbh()->begin_work();
1018 try {
1019 $login_history->save();
1020 $login_history->db()->dbh()->commit();
1021 }
1022 catch {
1023 $login_history->db()->dbh()->rollback();
1024 };
1025  
1026 =head1 DESCRIPTION
1027  
1028 When a real DBI database handle ($dbh) is provided, the module generates C<DBD::Mock::Session> data and stores it in a JSON file.
1029 After the data is generated, remove the 'dbh' argument from the constructor, and it will use the previously generated data to create a 'DBD::Mock::Session' database handle.
1030 Mocked data can also be loaded from a custom file or as a data structure.
1031 This is not a part of the DBD::Mock::Session distribution; it's just a wrapper around it."
1032  
1033 =head1 METHODS
1034  
1035 =head2 new(\%args_for)
1036  
1037 Constructor method to create a new C<DBD::Mock::Session::GenerateFixtures> object.
1038  
1039 Accepts an optional hash reference C<\%args_for> with the following keys:
1040  
1041 =over 4
1042  
1043 =item * C<file>: File path to the fixture file containing mocked data.
1044  
1045 =item * C<data>: Reference to an array containing mock data.
1046  
1047 =item * C<dbh>: Database handle used for reading the data required to genereate a mocked dbh. This should used first time you are runnig the tests.
1048  
1049 =back
1050  
1051 =head2 get_dbh()
1052  
1053 Returns the mocked database handle object.
1054  
1055 =head2 get_override_object()
1056  
1057 Returns the override object used for mocking DBI methods.
1058  
1059 =head2 restore_all()
1060  
1061 Restores all overridden DBI methods to their original implementations.
1062  
1063 This method is used to revert all DBI method overrides set up for mocking database interactions back to their original implementations.
1064  
1065 Returns the current object.
1066  
1067 =head1 PRIVATE METHODS
1068  
1069 These methods are not intended to be called directly from outside the module.
1070  
1071 =head2 _initialize(\%args_for)
1072  
1073 Initializes the C<DBD::Mock::Session::GenerateFixtures> object with the provided arguments.
1074  
1075 =head2 _set_mock_dbh(\@data)
1076  
1077 Sets up the mocked database handle based on the provided data.
1078  
1079 =head2 _override_dbi_methods()
1080  
1081 Overrides various DBI methods for mocking database interactions.
1082  
1083 =head2 _override_dbi_execute($dbi_execute)
1084  
1085 Overrides the C<execute> method of C<DBI::st> in order to capture the sql statement, bound_params and column names.
1086  
1087 =head2 _override_dbi_bind_param($bind_param)
1088  
1089 Overrides the C<bind_param> method of C<DBI::st> in order to capture the bound params.
1090  
1091 =head2 _override_dbi_fetchrow_hashref($fetchrow_hashref)
1092  
1093 Overrides the C<fetchrow_hashref> method of C<DBI::st> in order to capture the rows returned.
1094  
1095 =head2 _override_dbi_fetchrow_arrayref($fetchrow_arrayref)
1096  
1097 Overrides the C<fetchrow_arrayref> method of C<DBI::st> in order to capture the rows returned.
1098  
1099 =head2 _override_dbi_fetchrow_array($fetchrow_array)
1100  
1101 Overrides the C<fetchrow_array> method of C<DBI::st> in order to capture the rows returned.
1102  
1103 =head2 _override_dbi_selectall_arrayref($selectall_arrayref)
1104  
1105 Overrides the C<selectall_arrayref> method of C<DBI::db> in order to capture the rows returned.
1106  
1107 =head2 _override_dbi_selectall_hashref($selectall_hashref)
1108  
1109 Overrides the C<selectall_hashref> method of C<DBI::db> in order to capture the rows returned.
1110  
1111 =head2 _override_dbi_selectcol_arrayref($selectcol_arrayref)
1112  
1113 Overrides the C<selectcol_arrayref> method of C<DBI::db> in order to capture the rows returned.
1114  
1115 =head2 _override_dbi_selectrow_array($selectrow_array)
1116  
1117 Overrides the C<selectrow_array> method of C<DBI::db> in order to capture the rows returned.
1118  
1119 =head2 _override_dbi_selectrow_arrayref($selectrow_arrayref)
1120  
1121 Overrides the C<selectrow_arrayref> method of C<DBI::db> in order to capture the rows returned.
1122  
1123 =head2 _override_dbi_selectrow_hashref($selectrow_hashref)
1124  
1125 Overrides the C<selectrow_hashref> method of C<DBI::db> in order to capture the rows returned.
1126  
1127 =head2 _override_dbi_fetch($sth, @args)
1128  
1129 Overrides the C<fetch> method of C<DBI::st>
1130  
1131 =head2 _override_dbi_prepare
1132  
1133 _override_dbi_prepare($prepare);
1134  
1135 This method overrides the `DBI::db::prepare` method. It customizes how SQL statements are prepared for execution
1136  
1137 =head2 _override_dbi_prepare_cached
1138  
1139 _override_dbi_prepare_cached($prepare_cached);
1140  
1141 This method overrides the `DBI::db::prepare_cached` method. It provides a mechanism for caching prepared statements to optimize repeated queries.
1142  
1143 =head2 _override_dbi_mocked_prepare
1144  
1145 _override_dbi_mocked_prepare($mocked_prepare);
1146  
1147 This method overrides the `DBD::Mock::db::prepare` method. It is used for testing purposes to mock the behavior of statement preparation.
1148  
1149 =head2 _override_dbi_begin_work
1150  
1151 _override_dbi_begin_work($dbi_begin_work);
1152  
1153 This method overrides the `DBD::Mock::db::begin` method. It is used for testing purposes to add in fxtures an 'BEGIN WORK' statement.
1154  
1155 =head2 _override_dbi_commit
1156  
1157 _override_dbi_commit($commit);
1158  
1159 This method overrides the `DBD::Mock::db::commit` method. It is used for testing purposes to add in fxtures an 'COMMIT' statement.
1160  
1161 =head2 _override_dbi_rollback
1162  
1163 _override_dbi_rollback($rollback);
1164  
1165 This method overrides the `DBD::Mock::db::rollback` method. It is used for testing purposes to add in fxtures an 'ROLLBACK' statement.
1166  
1167 =head2 _normalize_sql
1168  
1169 _normalize_sql($sql);
1170  
1171 This method normalizes an SQL query string by removing extra whitespace and trimming leading or trailing spaces.
1172  
1173 =head2 _get_current_record_column_names()
1174  
1175 Returns the column names of the current record being processed.
1176  
1177 =head2 _process_mock_data(\@data)
1178  
1179 Processes the mock data before setting up the mocked database handle.
1180  
1181 =head2 _set_fixtures_file($file)
1182  
1183 Sets the file path for the fixture file containing mocked data.
1184  
1185 =head2 _validate_args(\%args_for)
1186  
1187 Validates the arguments passed to the constructor.
1188  
1189 =head2 _write_to_file()
1190  
1191 Writes the current results to the fixture file if override flag is set.
1192  
1193 =head2 _set_hashref_response($sth, $retval)
1194  
1195 Sets the response for hash references fetched from the database.
1196  
1197 =head1 Support
1198  
1199 Bugs should be reported via the CPAN bug tracker at
1200  
1201 https://rt.cpan.org/Public/Bug/Report.html?Queue=DBD-Mock-Session-GenerateFixtures
1202  
1203 For other issues, contact the author.
1204  
1205 =head1 REPOSITORY
1206  
1207 L<DBD-Fixtures|https://github.com/DragosTrif/DBD-Fixtures>
1208  
1209 =head1 AUTHOR
1210  
1211 Dragos Trif <drd.trif@gmail.com>
1212  
1213 =head1 LICENSE
1214  
1215 This library is free software and may be distributed under the same terms
1216 as perl itself.
1217  
1218 =cut
↑ Summary