EasyFixture.pm

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