PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php

https://gitlab.com/freebird/WebApp
PHP | 277 lines | 224 code | 43 blank | 10 comment | 0 complexity | c826dcd5c94a8a9b310724daec5f5a4e MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Tests\Extension\Core\Type;
  11. use Symfony\Component\Form\Form;
  12. use Symfony\Component\Form\Tests\Fixtures\Author;
  13. use Symfony\Component\Form\Tests\Fixtures\AuthorType;
  14. class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
  15. {
  16. public function testContainsNoChildByDefault()
  17. {
  18. $form = $this->factory->create('collection', null, array(
  19. 'type' => 'text',
  20. ));
  21. $this->assertCount(0, $form);
  22. }
  23. public function testSetDataAdjustsSize()
  24. {
  25. $form = $this->factory->create('collection', null, array(
  26. 'type' => 'text',
  27. 'options' => array(
  28. 'attr' => array('maxlength' => 20),
  29. ),
  30. ));
  31. $form->setData(array('foo@foo.com', 'foo@bar.com'));
  32. $this->assertInstanceOf('Symfony\Component\Form\Form', $form[0]);
  33. $this->assertInstanceOf('Symfony\Component\Form\Form', $form[1]);
  34. $this->assertCount(2, $form);
  35. $this->assertEquals('foo@foo.com', $form[0]->getData());
  36. $this->assertEquals('foo@bar.com', $form[1]->getData());
  37. $formAttrs0 = $form[0]->getConfig()->getOption('attr');
  38. $formAttrs1 = $form[1]->getConfig()->getOption('attr');
  39. $this->assertEquals(20, $formAttrs0['maxlength']);
  40. $this->assertEquals(20, $formAttrs1['maxlength']);
  41. $form->setData(array('foo@baz.com'));
  42. $this->assertInstanceOf('Symfony\Component\Form\Form', $form[0]);
  43. $this->assertFalse(isset($form[1]));
  44. $this->assertCount(1, $form);
  45. $this->assertEquals('foo@baz.com', $form[0]->getData());
  46. $formAttrs0 = $form[0]->getConfig()->getOption('attr');
  47. $this->assertEquals(20, $formAttrs0['maxlength']);
  48. }
  49. public function testThrowsExceptionIfObjectIsNotTraversable()
  50. {
  51. $form = $this->factory->create('collection', null, array(
  52. 'type' => 'text',
  53. ));
  54. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  55. $form->setData(new \stdClass());
  56. }
  57. public function testNotResizedIfSubmittedWithMissingData()
  58. {
  59. $form = $this->factory->create('collection', null, array(
  60. 'type' => 'text',
  61. ));
  62. $form->setData(array('foo@foo.com', 'bar@bar.com'));
  63. $form->submit(array('foo@bar.com'));
  64. $this->assertTrue($form->has('0'));
  65. $this->assertTrue($form->has('1'));
  66. $this->assertEquals('foo@bar.com', $form[0]->getData());
  67. $this->assertEquals('', $form[1]->getData());
  68. }
  69. public function testResizedDownIfSubmittedWithMissingDataAndAllowDelete()
  70. {
  71. $form = $this->factory->create('collection', null, array(
  72. 'type' => 'text',
  73. 'allow_delete' => true,
  74. ));
  75. $form->setData(array('foo@foo.com', 'bar@bar.com'));
  76. $form->submit(array('foo@foo.com'));
  77. $this->assertTrue($form->has('0'));
  78. $this->assertFalse($form->has('1'));
  79. $this->assertEquals('foo@foo.com', $form[0]->getData());
  80. $this->assertEquals(array('foo@foo.com'), $form->getData());
  81. }
  82. public function testResizedDownIfSubmittedWithEmptyDataAndDeleteEmpty()
  83. {
  84. $form = $this->factory->create('collection', null, array(
  85. 'type' => 'text',
  86. 'allow_delete' => true,
  87. 'delete_empty' => true,
  88. ));
  89. $form->setData(array('foo@foo.com', 'bar@bar.com'));
  90. $form->submit(array('foo@foo.com', ''));
  91. $this->assertTrue($form->has('0'));
  92. $this->assertFalse($form->has('1'));
  93. $this->assertEquals('foo@foo.com', $form[0]->getData());
  94. $this->assertEquals(array('foo@foo.com'), $form->getData());
  95. }
  96. public function testDontAddEmptyDataIfDeleteEmpty()
  97. {
  98. $form = $this->factory->create('collection', null, array(
  99. 'type' => 'text',
  100. 'allow_add' => true,
  101. 'delete_empty' => true,
  102. ));
  103. $form->setData(array('foo@foo.com'));
  104. $form->submit(array('foo@foo.com', ''));
  105. $this->assertTrue($form->has('0'));
  106. $this->assertFalse($form->has('1'));
  107. $this->assertEquals('foo@foo.com', $form[0]->getData());
  108. $this->assertEquals(array('foo@foo.com'), $form->getData());
  109. }
  110. public function testNoDeleteEmptyIfDeleteNotAllowed()
  111. {
  112. $form = $this->factory->create('collection', null, array(
  113. 'type' => 'text',
  114. 'allow_delete' => false,
  115. 'delete_empty' => true,
  116. ));
  117. $form->setData(array('foo@foo.com'));
  118. $form->submit(array(''));
  119. $this->assertTrue($form->has('0'));
  120. $this->assertEquals('', $form[0]->getData());
  121. }
  122. public function testResizedDownIfSubmittedWithCompoundEmptyDataAndDeleteEmpty()
  123. {
  124. $form = $this->factory->create('collection', null, array(
  125. 'type' => new AuthorType(),
  126. // If the field is not required, no new Author will be created if the
  127. // form is completely empty
  128. 'options' => array('required' => false),
  129. 'allow_add' => true,
  130. 'delete_empty' => true,
  131. ));
  132. $form->setData(array(new Author('first', 'last')));
  133. $form->submit(array(
  134. array('firstName' => 's_first', 'lastName' => 's_last'),
  135. array('firstName' => '', 'lastName' => ''),
  136. ));
  137. $this->assertTrue($form->has('0'));
  138. $this->assertFalse($form->has('1'));
  139. $this->assertEquals(new Author('s_first', 's_last'), $form[0]->getData());
  140. $this->assertEquals(array(new Author('s_first', 's_last')), $form->getData());
  141. }
  142. public function testNotResizedIfSubmittedWithExtraData()
  143. {
  144. $form = $this->factory->create('collection', null, array(
  145. 'type' => 'text',
  146. ));
  147. $form->setData(array('foo@bar.com'));
  148. $form->submit(array('foo@foo.com', 'bar@bar.com'));
  149. $this->assertTrue($form->has('0'));
  150. $this->assertFalse($form->has('1'));
  151. $this->assertEquals('foo@foo.com', $form[0]->getData());
  152. }
  153. public function testResizedUpIfSubmittedWithExtraDataAndAllowAdd()
  154. {
  155. $form = $this->factory->create('collection', null, array(
  156. 'type' => 'text',
  157. 'allow_add' => true,
  158. ));
  159. $form->setData(array('foo@bar.com'));
  160. $form->submit(array('foo@bar.com', 'bar@bar.com'));
  161. $this->assertTrue($form->has('0'));
  162. $this->assertTrue($form->has('1'));
  163. $this->assertEquals('foo@bar.com', $form[0]->getData());
  164. $this->assertEquals('bar@bar.com', $form[1]->getData());
  165. $this->assertEquals(array('foo@bar.com', 'bar@bar.com'), $form->getData());
  166. }
  167. public function testAllowAddButNoPrototype()
  168. {
  169. $form = $this->factory->create('collection', null, array(
  170. 'type' => 'form',
  171. 'allow_add' => true,
  172. 'prototype' => false,
  173. ));
  174. $this->assertFalse($form->has('__name__'));
  175. }
  176. public function testPrototypeMultipartPropagation()
  177. {
  178. $form = $this->factory
  179. ->create('collection', null, array(
  180. 'type' => 'file',
  181. 'allow_add' => true,
  182. 'prototype' => true,
  183. ))
  184. ;
  185. $this->assertTrue($form->createView()->vars['multipart']);
  186. }
  187. public function testGetDataDoesNotContainsPrototypeNameBeforeDataAreSet()
  188. {
  189. $form = $this->factory->create('collection', array(), array(
  190. 'type' => 'file',
  191. 'prototype' => true,
  192. 'allow_add' => true,
  193. ));
  194. $data = $form->getData();
  195. $this->assertFalse(isset($data['__name__']));
  196. }
  197. public function testGetDataDoesNotContainsPrototypeNameAfterDataAreSet()
  198. {
  199. $form = $this->factory->create('collection', array(), array(
  200. 'type' => 'file',
  201. 'allow_add' => true,
  202. 'prototype' => true,
  203. ));
  204. $form->setData(array('foobar.png'));
  205. $data = $form->getData();
  206. $this->assertFalse(isset($data['__name__']));
  207. }
  208. public function testPrototypeNameOption()
  209. {
  210. $form = $this->factory->create('collection', null, array(
  211. 'type' => 'form',
  212. 'prototype' => true,
  213. 'allow_add' => true,
  214. ));
  215. $this->assertSame('__name__', $form->getConfig()->getAttribute('prototype')->getName(), '__name__ is the default');
  216. $form = $this->factory->create('collection', null, array(
  217. 'type' => 'form',
  218. 'prototype' => true,
  219. 'allow_add' => true,
  220. 'prototype_name' => '__test__',
  221. ));
  222. $this->assertSame('__test__', $form->getConfig()->getAttribute('prototype')->getName());
  223. }
  224. public function testPrototypeDefaultLabel()
  225. {
  226. $form = $this->factory->create('collection', array(), array(
  227. 'type' => 'file',
  228. 'allow_add' => true,
  229. 'prototype' => true,
  230. 'prototype_name' => '__test__',
  231. ));
  232. $this->assertSame('__test__label__', $form->createView()->vars['prototype']->vars['label']);
  233. }
  234. }