EasyFixture/Definition.pm

filter: Statementstmt 100% 132 / 132 Branchbran 98.4% 63 / 64 Conditioncond 77.7% 7 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 7 totaltotal 95.3% 226 / 237 CC 39 SCAR 18.0
CC
39
Cov
99%
__ANON__
12.0
_validate_required_objects
9.0
_validate_next
7.0
CRAP 6.0
↑ Summary EasyFixture.pm »
1 package DBIx::Class::EasyFixture::Definition;
2 10 $DBIx::Class::EasyFixture::Definition::VERSION = '0.13';
3 # ABSTRACT: Validate fixture definitions
4  
5 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Types::Standard qw(Str HashRef ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 has 'name' => (
14 is => 'ro',
15 isa => Str,
16 required => 1,
17 );
18  
19 10 has 'definition' => (
20 is => 'ro',
21 isa => HashRef,
22 required => 1,
23 );
24  
25 10 has 'fixtures' => (
26 is => 'ro',
27 isa => HashRef,
28 required => 1,
29 handles_via => 'Hash',
30 handles => {
31 fixture_exists => 'exists',
32 },
33 );
34  
35 10 has 'group' => (
36 is => 'ro',
37 isa => ArrayRef[Str],
38 );
39  
40 462 ▶around 'BUILDARGS' => sub {
Subroutine __ANON__: called CC=12 SCAR=24.9
41 462 my $orig = shift;
42 462 my $self = shift;
43 462 my $args = $_[0];
44 462 ▶ if ( 'HASH' ne ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" ne ref $_[0]) 16 446 462
45 16 $args = {@_};
46 }
47 462 ▶ if ( 'ARRAY' eq ref $args->{definition} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" eq ref $args->{"definition"}) 18 444 462
48 18 $args->{group} = $args->{definition};
49 18 $args->{definition} = {};
50 }
51 462 my $definition = $args->{definition};
52 462 ▶ if ( my $using = $definition->{using} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $using = $definition->{"using"}) 441 21 462
53 441 foreach my $attribute ( keys %$using ) {
54 812 my $value = $using->{$attribute};
55 812 my $ref = ref $value;
56 812 ▶ next if not $ref or blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
if not $ref or builtin::blessed($value) 783 29 812
Condition100%3/3
not $ref or builtin::blessed($value)
l!l&&r!l&&!r
51327029
MC/DC100%2/2
not $ref or builtin::blessed($value)
not $ref builtin::blessed($value)
Truth table100%3/3
not(A) or B
A= not $refB= builtin::blessed($value)
ABresult
1X1
011
000
57  
58 my @requires
59 29 ▶ = 'ARRAY' eq $ref ? @$value
Branch: 3 branches
BranchTrueFalseTotal
'ARRAY' eq $ref ? : 10 19 29
'HASH' eq $ref ? : 9 10 19
'SCALAR' eq $ref ? : 9 1 10
60 : 'HASH' eq $ref ? %$value
61 : 'SCALAR' eq $ref ? ( $$value => $attribute )
62 : croak(
63 "Unhandled reference type passed for $args->{name}.$attribute: $value"
64 );
65 28 ▶ unless ( 2 == @requires ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (2 == @requires) 1 27 28
66 1 croak("$args->{name}.$attribute malformed: @requires");
67 }
68 27 delete $using->{$attribute};
69 27 ▶ $definition->{requires} ||= {};
Condition50%1/2
$definition->{"requires"} ||= {}
l!l
027
MC/DC0%0/1
$definition->{"requires"} ||= {}
$definition->{"requires"}
Truth table50%1/2
A
A= $definition->{"requires"}
Aresult
11
00
70 27 $definition->{requires}{ $requires[0] } = $requires[1];
71 }
72 }
73 460 $self->$orig( dclone($args) );
74 10 };
75  
76 460 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
77 460 my $self = shift;
78  
79 460 ▶ if ( $self->group ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->group) { } 18 442 460
80 18 $self->_validate_group;
81 }
82 else {
83 442 $self->_validate_keys;
84 440 $self->_validate_class_and_data;
85 438 $self->_validate_next;
86 435 $self->_validate_required_objects;
87 }
88 }
89  
90 946 ▶sub resultset_class { shift->definition->{new} }
Subroutine resultset_class: called CC=1 SCAR=0.0
Pod undocumented
91 581 ▶sub constructor_data { shift->definition->{using} }
Subroutine constructor_data: called CC=1 SCAR=0.0
Pod undocumented
92 516 ▶sub next { shift->definition->{next} }
Subroutine next: called CC=1 SCAR=0.0
Pod undocumented
93 # used only internally to validate the fixture definitions
94 436 ▶sub requires { shift->definition->{requires} }
Subroutine requires: called CC=1 SCAR=0.0
Pod undocumented
95 # returns all requires configs which are not marked as `deferred`
96 147 ▶sub requires_pre {
Subroutine requires_pre: called CC=3 SCAR=11.0
Pod undocumented
97 147 my $self = shift;
98 147 my $requires = {};
99 147 ▶ REQUIRES: while( my ($parent, $methods) = each( %{ $self->definition->{requires} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->definition->{"requires"};}) 86 147 233
100 86 ▶ next REQUIRES if($methods->{deferred});
Branch: 1 branch
BranchTrueFalseTotal
if $methods->{"deferred"} 8 78 86
101 78 $requires->{$parent} = $methods;
102 }
103 147 return $requires;
104 }
105 # returns all requires configs which are marked as `deferred`
106 77 ▶sub requires_defer {
Subroutine requires_defer: called CC=3 SCAR=11.0
Pod undocumented
107 77 my $self = shift;
108 77 my $deferred = {};
109 77 ▶ DEFERRED: while( my ($parent, $methods) = each( %{ $self->definition->{requires} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->definition->{"requires"};}) 47 77 124
110 47 ▶ next DEFERRED unless($methods->{deferred});
Branch: 1 branch
BranchTrueFalseTotal
unless $methods->{"deferred"} 43 4 47
111 4 $deferred->{$parent} = $methods;
112 }
113 77 return $deferred;
114 }
115  
116 18 ▶sub _validate_group {
Subroutine _validate_group: called CC=3 SCAR=11.0
117 18 my $self = shift;
118 18 my $name = $self->name;
119 18 my @group = @{ $self->group }; # shallow copy currently ok
120 18 ▶ unless ( @group ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@group) 1 17 18
121 1 croak("Fixture '$name' defines an empty group");
122 }
123 17 ▶ if ( my @unknown = sort grep { ! $self->fixture_exists($_) } @group ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unknown) = sort(grep({not $self->fixture_exists($_);} @group))) 1 16 17
124 1 croak("Fixture '$name'.group had unknown fixtures: @unknown");
125 }
126  
127 }
128 442 ▶sub _validate_keys {
Subroutine _validate_keys: called CC=3 SCAR=11.0
129 442 my $self = shift;
130 442 my $name = $self->name;
131 442 my %definition = %{ $self->definition }; # shallow copy currently ok
132 442 ▶ unless ( keys %definition ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %definition) 1 441 442
133 1 croak("Fixture '$name' had no keys");
134 }
135 441 delete @definition{qw/group new using next requires/};
136 441 ▶ if ( my @unknown = sort keys %definition ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unknown) = sort(keys %definition)) 1 440 441
137 1 croak("Fixture '$name' had unknown keys: @unknown");
138 }
139 }
140  
141 440 ▶sub _validate_class_and_data {
Subroutine _validate_class_and_data: called CC=5 SCAR=16.1
142 440 my $self = shift;
143  
144 440 my $class = $self->resultset_class;
145 440 my $data = $self->constructor_data;
146  
147 440 ▶ if ( $class xor $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class xor $data) 2 438 440
Condition75%3/4
$class xor $data
l&&rl&&!r!l&&r!l&&!r
438110
MC/DC100%2/2
$class xor $data
$class $data
Truth table75%3/4
A xor B
A= $classB= $data
ABresult
110
101
011
000
148 2 ▶ my $found = $class ? 'new' : 'using';
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 1 1 2
149 2 ▶ my $missing = $class ? 'using' : 'new';
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 1 1 2
150 2 my $name = $self->name;
151 2 croak("Fixture '$name' had a '$found' without a '$missing'");
152 }
153 }
154  
155 438 ▶sub _validate_next {
Subroutine _validate_next: called CC=7 SCAR=19.5
156 438 my $self = shift;
157 438 my $next = $self->next;
158 438 ▶ return if not $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 255 183 438
159  
160 183 ▶ $next = [$next] unless 'ARRAY' eq ref $next;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $next 0 183 183
161 183 my $name = $self->name;
162 183 foreach my $child (@$next) {
163 206 ▶ if ( !defined $child ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child) 1 205 206
164 1 croak("Fixture '$name' had an undefined element in 'next'");
165 }
166 205 ▶ if ( ref $child ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $child) 1 204 205
167 1 croak("Fixture '$name' had non-string elements in 'next'");
168 }
169 204 ▶ unless ( $self->fixture_exists($child) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->fixture_exists($child)) 1 203 204
170 1 croak(
171 "Fixture '$name' lists a non-existent fixture in 'next': '$child'"
172 );
173 }
174 }
175 }
176  
177 435 ▶sub _validate_required_objects {
Subroutine _validate_required_objects: called CC=9 SCAR=22.0
178 435 my $self = shift;
179  
180 435 my $name = join '.' => $self->name, $self->resultset_class, 'requires';
181  
182 435 my $requires = $self->requires;
183 435 ▶ return if not $requires;
Branch: 1 branch
BranchTrueFalseTotal
unless $requires 216 219 435
184 219 ▶ unless ( 'HASH' eq ref $requires ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("HASH" eq ref $requires) 1 218 219
185 1 croak("$name does not appear to be a hashref");
186 }
187  
188 # XXX don't use a while loop here because we might rewrite requires() and
189 # that would break the iterator
190 218 foreach my $parent ( keys %$requires ) {
191 255 my $methods = $requires->{$parent};
192 255 ▶ unless ( $self->fixture_exists($parent) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->fixture_exists($parent)) 1 254 255
193 1 croak(
194 "Fixture '$name' requires a non-existent fixture '$parent'");
195 }
196 254 ▶ if ( !ref $methods ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $methods) 177 77 254
197  
198 # they used a single key and it matched
199 177 $self->definition->{requires}{$parent}
200 = { our => $methods, their => $methods };
201 177 next;
202 }
203 77 ▶ if ( my @bad_keys = grep { !/^(?:our|their|deferred)$/ } keys %$methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad_keys) = grep({not /^(?:our|their|deferred)$/;} keys %$methods)) 1 76 77
204 1 croak("'$name' had bad keys: @bad_keys");
205 }
206 76 ▶ unless ( exists $methods->{our} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $methods->{"our"}) 1 75 76
207 1 croak("'$name' requires 'our'");
208 }
209 75 ▶ unless ( exists $methods->{their} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $methods->{"their"}) 1 74 75
210 1 croak("'$name' requires 'their'");
211 }
212 }
213 }
214  
215 10 1;
216  
217 __END__
↑ Summary EasyFixture.pm »