File Coverage

blib/lib/Test/DBIx/Class/FixtureCommand/Populate.pm
Criterion Covered Total %
statement 28 28 100.0
branch 7 8 87.5
condition 7 9 77.7
subroutine 3 3 100.0
pod 1 1 100.0
total 46 49 93.8


line stmt bran cond sub pod time code
1             package Test::DBIx::Class::FixtureCommand::Populate; {
2              
3 13     13   8035 use Moose;
  13         22  
  13         101  
4 13     13   57043 use Test::More ();
  13         22  
  13         2760  
5             with 'Test::DBIx::Class::Role::FixtureCommand';
6              
7             sub install_fixtures {
8 11     11 1 26 my ($self, $arg, @rest) = @_;
9 11         374 my $builder = $self
10             ->schema_manager
11             ->builder;
12              
13 11 50       38 $builder->croak("Argument is required.")
14             unless $arg;
15              
16 11         17 my @args;
17 11 100 100     106 if(ref $arg && ref $arg eq 'ARRAY') {
    100 66        
18 6         20 @args = @$arg;
19             }
20             elsif(ref $arg && ref $arg eq 'HASH') {
21 4         16 @args = %$arg;
22             }
23             else {
24 1         3 @args = ($arg, @rest);
25             }
26              
27 11         17 my @definitions;
28 11         33 while(@args) {
29 17         24 my $next = shift(@args);
30 17 100 66     68 if( (ref $next) && (ref $next eq 'HASH') ) {
31 6         17 push @definitions, $next;
32             } else {
33 11         16 my $value = shift(@args);
34 11         37 push @definitions, {$next => $value};
35             }
36             }
37              
38 11         21 my @return;
39 11         27 foreach my $definition (@definitions) {
40 17         189 while (my ($source, $rows) = each %$definition) {
41 18         632 my $rs = $self->schema_manager->schema->resultset($source);
42              
43 18         5563 my @rows = $rs->populate($rows);
44 18         38675140 push @return, {$source => [@rows]};
45             }
46             }
47 11         257 return @return;
48             }
49             } 1;
50              
51             __END__
52              
53             =head1 NAME
54              
55             Test::DBIx::Class::FixtureCommand::Populate - Install fixtures using Populate
56              
57             =head1 SYNOPSIS
58              
59             my $command = Test::DBIx::Class::FixtureComand::Populate->new(schema=>$schema);
60             $command->install_fixtures($fixtures);
61              
62             =head1 DESCRIPTION
63              
64             This uses the L<DBIx::Class::Schema/populate> method to install fixture data.
65             Expects an hash of "Source => [\@fields, \@rows]". Please see the 'populate'
66             method for more information. Examples:
67              
68             ->install_fixtures(
69             Person => [
70             ['name', 'age'],
71             ['john', 40],
72             ['vincent', 15],
73             ],
74             Job => [
75             [title => 'description'],
76             [programmer => 'Who wrote the code'],
77             [marketer => 'Who sold the code'],
78             ],
79             );
80              
81             You may include as many Sources as you like, and even the same one more than
82             once.
83              
84             For additional flexibility with various configuration formats, we accept three
85             variations of the incoming arguments:
86              
87             ## Array of HashRefs
88             ->install_fixtures(
89             {Person => [
90             ['name', 'age'],
91             ['john', 40],
92             ['vincent', 15],
93             ]},
94             {Job => [
95             [title => 'description'],
96             [programmer => 'Who wrote the code'],
97             [marketer => 'Who sold the code'],
98             ]},
99             );
100              
101             ## ArrayRef
102             ->install_fixtures([
103             Person => [
104             ['name', 'age'],
105             ['john', 40],
106             ['vincent', 15],
107             ],
108             Job => [
109             [title => 'description'],
110             [programmer => 'Who wrote the code'],
111             [marketer => 'Who sold the code'],
112             ],
113             ]);
114              
115             ## ArrayRef of HashRefs
116             ->install_fixtures([
117             {Person => [
118             ['name', 'age'],
119             ['john', 40],
120             ['vincent', 15],
121             ]},
122             {Job => [
123             [title => 'description'],
124             [programmer => 'Who wrote the code'],
125             [marketer => 'Who sold the code'],
126             ]},
127             ]);
128              
129             This should allow you to model your fixtures in your configuration format of
130             choice without a lot of trouble.
131              
132             =head1 METHODS
133              
134             This class defines the following methods
135              
136             =head2 install_fixtures
137              
138             Takes an Array or ArrayRef of arguments and installs them into your target
139             database. Returns an array of hashrefs, where each hashref is a {$source =>
140             @rows} pair.
141              
142             =head1 AUTHOR
143              
144             John Napiorkowski C<< <jjnapiork@cpan.org> >>
145              
146             =head1 COPYRIGHT & LICENSE
147              
148             Copyright 2009, John Napiorkowski C<< <jjnapiork@cpan.org> >>
149              
150             This program is free software; you can redistribute it and/or modify
151             it under the same terms as Perl itself.
152              
153             =cut