lib/DBD/Mock/Session/GenerateFixtures.pm

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