EasyFixture/Definition.pm

filter: Statementstmt 100% 128 / 128 Branchbran 98.4% 63 / 64 Conditioncond 77.7% 7 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 7 totaltotal 95.2% 221 / 232 CC 39 SCAR 18.1
CC
39
Cov
99%
__ANON__
12.0
_validate_required_objects
9.0
_validate_next
7.0
CRAP 6.1
↑ Summary EasyFixture.pm »
1 package DBIx::Class::EasyFixture::Definition;
2 10 $DBIx::Class::EasyFixture::Definition::VERSION = '0.12';
3 # ABSTRACT: Validate fixture definitions
4  
5 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 has 'name' => (
13 is => 'ro',
14 isa => 'Str',
15 required => 1,
16 );
17  
18 10 has 'definition' => (
19 is => 'ro',
20 isa => 'HashRef',
21 required => 1,
22 );
23  
24 10 has 'fixtures' => (
25 traits => ['Hash'],
26 is => 'ro',
27 isa => 'HashRef',
28 required => 1,
29 handles => {
30 fixture_exists => 'exists',
31 },
32 );
33  
34 10 has 'group' => (
35 is => 'ro',
36 isa => 'ArrayRef[Str]',
37 );
38  
39 around 'BUILDARGS' => sub {
40 464 ▶ my $orig = shift;
Subroutine __ANON__: called CC=12 SCAR=24.9
41 464 my $self = shift;
42 464 my $args = $_[0];
43 464 ▶ if ( 'HASH' ne ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" ne ref $_[0]) 16 448 464
44 16 $args = {@_};
45 }
46 464 ▶ if ( 'ARRAY' eq ref $args->{definition} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" eq ref $args->{"definition"}) 18 446 464
47 18 $args->{group} = $args->{definition};
48 18 $args->{definition} = {};
49 }
50 464 my $definition = $args->{definition};
51 464 ▶ if ( my $using = $definition->{using} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $using = $definition->{"using"}) 443 21 464
52 443 foreach my $attribute ( keys %$using ) {
53 815 my $value = $using->{$attribute};
54 815 my $ref = ref $value;
55 815 ▶ next if not $ref or blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
if not $ref or builtin::blessed($value) 786 29 815
Condition100%3/3
not $ref or builtin::blessed($value)
l!l&&r!l&&!r
51527129
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
56  
57 my @requires
58 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
59 : 'HASH' eq $ref ? %$value
60 : 'SCALAR' eq $ref ? ( $$value => $attribute )
61 : croak(
62 "Unhandled reference type passed for $args->{name}.$attribute: $value"
63 );
64 28 ▶ unless ( 2 == @requires ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (2 == @requires) 1 27 28
65 1 croak("$args->{name}.$attribute malformed: @requires");
66 }
67 27 delete $using->{$attribute};
68 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
69 27 $definition->{requires}{ $requires[0] } = $requires[1];
70 }
71 }
72 462 $self->$orig( dclone($args) );
73 10 };
74  
75 sub BUILD {
76 462 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
77  
78 462 ▶ if ( $self->group ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->group) { } 18 444 462
79 18 $self->_validate_group;
80 }
81 else {
82 444 $self->_validate_keys;
83 442 $self->_validate_class_and_data;
84 440 $self->_validate_next;
85 437 $self->_validate_required_objects;
86 }
87 }
88  
89 950 ▶sub resultset_class { shift->definition->{new} }
Subroutine resultset_class: called CC=1 SCAR=0.0
Pod undocumented
90 583 ▶sub constructor_data { shift->definition->{using} }
Subroutine constructor_data: called CC=1 SCAR=0.0
Pod undocumented
91 519 ▶sub next { shift->definition->{next} }
Subroutine next: called CC=1 SCAR=0.0
Pod undocumented
92 # used only internally to validate the fixture definitions
93 438 ▶sub requires { shift->definition->{requires} }
Subroutine requires: called CC=1 SCAR=0.0
Pod undocumented
94 # returns all requires configs which are not marked as `deferred`
95 sub requires_pre {
96 148 ▶ my $self = shift;
Subroutine requires_pre: called CC=3 SCAR=11.0
Pod undocumented
97 148 my $requires = {};
98 148 ▶ REQUIRES: while( my ($parent, $methods) = each( %{ $self->definition->{requires} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->definition->{"requires"};}) 88 148 236
99 88 ▶ next REQUIRES if($methods->{deferred});
Branch: 1 branch
BranchTrueFalseTotal
if $methods->{"deferred"} 8 80 88
100 80 $requires->{$parent} = $methods;
101 }
102 148 return $requires;
103 }
104 # returns all requires configs which are marked as `deferred`
105 sub requires_defer {
106 78 ▶ my $self = shift;
Subroutine requires_defer: called CC=3 SCAR=11.0
Pod undocumented
107 78 my $deferred = {};
108 78 ▶ DEFERRED: while( my ($parent, $methods) = each( %{ $self->definition->{requires} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->definition->{"requires"};}) 49 78 127
109 49 ▶ next DEFERRED unless($methods->{deferred});
Branch: 1 branch
BranchTrueFalseTotal
unless $methods->{"deferred"} 45 4 49
110 4 $deferred->{$parent} = $methods;
111 }
112 78 return $deferred;
113 }
114  
115 sub _validate_group {
116 18 ▶ my $self = shift;
Subroutine _validate_group: called CC=3 SCAR=11.0
117 18 my $name = $self->name;
118 18 my @group = @{ $self->group }; # shallow copy currently ok
119 18 ▶ unless ( @group ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@group) 1 17 18
120 1 croak("Fixture '$name' defines an empty group");
121 }
122 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
123 1 croak("Fixture '$name'.group had unknown fixtures: @unknown");
124 }
125  
126 }
127 sub _validate_keys {
128 444 ▶ my $self = shift;
Subroutine _validate_keys: called CC=3 SCAR=11.0
129 444 my $name = $self->name;
130 444 my %definition = %{ $self->definition }; # shallow copy currently ok
131 444 ▶ unless ( keys %definition ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %definition) 1 443 444
132 1 croak("Fixture '$name' had no keys");
133 }
134 443 delete @definition{qw/group new using next requires/};
135 443 ▶ if ( my @unknown = sort keys %definition ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unknown) = sort(keys %definition)) 1 442 443
136 1 croak("Fixture '$name' had unknown keys: @unknown");
137 }
138 }
139  
140 sub _validate_class_and_data {
141 442 ▶ my $self = shift;
Subroutine _validate_class_and_data: called CC=5 SCAR=16.1
142  
143 442 my $class = $self->resultset_class;
144 442 my $data = $self->constructor_data;
145  
146 442 ▶ if ( $class xor $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class xor $data) 2 440 442
Condition75%3/4
$class xor $data
l&&rl&&!r!l&&r!l&&!r
440110
MC/DC100%2/2
$class xor $data
$class $data
Truth table75%3/4
A xor B
A= $classB= $data
ABresult
110
101
011
000
147 2 ▶ my $found = $class ? 'new' : 'using';
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 1 1 2
148 2 ▶ my $missing = $class ? 'using' : 'new';
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 1 1 2
149 2 my $name = $self->name;
150 2 croak("Fixture '$name' had a '$found' without a '$missing'");
151 }
152 }
153  
154 sub _validate_next {
155 440 ▶ my $self = shift;
Subroutine _validate_next: called CC=7 SCAR=19.5
156 440 ▶ my $next = $self->next or return;
Branch: 1 branch
BranchTrueFalseTotal
my $next = $self->next or return 256 184 440
157  
158 184 ▶ $next = [$next] unless 'ARRAY' eq ref $next;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $next 0 184 184
159 184 my $name = $self->name;
160 184 foreach my $child (@$next) {
161 208 ▶ if ( !defined $child ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child) 1 207 208
162 1 croak("Fixture '$name' had an undefined element in 'next'");
163 }
164 207 ▶ if ( ref $child ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $child) 1 206 207
165 1 croak("Fixture '$name' had non-string elements in 'next'");
166 }
167 206 ▶ unless ( $self->fixture_exists($child) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->fixture_exists($child)) 1 205 206
168 1 croak(
169 "Fixture '$name' lists a non-existent fixture in 'next': '$child'"
170 );
171 }
172 }
173 }
174  
175 sub _validate_required_objects {
176 437 ▶ my $self = shift;
Subroutine _validate_required_objects: called CC=9 SCAR=22.0
177  
178 437 my $name = join '.' => $self->name, $self->resultset_class, 'requires';
179  
180 437 ▶ my $requires = $self->requires or return;
Branch: 1 branch
BranchTrueFalseTotal
my $requires = $self->requires or return 217 220 437
181 220 ▶ unless ( 'HASH' eq ref $requires ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("HASH" eq ref $requires) 1 219 220
182 1 croak("$name does not appear to be a hashref");
183 }
184  
185 # XXX don't use a while loop here because we might rewrite requires() and
186 # that would break the iterator
187 219 foreach my $parent ( keys %$requires ) {
188 256 my $methods = $requires->{$parent};
189 256 ▶ unless ( $self->fixture_exists($parent) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->fixture_exists($parent)) 1 255 256
190 1 croak(
191 "Fixture '$name' requires a non-existent fixture '$parent'");
192 }
193 255 ▶ if ( !ref $methods ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $methods) 177 78 255
194  
195 # they used a single key and it matched
196 177 $self->definition->{requires}{$parent}
197 = { our => $methods, their => $methods };
198 177 next;
199 }
200 78 ▶ 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 77 78
201 1 croak("'$name' had bad keys: @bad_keys");
202 }
203 77 ▶ unless ( exists $methods->{our} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $methods->{"our"}) 1 76 77
204 1 croak("'$name' requires 'our'");
205 }
206 76 ▶ unless ( exists $methods->{their} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $methods->{"their"}) 1 75 76
207 1 croak("'$name' requires 'their'");
208 }
209 }
210 }
211  
212 10 __PACKAGE__->meta->make_immutable;
213  
214 10 1;
215  
216 __END__
217  
218 =pod
219  
220 =encoding UTF-8
221  
222 =head1 NAME
223  
224 DBIx::Class::EasyFixture::Definition - Validate fixture definitions
225  
226 =head1 VERSION
227  
228 version 0.12
229  
230 =head2 DESCRIPTION
231  
232 For internal use only. Maybe I'll document it some day.
233  
234 =head1 AUTHOR
235  
236 Curtis "Ovid" Poe <ovid@cpan.org>
237  
238 =head1 COPYRIGHT AND LICENSE
239  
240 This software is copyright (c) 2014 by Curtis "Ovid" Poe.
241  
242 This is free software; you can redistribute it and/or modify it under
243 the same terms as the Perl 5 programming language system itself.
244  
245 =cut
↑ Summary EasyFixture.pm »