EasyFixture.pm

filter: Statementstmt 97.7% 87 / 89 Branchbran 93.7% 30 / 32 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 89.4% 17 / 19 Podpod 62.5% 5 / 8 totaltotal 93.2% 152 / 163 CC 24 SCAR 13.6
CC
24
Cov
96%
load
8.0
_load_next_fixtures
5.0
_get_object
5.0
CRAP 3.9
« EasyFixture/Definition.pm ↑ Summary EasyFixture/Tutorial.pm »
1 package DBIx::Class::EasyFixture;
2 9 $DBIx::Class::EasyFixture::VERSION = '0.11';
3 # ABSTRACT: Easy fixtures with DBIx::Class
4  
5 9 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use aliased 'DBIx::Class::EasyFixture::Definition';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 has 'schema' => (
12 is => 'ro',
13 isa => 'DBIx::Class::Schema',
14 required => 1,
15 );
16 9 has '_in_transaction' => (
17 is => 'rw',
18 isa => 'Bool',
19 default => 0,
20 writer => '_set_in_transaction',
21 );
22 has '_cache' => (
23 traits => ['Hash'],
24 is => 'ro',
25 isa => 'HashRef',
26 12 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 9 handles => {
28 _set_fixture => 'set',
29 _get_result => 'get',
30 _clear => 'clear',
31 is_loaded => 'exists',
32 },
33 );
34 9 has 'no_transactions' => (
35 is => 'ro',
36 isa => 'Bool',
37 default => 0,
38 );
39  
40 sub BUILD {
41 12 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
42  
43 # Creating a definition object validates them, so this tells us at
44 # construction time if all fixtures are valid.
45 12 $self->_get_definition_object($_) foreach $self->all_fixture_names;
46 }
47  
48 sub load {
49 16 ▶ my ( $self, @fixtures ) = @_;
Subroutine load: called CC=8 SCAR=20.8
Pod documented
50 16 ▶ if ( not $self->no_transactions and not $self->_in_transaction ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_transactions or $self->_in_transaction) 12 4 16
Condition100%3/3
$self->no_transactions or $self->_in_transaction
l!l&&r!l&&!r
1312
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
51 12 $self->schema->txn_begin;
52 12 $self->_set_in_transaction(1);
53 }
54 16 my @dbic_objects;
55 16 foreach my $fixture (@fixtures) {
56 43 my $definition = $self->_get_definition_object($fixture);
57 43 ▶ if ( my $group = $definition->group ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $definition->group) { } 2 41 43
58 2 push @dbic_objects => $self->load(@$group);
59 }
60 else {
61 41 push @dbic_objects => $self->_load($definition);
62 }
63 }
64 16 ▶ return 1 if not defined wantarray or not @dbic_objects;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray and @dbic_objects 1 15 16
Condition66%2/3
defined wantarray and @dbic_objects
!ll&&!rl&&r
1015
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
65 15 ▶ return $dbic_objects[0] if not wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 12 3 15
66 3 return @dbic_objects;
67 }
68  
69 sub _get_definition_object {
70 396 ▶ my ( $self, $fixture ) = @_;
Subroutine _get_definition_object: called CC=1 SCAR=0.0
71 return Definition->new(
72 { name => $fixture,
73 definition => $self->get_definition($fixture),
74 396 fixtures => { map { $_ => 1 } $self->all_fixture_names },
75 }
76 );
77 }
78  
79 sub get_result {
80 4 ▶ my ( $self, $fixture ) = @_;
Subroutine get_result: called CC=2 SCAR=6.9
Pod documented
81  
82 4 ▶ unless ( $self->is_loaded($fixture) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_loaded($fixture)) 1 3 4
83 1 carp("Fixture '$fixture' was never loaded");
84 1 return;
85 }
86 3 return $self->_get_result($fixture);
87 }
88  
89 sub _get_object {
90 72 ▶ my ( $self, $definition ) = @_;
Subroutine _get_object: called CC=5 SCAR=16.1
91  
92 72 my $name = $definition->name;
93 72 my $object = $self->_get_result($name);
94 72 ▶ unless ($object) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object) 66 6 72
95 66 my $args = $definition->constructor_data;
96 66 ▶ if ( my $requires = $definition->requires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $requires = $definition->requires) 29 37 66
97 29 ▶ while ( my ( $parent, $methods ) = each %$requires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$requires) 35 29 64
98 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") 0 35 35
99 or croak("Panic: required object '$parent' not loaded");
100 35 my ( $our, $their ) = @{$methods}{qw/our their/};
101 35 $args->{$our} = $other->$their;
102 }
103 }
104 66 $object = $self->schema->resultset( $definition->resultset_class )
105 ->create( $definition->constructor_data );
106 66 $self->_set_fixture( $name, $object );
107 }
108 72 return $object;
109 }
110  
111 sub _load {
112 142 ▶ my ( $self, $definition ) = @_;
Subroutine _load: called CC=4 SCAR=13.9
113  
114 # XXX This is our guard against circular definitions.
115 142 ▶ if ( $self->is_loaded( $definition->name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_loaded($definition->name)) 70 72 142
116 70 return $self->_get_result( $definition->name );
117 }
118  
119 72 ▶ if ( my $requires = $definition->requires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $requires = $definition->requires) 35 37 72
120 35 $self->_load_previous_fixtures($requires);
121 }
122  
123 72 my $object = $self->_get_object($definition);
124  
125 72 ▶ if ( my $next = $definition->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $definition->next) 27 45 72
126 27 $self->_load_next_fixtures($next);
127 }
128 72 return $object;
129 }
130  
131 sub _load_previous_fixtures {
132 35 ▶ my ( $self, $requires ) = @_;
Subroutine _load_previous_fixtures: called CC=2 SCAR=6.9
133  
134 35 foreach my $parent ( keys %$requires ) {
135 43 $self->_load( $self->_get_definition_object($parent) );
136 }
137 }
138  
139 sub _load_next_fixtures {
140 27 ▶ my ( $self, $next ) = @_;
Subroutine _load_next_fixtures: called CC=5 SCAR=16.1
141  
142 27 foreach my $fixture (@$next) {
143 30 my $definition = $self->_get_definition_object($fixture);
144  
145 # a fixture may say "load these other fixtures next", but if that
146 # appens, those "next" fixtures may have different parent fixtures
147 # that they require, so we load them here.
148 30 ▶ if ( my $requires = $definition->requires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $requires = $definition->requires) 22 8 30
149 22 ▶ while ( my ( $parent, $methods ) = each %$requires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$requires) 28 22 50
150 28 ▶ $self->_load( $self->_get_definition_object($parent) )
Branch: 1 branch
BranchTrueFalseTotal
$self->_load($self->_get_definition_object($parent)) or croak("Panic: related object '${parent}' not loaded") 0 28 28
151 || croak("Panic: related object '$parent' not loaded");
152 }
153 }
154  
155 # ... and *now* we can load the fixture
156 30 $self->_load($definition);
157 }
158 }
159  
160 sub unload {
161 15 ▶ my $self = shift;
Subroutine unload: called CC=3 SCAR=11.0
Pod documented
162 15 ▶ if ( not $self->no_transactions and $self->_in_transaction ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->no_transactions and $self->_in_transaction) { } 12 3 15
Condition100%3/3
not $self->no_transactions and $self->_in_transaction
!ll&&!rl&&r
2112
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
163 12 $self->schema->txn_rollback;
164 12 $self->_clear;
165 12 $self->_set_in_transaction(0);
166 }
167 else {
168 # XXX I don't think I really need this
169 #carp("finish() called without load()");
170 }
171 15 return 1;
172 }
173  
174 sub all_fixture_names {
175 0 ▶ croak("You must override all_fixture_names() in a subclass");
Subroutine all_fixture_names: not called CC=1 SCAR=6.9
Pod documented
176 }
177  
178 sub get_definition {
179 0 ▶ croak("You must override get_definition() in a subclass");
Subroutine get_definition: not called CC=1 SCAR=6.9
Pod documented
180 }
181  
182 7 ▶sub fixture_loaded { $_[0]->is_loaded( $_[1] ) }
Subroutine fixture_loaded: called CC=1 SCAR=0.0
Pod undocumented
183  
184 sub DEMOLISH {
185 12 ▶ my $self = shift;
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod undocumented
186 12 $self->unload;
187 }
188  
189 9 __PACKAGE__->meta->make_immutable;
190  
191 9 1;
192  
193 __END__
194  
195 =pod
196  
197 =encoding UTF-8
198  
199 =head1 NAME
200  
201 DBIx::Class::EasyFixture - Easy fixtures with DBIx::Class
202  
203 =head1 VERSION
204  
205 version 0.11
206  
207 =head1 SYNOPSIS
208  
209 package My::Fixtures;
210 use Moose;
211 extends 'DBIx::Class::EasyFixture';
212  
213 sub get_fixture { ... }
214 sub all_fixture_names { ... }
215  
216 And in your test code:
217  
218 my $fixtures = My::Fixtures->new( { schema => $schema } );
219 my $dbic_object = $fixtures->load('some_fixture');
220  
221 # run your tests
222  
223 $fixtures->unload;
224  
225 Note that C<unload> will be called for you if your fixture object falls out of
226 scope.
227  
228 =head1 DESCRIPTION
229  
230 The latest version of this is always at
231 L<https://github.com/Ovid/dbix-class-easyfixture>.
232  
233 This is C<ALPHA> code. Documentation is on its way, including a tutorial. For
234 now, you'll have to read the tests. You can read F<t/lib/My/Fixtures.pm> to
235 see how fixtures are defined.
236  
237 I wanted an easier way to load fixtures for L<DBIx::Class> code. I looked at
238 L<DBIx::Class::Fixtures> and it made a lot of assumptions that, while
239 appropriate for some, is not what I wanted (such as the necessity of storing
240 fixtures in JSON files), and had a reliance on knowing the values of primary
241 keys, I wrote this to make it easier to define and load L<DBIx::Class>
242 fixtures for tests.
243  
244 =head1 METHODS
245  
246 =head2 C<new>
247  
248 my $fixtures = Subclass::Of::DBIx::Class::EasyFixture->new({
249 schema => $dbix_class_schema_instance,
250 });
251  
252 This creates and returns a new instance of your C<DBIx::Class::EasyFixture>
253 subclass. All fixture definitions are validated at this time and the
254 constructor will C<croak()> with a useful error message upon validation
255 failure.
256  
257 =head2 C<all_fixture_names>
258  
259 my @fixture_names = $fixtures->all_fixture_names;
260  
261 Must overridden in your subclass. Should return a list (not an array ref!) of
262 all fixture names available. This is used internally to generate error
263 messages if a fixture attempts to reference a non-existent fixture in its
264 C<next> or C<requires> section.
265  
266 =head2 C<get_definition>
267  
268 my $definition = $fixtures->get_definition($fixture_name);
269  
270 Must be overridden in a subclass. Should return the fixture definition for the
271 fixture name passed in. Should return C<undef> if the fixture is not found.
272  
273 =head2 C<get_result>
274  
275 my $dbic_result_object = $fixtures->get_result($fixture_name);
276  
277 Returns the C<DBIx::Class::Result> object for the given fixture name. Will
278 C<carp> if the fixture wasn't loaded (this may become a fatal error in future
279 versions).
280  
281 =head2 C<load>
282  
283 my @dbic_objects = $fixtures->load(@list_of_fixture_names);
284  
285 Attempts to load all fixtures passed to it. If a transaction has not already
286 been started, it will be started now. This method may be called multiple
287 times and it returns the fixtures loaded. If called in scalar context, only
288 returns the first fixture loaded.
289  
290 =head2 C<unload>
291  
292 $fixtures->unload;
293  
294 Rolls back the transaction started with C<load>
295  
296 =head2 C<is_loaded>
297  
298 if ( $fixtures->is_loaded($fixture_name) ) {
299 ...
300 }
301  
302 Returns a boolean value indicating whether or not the given fixture was
303 loaded.
304  
305 *Note*: Originally this method was called C<fixture_loaded>. That was a bad
306 name. However, C<fixture_loaded> still works as an alias to C<is_loaded>.
307  
308 =head1 TRANSACTIONS
309  
310 If you attempt to load a fixture, a transaction is started and it will be
311 rolled back when you call C<unload()> or when the fixture object falls out of
312 scope. If, for some reason, you do not want transactions (for example, if you
313 need to controll them manually), you can use a true value with the
314 C<no_transactions> argument.
315  
316 my $fixtures = My::Fixtures->new(
317 schema => $schema,
318 no_transactions => 1,
319 );
320  
321 =head1 FIXTURES
322  
323 If the following is unclear, see L<DBIx::Class::EasyFixture::Tutorial>.
324  
325 The C<get_definition($fixture_name)> method must always return a fixture
326 definition. The definition must be either a fixture group or a fixture
327 builder.
328  
329 A fixture group is an array reference containing a list of fixture names. For
330 example, C<< $fixture->get_definition('all_people') >> might return:
331  
332 [qw/ person_1 person_2 person_2 /]
333  
334 A fixture builder must return a hash reference with the one or more of the
335 following keys:
336  
337 =over 4
338  
339 =item * C<new> (required)
340  
341 A C<DBIx::Class> result source name.
342  
343 {
344 new => 'Person',
345 using => {
346 name => 'Bob',
347 email => 'bob@example.com',
348 }
349 }
350  
351 Internally, the above will do something similar to this:
352  
353 $schema->resultset($definition->{name})
354 ->create($definition->{using});
355  
356 =item * C<using> (required)
357  
358 A hashref of key/value pairs that will be used to create the C<DBIx::Class>
359 result source referred to by the C<new> key.
360  
361 {
362 new => 'Person',
363 using => {
364 name => 'Bob',
365 email => 'bob@example.com',
366 }
367 }
368  
369 =item * C<next> (optional)
370  
371 If present, this must point to an array reference of fixture names (in other
372 words, a fixture group). These fixtures will then be built I<after> the
373 current fixture is built.
374  
375 {
376 new => 'Person',
377 using => {
378 name => 'Bob',
379 email => 'bob@example.com',
380 },
381 next => [@list_of_fixture_names],
382 }
383  
384 =item * C<requires> (optional)
385  
386 Must point to either a scalar of an attribute name or a hash mapping of
387 attribute names.
388  
389 Many fixtures require data from another fixture. For example, a customer might
390 require a person object being built and the following won't work:
391  
392 {
393 new => 'Customer',
394 using => {
395 first_purchase => $datetime_object,
396 person_id => 'some_person.person_id',
397 }
398 }
399  
400 Assuming we already have a C<Person> fixture defined and it's named
401 C<some_person> and its ID is named C<id>, we can do this:
402  
403 {
404 new => 'Customer',
405 using => { first_purchase => $datetime_object },
406 requires => {
407 some_person => {
408 our => 'person_id',
409 their => 'id',
410 },
411 },
412 }
413  
414 If you prefer, you can I<inline> the C<requires> into the C<using> key. You
415 may find this syntax cleaner:
416  
417 {
418 new => 'Customer',
419 using => {
420 first_purchase => $datetime_object,
421 person_id => { some_person => 'id' },
422 },
423 }
424  
425 The C<our> key refers to the attribute for the C<Customer> fixture and the
426 C<their> key refers to the attribute of the C<Person> fixture. As a
427 convenience, if both attributes have the same name, you can omit that hashref
428 and just use the attribute name:
429  
430 {
431 new => 'Customer',
432 using => { first_purchase => $datetime_object },
433 requires => {
434 some_person => 'person_id',
435 },
436 }
437  
438 And multiple C<requires> can be specified:
439  
440 {
441 new => 'Customer',
442 using => { first_purchase => $datetime_object },
443 requires => {
444 some_person => 'person_id',
445 primary_contact => 'contact_id',
446 },
447 }
448  
449 Or you can skip the C<requires> block entirely and write the above like this
450 (which is now the preferred syntax, but whatever floats your boat):
451  
452 {
453 new => 'Customer',
454 using => {
455 first_purchase => $datetime_object,
456 person_id => { some_person => 'person_id' },
457 contact_id => { primary_contact => 'contact_id' },
458 },
459 }
460  
461 If both the current fixture and the other fixture it requires have the same
462 name for the attribute, a reference to the other fixture name (scalar
463 reference) will suffice:
464  
465 {
466 new => 'Customer',
467 using => {
468 first_purchase => $datetime_object,
469 person_id => \'some_person',
470 contact_id => \'primary_contact',
471 },
472 }
473 The above will construct the fixture like this:
474  
475 $schema->resultset('Customer')->create({
476 first_purchase => $datetime_object,
477 person_id => $person->person_id,
478 primary_contact => $contact->contact_id,
479 });
480  
481 =back
482  
483 When writing a fixture builder, remember that C<requires> are always built
484 before the current fixture and C<next> is also built after the current
485 fixture.
486  
487 =head1 TUTORIAL
488  
489 See L<DBIx::Class::EasyFixture::Tutorial>.
490  
491 =head1 AUTHOR
492  
493 Curtis "Ovid" Poe, C<< <ovid at cpan.org> >>
494  
495 =head1 TODO
496  
497 =over 4
498  
499 =item * Prevent circular fixtures
500  
501 Currently it's very easy to define circular dependencies. We'll worry about
502 that later when it becomes more clear how to best handle them.
503  
504 =item * Better load information
505  
506 Track what fixtures are requested and what fixtures are loaded (and in which
507 order). This makes for better error reporting.
508  
509 =head1 AUTHOR
510  
511 Curtis "Ovid" Poe <ovid@cpan.org>
512  
513 =head1 COPYRIGHT AND LICENSE
514  
515 This software is copyright (c) 2014 by Curtis "Ovid" Poe.
516  
517 This is free software; you can redistribute it and/or modify it under
518 the same terms as the Perl 5 programming language system itself.
519  
520 =cut
« EasyFixture/Definition.pm ↑ Summary EasyFixture/Tutorial.pm »