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