EasyFixture/Definition.pm

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