PageRenderTime 52ms CodeModel.GetById 2ms app.highlight 47ms RepoModel.GetById 1ms app.codeStats 0ms

/t/rep_nest.t

http://github.com/gshank/html-formhandler
text | 248 lines | 202 code | 46 blank | 0 comment | 0 complexity | a954cad855c4c54fb4b6485f5dbe5114 MD5 | raw file
  1use strict;
  2use warnings;
  3use Test::More;
  4
  5# This test uses roles to create forms and fields
  6# and nests the repeatables
  7
  8{
  9    package Test::Form::Role::Employee;
 10    use HTML::FormHandler::Moose::Role;
 11
 12    has_field 'first_name';
 13    has_field 'last_name';
 14    has_field 'email';
 15    has_field 'password';
 16}
 17
 18{
 19    package Test::Form::Employee;
 20    use HTML::FormHandler::Moose;
 21    extends 'HTML::FormHandler';
 22
 23    with 'Test::Form::Role::Employee';
 24}
 25
 26{
 27    package Test::Form::Field::Employee;
 28    use HTML::FormHandler::Moose;
 29    extends 'HTML::FormHandler::Field::Compound';
 30    
 31    has_field 'id' => ( type => 'PrimaryKey' );
 32    with 'Test::Form::Role::Employee';
 33}
 34
 35{
 36    package Test::Form::Role::Office;
 37    use HTML::FormHandler::Moose::Role;
 38
 39    has_field 'address';
 40    has_field 'city';
 41    has_field 'state';
 42    has_field 'zip';
 43    has_field 'phone';
 44    has_field 'fax';
 45    has_field 'employees' => ( type => 'Repeatable' );
 46    has_field 'employees.contains' =>  ( type =>  '+Test::Form::Field::Employee' );
 47    
 48}
 49
 50{
 51    package Test::Form::Field::Office;
 52    use HTML::FormHandler::Moose;
 53    extends 'HTML::FormHandler::Field::Compound';
 54
 55    has_field 'id' => ( type => 'PrimaryKey' );
 56    with 'Test::Form::Role::Office';
 57
 58}
 59
 60{
 61    package Test::Form::Office;
 62    use HTML::FormHandler::Moose;
 63    extends 'HTML::FormHandler';
 64    with 'Test::Form::Role::Office';
 65
 66}
 67
 68{
 69    package Test::Form::Company;
 70
 71    use HTML::FormHandler::Moose;
 72    extends 'HTML::FormHandler';
 73
 74    has '+item_class' => (
 75        default => 'Company'
 76    );
 77
 78    has_field 'name';
 79    has_field 'username';
 80    has_field 'tier';
 81    has_field 'type';
 82
 83    has_field 'offices' => ( type => 'Repeatable' ); 
 84    has_field 'offices.contains' => ( type => '+Test::Form::Field::Office' );
 85
 86}
 87
 88my $field = Test::Form::Field::Employee->new( name => 'test_employee' );
 89ok( $field, 'field created' );
 90is( $field->num_fields, 5, 'right number of fields' );
 91
 92my $form = Test::Form::Company->new;
 93my $params = {
 94    name => 'my_name',
 95    username => 'a_user',
 96    tier => 1,
 97    type => 'simple',
 98    offices => [
 99        {
100            id => 1,
101            address => '101 Main St',
102            city => 'Smallville',
103            state => 'CA',
104            employees => [
105                {
106                    id => 1,
107                    first_name => 'John',
108                    last_name  => 'Doe',
109                    email      => 'jdoe@gmail.com',
110                }
111            ]
112        },
113    ] 
114};
115$form->process( params => $params );
116ok( $form, 'form built' );
117my $fif = $form->fif;
118my $value = $form->value;
119my $expected = {
120   'name' => 'my_name',
121   'offices.0.address' => '101 Main St',
122   'offices.0.city' => 'Smallville',
123   'offices.0.employees.0.email' => 'jdoe@gmail.com',
124   'offices.0.employees.0.first_name' => 'John',
125   'offices.0.employees.0.id' => 1,
126   'offices.0.employees.0.last_name' => 'Doe',
127   'offices.0.employees.0.password' => '',
128   'offices.0.fax' => '',
129   'offices.0.id' => 1,
130   'offices.0.phone' => '',
131   'offices.0.state' => 'CA',
132   'offices.0.zip' => '',
133   'tier' => 1,
134   'type' => 'simple',
135   'username' => 'a_user',
136};
137is_deeply( $fif, $expected, 'fif is correct' );
138is_deeply( $value, $params, 'value is correct' );
139
140# following takes some pieces of above tests and tests using
141# a Repeatable subclass
142{
143
144{
145    package Test::Form::Field::RepEmployee;
146    use HTML::FormHandler::Moose;
147    extends 'HTML::FormHandler::Field::Repeatable';
148    
149    has_field 'id' => ( type => 'PrimaryKey' );
150    with 'Test::Form::Role::Employee';
151}
152
153{
154    package Test::Form::Role::RepOffice;
155    use HTML::FormHandler::Moose::Role;
156
157    has_field 'address';
158    has_field 'city';
159    has_field 'state';
160    has_field 'zip';
161    has_field 'phone';
162    has_field 'fax';
163    has_field 'employees' => ( type => '+Test::Form::Field::RepEmployee' );
164    
165}
166
167{
168    package Test::Form::RepOffice;
169    use HTML::FormHandler::Moose;
170    extends 'HTML::FormHandler';
171    with 'Test::Form::Role::RepOffice';
172
173}
174
175my $field = Test::Form::Field::RepEmployee->new( name => 'test_employee' );
176ok( $field, 'field created' );
177is( $field->num_fields, 5, 'right number of fields' );
178
179my $form = Test::Form::RepOffice->new;
180my $params = {
181    address => '101 Main St',
182    city => 'Smallville',
183    state => 'CA',
184    employees => [
185        {
186            id => 1,
187            first_name => 'John',
188            last_name  => 'Doe',
189            email      => 'jdoe@gmail.com',
190        }
191    ]
192};
193$form->process( params => $params );
194ok( $form, 'form built' );
195my $fif = $form->fif;
196my $value = $form->value;
197my $expected = {
198   'address' => '101 Main St',
199   'city' => 'Smallville',
200   'employees.0.email' => 'jdoe@gmail.com',
201   'employees.0.first_name' => 'John',
202   'employees.0.id' => 1,
203   'employees.0.last_name' => 'Doe',
204   'employees.0.password' => '',
205   'fax' => '',
206   'phone' => '',
207   'state' => 'CA',
208   'zip' => '',
209};
210is_deeply( $fif, $expected, 'fif is correct' );
211is_deeply( $value, $params, 'value is correct' );
212
213}
214
215{
216    package MyForm;
217
218    use HTML::FormHandler::Moose;
219    extends 'HTML::FormHandler';
220
221    has_field 'name' => ( type => 'Text' );
222    has_field 'args' => ( type => '+MyForm::Args' );
223
224
225    package MyForm::Args;
226
227    use HTML::FormHandler::Moose;
228    use namespace::autoclean;
229
230    extends 'HTML::FormHandler::Field::Compound';
231
232    has_field 'id'        => (type => 'Text');
233    has_field 'data'      => (type => 'Repeatable');
234    has_field 'data.type' => (type => 'Text');
235    has_field 'data.links' => (type => 'Repeatable');
236    has_field 'data.links.title' => (type => 'Text');
237    has_field 'data.links.url'   => (type => 'Text');
238}
239
240$form = MyForm->new;
241ok( $form, 'form built' );
242
243$form->process( params => {} );
244
245my $rendered = $form->render;
246like( $rendered, qr/"args.data.0.links.0.title"/, 'form has args.data.link.title in Repeatable' );
247
248done_testing;