blib/lib/Test/FixtureBuilder.pm

filter: Statementstmt 96.0% 98 / 102 Branchbran 50.0% 14 / 28 Conditioncond 27.2% 3 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 94.4% 17 / 18 Podpod 83.3% 5 / 6 totaltotal 80.1% 137 / 171 CC 24 SCAR 13.4
CC
24
Cov
82%
__ANON__
6.5
__ANON__
6.4
name_to_handle
6.0
CRAP 3.8
↑ Summary
1 package Test::FixtureBuilder;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 our $VERSION = '0.001';
6  
7 5 ▶use Exporter::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶sub after_import {
Subroutine after_import: called CC=1 SCAR=0.0
Pod undocumented
13 3 my $class = shift;
14 3 my ($importer, $specs) = @_;
15 3 $importer->FIXTURE_BUILDER_META->{class} = $class;
16 }
17  
18 3 ▶gen_default_export FIXTURE_BUILDER_META => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 3 my $meta = { class => undef };
20 3 ▶ return sub { $meta };
Subroutine __ANON__: called CC=1 SCAR=0.0
21 5 };
22  
23 1 ▶default_export fixture_db => sub {
Subroutine __ANON__: called CC=6 SCAR=18.6
24 1 my $caller = caller;
25 1 my ($name, $code) = @_;
26  
27 1 ▶ croak "'$caller' does not have metadata!"
Branch: 1 branch
BranchTrueFalseTotal
unless $caller->can("FIXTURE_BUILDER_META") 0 1 1
28 unless $caller->can('FIXTURE_BUILDER_META');
29  
30 1 my $meta = $caller->FIXTURE_BUILDER_META;
31  
32 croak "Cannot set db to '$name', already set to '$meta->{db}'"
33 1 ▶ if exists $meta->{db};
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta->{"db"} 0 1 1
34 1 $meta->{db} = $name;
35  
36 1 $meta->{builder} = $meta->{class}->new(%$meta);
37  
38 1 my $success = eval { $code->($meta->{builder}); 1 };
39 1 my $error = $@;
40  
41 1 delete $meta->{$_} for qw/db dbh builder/;
42  
43 1 ▶ die $error || "Unknown Error"
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 1 1
Condition0%0/2
$error || 'Unknown Error'
l!l
00
MC/DC0%0/1
$error || 'Unknown Error'
$error
Truth table0%0/2
A
A= $error
Aresult
11
00
44 unless $success;
45  
46 1 return $name;
47 5 };
48  
49 2 ▶default_export fixture_table => sub {
Subroutine __ANON__: called CC=6 SCAR=18.7
50 2 my $caller = caller;
51 2 my ($name, $code) = @_;
52  
53 2 ▶ croak "'$caller' does not have metadata!"
Branch: 1 branch
BranchTrueFalseTotal
unless $caller->can("FIXTURE_BUILDER_META") 0 2 2
54 unless $caller->can('FIXTURE_BUILDER_META');
55  
56 2 my $meta = $caller->FIXTURE_BUILDER_META;
57  
58 croak "Cannot set table to '$name', already set to '$meta->{table}'"
59 2 ▶ if exists $meta->{table};
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta->{"table"} 0 2 2
60 2 $meta->{table} = $name;
61  
62 2 my $success = eval { $code->($meta->{builder}, $name); 1 };
63 2 my $error = $@;
64  
65 2 delete $meta->{$_} for qw/table/;
66  
67 2 ▶ die $error || "Unknown Error"
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 2 2
Condition0%0/2
$error || 'Unknown Error'
l!l
00
MC/DC0%0/1
$error || 'Unknown Error'
$error
Truth table0%0/2
A
A= $error
Aresult
11
00
68 unless $success;
69  
70 2 return $name;
71 5 };
72  
73 6 ▶default_export fixture_row => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
74 6 my $caller = caller;
75  
76 6 ▶ croak "'$caller' does not have metadata!"
Branch: 1 branch
BranchTrueFalseTotal
unless $caller->can("FIXTURE_BUILDER_META") 0 6 6
77 unless $caller->can('FIXTURE_BUILDER_META');
78  
79 6 my $meta = $caller->FIXTURE_BUILDER_META;
80  
81 6 ▶ my $row = @_ > 1 ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 6 6
82  
83 6 return $meta->{builder}->insert_row( $meta->{table} => $row );
84 5 };
85  
86 0 ▶sub name_to_handle {
Subroutine name_to_handle: not called CC=2 SCAR=17.9
Pod documented
87 0 my $class = shift;
88 0 my ($name) = @_;
89  
90 0 ▶ return $name if blessed $name;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($name) 0 0 0
91  
92 0 croak "I don't know how to convert '$name' to a database handle, override name_to_handle()";
93 }
94  
95 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
96 2 my $class = shift;
97 2 my %params = @_;
98  
99 2 my $self = bless {}, $class;
100  
101 2 for my $field ( keys %params ) {
102 3 ▶ croak "'$field' is not a valid accessor for '$class'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($field) 0 3 3
103 unless $self->can($field);
104  
105 3 $self->$field($params{$field});
106 }
107  
108 2 return $self;
109 }
110  
111 2 ▶sub db {
Subroutine db: called CC=3 SCAR=11.2
Pod documented
112 2 my $self = shift;
113  
114 2 ▶ return $self->dbh
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
115 unless @_;
116  
117 2 my ($name) = @_;
118  
119 2 my $dbh = $self->name_to_handle($name);
120 2 ▶ croak "Could not get db handle for '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 2 2
121 unless $dbh;
122  
123 2 $self->dbh( $dbh );
124 }
125  
126 5 for my $field (qw/dbh class/) {
127 15 ▶ my $accessor = sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
128 15 my $self = shift;
129  
130 15 ▶ croak "Accessor '$field' called without instance!"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) and $self->isa(__PACKAGE__) 0 15 15
Condition33%1/3
builtin::blessed($self) and $self->isa(__PACKAGE__)
!ll&&!rl&&r
0015
MC/DC0%0/2
builtin::blessed($self) and $self->isa(__PACKAGE__)
builtin::blessed($self) $self->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($self)B= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
131 unless blessed $self && $self->isa( __PACKAGE__ );
132  
133 15 ▶ ($self->{$field}) = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 12 15
134  
135 15 return $self->{$field};
136 10 };
137 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
138 10 *$field = $accessor;
139 }
140  
141 12 ▶sub insert_row {
Subroutine insert_row: called CC=3 SCAR=11.0
Pod documented
142 12 my $self = shift;
143 12 my ($table, $row) = @_;
144  
145 12 ▶ my $dbh = $self->dbh || croak "No database handle set!";
Condition50%1/2
$self->dbh || croak('No database handle set!')
l!l
120
MC/DC0%0/1
$self->dbh || croak('No database handle set!')
$self->dbh
Truth table50%1/2
A
A= $self->dbh
Aresult
11
00
146  
147 12 my $quoted_table = $dbh->quote_identifier(undef, undef, $table);
148 12 my @vals = values %$row;
149 12 my $cols = join ',' => map { $dbh->quote_identifier($_) } keys %$row;
150 12 my $vals = join ',' => map { '?' } @vals;
151  
152 12 my $sth = $dbh->prepare("INSERT INTO $quoted_table ($cols) VALUES($vals)");
153 12 $sth->execute(@vals);
154  
155 12 ▶ return eval { $dbh->last_insert_id } || 0;
Condition50%1/2
eval { do { $dbh->last_insert_id } } || 0
l!l
120
MC/DC0%0/1
eval { do { $dbh->last_insert_id } } || 0
eval { do { $dbh->last_insert_id } }
Truth table50%1/2
A
A= eval { do { $dbh->last_insert_id } }
Aresult
11
00
156 }
157  
158 1 ▶sub insert_rows {
Subroutine insert_rows: called CC=2 SCAR=6.9
Pod documented
159 1 my $self = shift;
160 1 my ($table, @rows) = @_;
161  
162 1 $self->insert_row($table => $_) for @rows;
163 }
164  
165 5 1;
166  
167 __END__
↑ Summary