File Coverage

lib/CatalystX/CRUD/Test/Controller.pm
Criterion Covered Total %
statement 59 62 95.1
branch 13 18 72.2
condition 0 3 0.0
subroutine 11 11 100.0
pod 1 1 100.0
total 84 95 88.4


line stmt bran cond sub pod time code
1             package CatalystX::CRUD::Test::Controller;
2 4     4   17595 use strict;
  4         16  
  4         161  
3 4     4   21 use warnings;
  4         19  
  4         116  
4 4     4   29 use base qw( CatalystX::CRUD::Controller );
  4         10  
  4         1921  
5 4     4   32 use Carp;
  4         10  
  4         286  
6 4     4   39 use Data::Dump;
  4         13  
  4         259  
7 4     4   44 use mro 'c3';
  4         6  
  4         37  
8              
9             __PACKAGE__->mk_accessors(qw( form_fields ));
10              
11             our $VERSION = '0.58';
12              
13             =head1 NAME
14              
15             CatalystX::CRUD::Test::Controller - mock controller class for testing CatalystX::CRUD packages
16              
17             =head1 SYNOPSIS
18              
19             package MyApp::Controller::Foo;
20             use strict;
21             use base qw( CatalystX::CRUD::Test::Controller );
22            
23             use MyForm;
24            
25             __PACKAGE__->config(
26             form_class => 'MyForm',
27             form_fields => [qw( one two three )],
28             init_form => 'init_with_foo',
29             init_object => 'foo_from_form',
30             default_template => 'no/such/file',
31             model_name => 'Foo',
32             primary_key => 'id',
33             view_on_single_result => 0,
34             page_size => 50,
35             allow_GET_writes => 0,
36             );
37              
38             1;
39            
40            
41             =head1 DESCRIPTION
42              
43             CatalystX::CRUD::Test::Controller is a mock controller class for
44             testing CatalystX::CRUD packages. It implements the required Controller
45             methods and overrides others to work with CatalystX::CRUD::Test::Form.
46              
47             =head1 METHODS
48              
49             =head2 form_to_object
50              
51             The flow of this methods comes more or less verbatim from the RHTMLO controller.
52              
53             Returns the object from stash() initialized with the form and request params.
54              
55             =cut
56              
57             sub form_to_object {
58 9     9   27 my ( $self, $c ) = @_;
59 9         35 my $form = $c->stash->{form};
60 9         620 my $obj = $c->stash->{object};
61 9         603 my $obj_meth = $self->init_object;
62 9         1186 my $form_meth = $self->init_form;
63              
64             # id always comes from url but not necessarily from form
65 9         1060 my $id = $c->stash->{object_id};
66              
67             # initialize the form with the object's values
68 9         631 $form->$form_meth($obj);
69              
70             # set param values from request
71 9         38 $form->params( $c->req->params );
72              
73             # override form's values with those from params
74             # no_clear is important because we already initialized with object
75             # and we do not want to undo those mods.
76 9         989 $form->init_fields( no_clear => 1 );
77              
78             # return if there was a problem with any param values
79 9 50       39 unless ( $form->validate() ) {
80 0         0 $c->stash->{error} = $form->error; # NOT throw_error()
81 0   0     0 $c->stash->{template} ||= $self->default_template; # MUST specify
82 0         0 return 0;
83             }
84              
85             # re-set object's values from the now-valid form
86 9         49 $form->$obj_meth($obj);
87              
88 9         32 return $obj;
89             }
90              
91             =head2 form
92              
93             Returns a new C<form_class> object every time, initialized with C<form_fields>.
94              
95             =cut
96              
97             sub form {
98 55     55   132 my ( $self, $c ) = @_;
99 55         211 my $form_class = $self->form_class;
100 55         8112 my $arg = { fields => $self->form_fields };
101 55         7253 my $form = $form_class->new($arg);
102 55         271 return $form;
103             }
104              
105             =head2 end
106              
107             If the stash() has an 'object' defined,
108             serializes the object with serialize_object()
109             and sticks it in the response body().
110              
111             If there are any errors, replaces the normal Catalyst debug screen
112             with contents of $c->error.
113              
114             =cut
115              
116             sub end : Private {
117 48     48   71192 my ( $self, $c ) = @_;
118 48 50       143 $c->log->debug('test controller end()') if $c->debug;
119 48 100       239 if ( defined $c->stash->{object} ) {
    100          
120 34         2380 $c->res->body( $self->serialize_object( $c, $c->stash->{object} ) );
121             }
122             elsif ( defined $c->stash->{results} ) {
123 4         574 my @body;
124 4         25 while ( my $result = $c->stash->{results}->next ) {
125 6         53 push( @body, $self->serialize_object( $c, $result ) );
126             }
127 4         25 $c->res->body( join( "\n", @body ) );
128             }
129 48 100       14554 if ( $self->has_errors($c) ) {
130 1         13 my $err = join( "\n", @{ $c->error } );
  1         3  
131 1 50       9 $c->log->error($err) if $c->debug;
132 1 50       6 $c->res->body($err) unless $c->res->body;
133 1 50       88 $c->res->status(500) unless $c->res->status;
134 1         155 $c->clear_errors;
135             }
136 4     4   2186 }
  4         20  
  4         37  
137              
138             =head2 serialize_object( I<context>, I<object> )
139              
140             Serializes I<object> for response. Default is just to create hashref
141             of key/value pairs and send through Data::Dump::dump().
142              
143             =cut
144              
145             sub serialize_object {
146 40     40 1 3335 my ( $self, $c, $object ) = @_;
147 40         131 my $fields = $self->form_fields;
148 40         5111 my $serial = {};
149 40         150 for my $f (@$fields) {
150 80 100       9652 $serial->{$f} = defined $object->$f ? $object->$f . '' : undef;
151             }
152 40         8524 return Data::Dump::dump($serial);
153             }
154              
155             1;
156              
157             __END__
158              
159             =head1 AUTHOR
160              
161             Peter Karman, C<< <perl at peknet.com> >>
162              
163             =head1 BUGS
164              
165             Please report any bugs or feature requests to
166             C<bug-catalystx-crud at rt.cpan.org>, or through the web interface at
167             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CatalystX-CRUD>.
168             I will be notified, and then you'll automatically be notified of progress on
169             your bug as I make changes.
170              
171             =head1 SUPPORT
172              
173             You can find documentation for this module with the perldoc command.
174              
175             perldoc CatalystX::CRUD
176              
177             You can also look for information at:
178              
179             =over 4
180              
181             =item * AnnoCPAN: Annotated CPAN documentation
182              
183             L<http://annocpan.org/dist/CatalystX-CRUD>
184              
185             =item * CPAN Ratings
186              
187             L<http://cpanratings.perl.org/d/CatalystX-CRUD>
188              
189             =item * RT: CPAN's request tracker
190              
191             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CatalystX-CRUD>
192              
193             =item * Search CPAN
194              
195             L<http://search.cpan.org/dist/CatalystX-CRUD>
196              
197             =back
198              
199             =head1 ACKNOWLEDGEMENTS
200              
201             =head1 COPYRIGHT & LICENSE
202              
203             Copyright 2008 Peter Karman, all rights reserved.
204              
205             This program is free software; you can redistribute it and/or modify it
206             under the same terms as Perl itself.
207              
208             =cut