EasyFixture.pm

filter: Statementstmt 98.0% 100 / 102 Branchbran 86.6% 26 / 30 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 90.9% 20 / 22 Podpod 62.5% 5 / 8 totaltotal 92.6% 164 / 177 CC 23 SCAR 12.8
CC
23
Cov
95%
load
8.0
_load
5.0
_get_object
5.0
CRAP 3.6
« EasyFixture/Definition.pm ↑ Summary EasyFixture/Tutorial.pm »
1 package DBIx::Class::EasyFixture;
2 10 $DBIx::Class::EasyFixture::VERSION = '0.13';
3 # ABSTRACT: Easy fixtures with DBIx::Class
4  
5 10 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Types::Standard qw(InstanceOf Bool HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use aliased 'DBIx::Class::EasyFixture::Definition';
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 'schema' => (
14 is => 'ro',
15 isa => InstanceOf['DBIx::Class::Schema'],
16 required => 1,
17 );
18 10 has '_in_transaction' => (
19 is => 'rw',
20 isa => Bool,
21 default => 0,
22 writer => '_set_in_transaction',
23 );
24 has '_cache' => (
25 is => 'ro',
26 isa => HashRef,
27 13 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 10 handles_via => 'Hash',
29 handles => {
30 _set_fixture => 'set',
31 _get_result => 'get',
32 _clear => 'clear',
33 is_loaded => 'exists',
34 },
35 );
36 10 has 'no_transactions' => (
37 is => 'ro',
38 isa => Bool,
39 default => 0,
40 );
41  
42 13 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
43 13 my $self = shift;
44  
45 # Creating a definition object validates them, so this tells us at
46 # construction time if all fixtures are valid.
47 13 $self->_get_definition_object($_) foreach $self->all_fixture_names;
48 }
49  
50 17 ▶sub load {
Subroutine load: called CC=8 SCAR=20.8
Pod documented
51 17 my ( $self, @fixtures ) = @_;
52 17 ▶ if ( not $self->no_transactions and not $self->_in_transaction ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_transactions or $self->_in_transaction) 13 4 17
Condition100%3/3
$self->no_transactions or $self->_in_transaction
l!l&&r!l&&!r
1313
MC/DC100%2/2
$self->no_transactions or $self->_in_transaction
$self->no_transactions $self->_in_transaction
Truth table100%3/3
A or B
A= $self->no_transactionsB= $self->_in_transaction
ABresult
1X1
011
000
53 13 $self->schema->txn_begin;
54 13 $self->_set_in_transaction(1);
55 }
56 17 my @dbic_objects;
57 17 foreach my $fixture (@fixtures) {
58 46 my $definition = $self->_get_definition_object($fixture);
59 46 ▶ if ( my $group = $definition->group ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $definition->group) { } 2 44 46
60 2 push @dbic_objects => $self->load(@$group);
61 }
62 else {
63 44 push @dbic_objects => $self->_load($definition);
64 }
65 }
66 17 ▶ return 1 if not defined wantarray or not @dbic_objects;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray and @dbic_objects 1 16 17
Condition66%2/3
defined wantarray and @dbic_objects
!ll&&!rl&&r
1016
MC/DC50%1/2
defined wantarray and @dbic_objects
defined wantarray @dbic_objects
Truth table66%2/3
A and B
A= defined wantarrayB= @dbic_objects
ABresult
0X0
100
111
67 16 ▶ return $dbic_objects[0] if not wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 13 3 16
68 3 return @dbic_objects;
69 }
70  
71 422 ▶sub _get_definition_object {
Subroutine _get_definition_object: called CC=1 SCAR=0.0
72 422 my ( $self, $fixture ) = @_;
73 return Definition->new(
74 { name => $fixture,
75 definition => $self->get_definition($fixture),
76 422 fixtures => { map { $_ => 1 } $self->all_fixture_names },
77 }
78 );
79 }
80  
81 4 ▶sub get_result {
Subroutine get_result: called CC=2 SCAR=6.9
Pod documented
82 4 my ( $self, $fixture ) = @_;
83  
84 4 ▶ unless ( $self->is_loaded($fixture) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_loaded($fixture)) 1 3 4
85 1 carp("Fixture '$fixture' was never loaded");
86 1 return;
87 }
88 3 return $self->_get_result($fixture);
89 }
90  
91 77 ▶sub _get_object {
Subroutine _get_object: called CC=5 SCAR=16.1
92 77 my ( $self, $definition ) = @_;
93  
94 77 my $name = $definition->name;
95 77 my $object = $self->_get_result($name);
96 77 ▶ unless ($object) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object) 70 7 77
97 70 my $args = $definition->constructor_data;
98 70 ▶ if ( my $requires = $definition->requires_pre ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $requires = $definition->requires_pre) 70 0 70
99 70 ▶ while ( my ( $parent, $methods ) = each %$requires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$requires) 35 70 105
100 35 ▶ my $other = $self->_get_result($parent)
Branch: 1 branch
BranchTrueFalseTotal
my $other = $self->_get_result($parent) or croak("Panic: required object '${parent}' not loaded") 35 0 35
101 or croak("Panic: required object '$parent' not loaded");
102 35 my ( $our, $their ) = @{$methods}{qw/our their/};
103 35 $args->{$our} = $other->$their;
104 }
105 }
106 70 $object = $self->schema->resultset( $definition->resultset_class )
107 ->create( $definition->constructor_data );
108 70 $self->_set_fixture( $name, $object );
109 }
110 77 return $object;
111 }
112  
113 121 ▶sub _load {
Subroutine _load: called CC=5 SCAR=16.2
114 121 my ( $self, $definition ) = @_;
115  
116 # XXX This is our guard against circular definitions.
117 121 ▶ if ( $self->is_loaded( $definition->name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_loaded($definition->name)) 44 77 121
118 44 return $self->_get_result( $definition->name );
119 }
120  
121 77 ▶ if ( my $requires = $definition->requires_pre ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $requires = $definition->requires_pre) 77 0 77
122 77 $self->_load_previous_fixtures($requires);
123 }
124  
125 77 my $object = $self->_get_object($definition);
126  
127 77 ▶ if ( my $next = $definition->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $definition->next) 27 50 77
128 27 $self->_load_next_fixtures($next);
129 }
130  
131 77 ▶ if ( my $deferred = $definition->requires_defer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $deferred = $definition->requires_defer) 77 0 77
132 77 $self->_load_deferred_fixtures( $object, $deferred );
133 }
134 77 return $object;
135 }
136  
137 77 ▶sub _load_previous_fixtures {
Subroutine _load_previous_fixtures: called CC=2 SCAR=6.9
138 77 my ( $self, $requires ) = @_;
139  
140 77 foreach my $parent ( keys %$requires ) {
141 43 $self->_load( $self->_get_definition_object($parent) );
142 }
143 }
144  
145 27 ▶sub _load_next_fixtures {
Subroutine _load_next_fixtures: called CC=2 SCAR=6.9
146 27 my ( $self, $next ) = @_;
147  
148 27 foreach my $fixture (@$next) {
149 30 my $definition = $self->_get_definition_object($fixture);
150  
151 # XXX This is done when _load calls _load_previous_fixtures,
152 # so it is not necessary here
153 #
154 # # a fixture may say "load these other fixtures next", but if that
155 # # happens, those "next" fixtures may have different parent fixtures
156 # # that they require, so we load them here.
157 # if ( my $requires = $definition->requires_pre ) {
158 # while ( my ( $parent, $methods ) = each %$requires ) {
159 # $self->_load( $self->_get_definition_object($parent) )
160 # || croak("Panic: related object '$parent' not loaded");
161 # }
162 # }
163  
164 # ... and *now* we can load the fixture
165 30 $self->_load($definition);
166 }
167 }
168  
169 77 ▶sub _load_deferred_fixtures {
Subroutine _load_deferred_fixtures: called CC=2 SCAR=6.9
170 77 my ( $self, $object, $deferred) = @_;
171  
172 77 my $update_args = {};
173 77 ▶ while( my ($fixture, $methods) = each( %{$deferred} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$deferred;}) 4 77 81
174 4 my $other = $self->_load( $self->_get_definition_object($fixture) );
175 4 my ( $our, $their ) = @{$methods}{qw/our their/};
176 4 $update_args->{$our} = $other->$their;
177 }
178  
179 77 $object->update( $update_args );
180 }
181  
182 16 ▶sub unload {
Subroutine unload: called CC=3 SCAR=11.0
Pod documented
183 16 my $self = shift;
184 16 ▶ if ( not $self->no_transactions and $self->_in_transaction ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->no_transactions and $self->_in_transaction) { } 13 3 16
Condition100%3/3
not $self->no_transactions and $self->_in_transaction
!ll&&!rl&&r
2113
MC/DC100%2/2
not $self->no_transactions and $self->_in_transaction
not $self->no_transactions $self->_in_transaction
Truth table100%3/3
not(A) and B
A= not $self->no_transactionsB= $self->_in_transaction
ABresult
0X0
100
111
185 13 $self->schema->txn_rollback;
186 13 $self->_clear;
187 13 $self->_set_in_transaction(0);
188 }
189 else {
190 # XXX I don't think I really need this
191 #carp("finish() called without load()");
192 }
193 16 return 1;
194 }
195  
196 0 ▶sub all_fixture_names {
Subroutine all_fixture_names: not called CC=1 SCAR=6.9
Pod documented
197 0 croak("You must override all_fixture_names() in a subclass");
198 }
199  
200 0 ▶sub get_definition {
Subroutine get_definition: not called CC=1 SCAR=6.9
Pod documented
201 0 croak("You must override get_definition() in a subclass");
202 }
203  
204 7 ▶sub fixture_loaded { $_[0]->is_loaded( $_[1] ) }
Subroutine fixture_loaded: called CC=1 SCAR=0.0
Pod undocumented
205  
206 13 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod undocumented
207 13 my $self = shift;
208 13 $self->unload;
209 }
210  
211 10 1;
212  
213 __END__
« EasyFixture/Definition.pm ↑ Summary EasyFixture/Tutorial.pm »