CollectionFieldTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Tests\Component\Form;
  11. require_once __DIR__.'/TestCase.php';
  12. use Symfony\Component\Form\CollectionForm;
  13. use Symfony\Component\Form\Form;
  14. class CollectionFormTest extends TestCase
  15. {
  16. public function testContainsOnlyCsrfTokenByDefault()
  17. {
  18. $field = $this->factory->create('collection', 'emails', array(
  19. 'type' => 'field',
  20. 'csrf_field_name' => 'abc',
  21. ));
  22. $this->assertTrue($field->has('abc'));
  23. $this->assertEquals(1, count($field));
  24. }
  25. public function testSetDataAdjustsSize()
  26. {
  27. $field = $this->factory->create('collection', 'emails', array(
  28. 'type' => 'field',
  29. ));
  30. $field->setData(array('foo@foo.com', 'foo@bar.com'));
  31. $this->assertTrue($field[0] instanceof Form);
  32. $this->assertTrue($field[1] instanceof Form);
  33. $this->assertEquals(2, count($field));
  34. $this->assertEquals('foo@foo.com', $field[0]->getData());
  35. $this->assertEquals('foo@bar.com', $field[1]->getData());
  36. $field->setData(array('foo@baz.com'));
  37. $this->assertTrue($field[0] instanceof Form);
  38. $this->assertFalse(isset($field[1]));
  39. $this->assertEquals(1, count($field));
  40. $this->assertEquals('foo@baz.com', $field[0]->getData());
  41. }
  42. public function testSetDataAdjustsSizeIfModifiable()
  43. {
  44. $field = $this->factory->create('collection', 'emails', array(
  45. 'type' => 'field',
  46. 'modifiable' => true,
  47. ));
  48. $field->setData(array('foo@foo.com', 'foo@bar.com'));
  49. $this->assertTrue($field[0] instanceof Form);
  50. $this->assertTrue($field[1] instanceof Form);
  51. $this->assertTrue($field['$$name$$'] instanceof Form);
  52. $this->assertEquals(3, count($field));
  53. $field->setData(array('foo@baz.com'));
  54. $this->assertTrue($field[0] instanceof Form);
  55. $this->assertFalse(isset($field[1]));
  56. $this->assertTrue($field['$$name$$'] instanceof Form);
  57. $this->assertEquals(2, count($field));
  58. }
  59. public function testThrowsExceptionIfObjectIsNotTraversable()
  60. {
  61. $field = $this->factory->create('collection', 'emails', array(
  62. 'type' => 'field',
  63. ));
  64. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  65. $field->setData(new \stdClass());
  66. }
  67. public function testModifiableCollectionsContainExtraForm()
  68. {
  69. $field = $this->factory->create('collection', 'emails', array(
  70. 'type' => 'field',
  71. 'modifiable' => true,
  72. ));
  73. $field->setData(array('foo@bar.com'));
  74. $this->assertTrue($field['0'] instanceof Form);
  75. $this->assertTrue($field['$$name$$'] instanceof Form);
  76. $this->assertEquals(2, count($field));
  77. }
  78. public function testNotResizedIfBoundWithMissingData()
  79. {
  80. $field = $this->factory->create('collection', 'emails', array(
  81. 'type' => 'field',
  82. ));
  83. $field->setData(array('foo@foo.com', 'bar@bar.com'));
  84. $field->bind(array('foo@bar.com'));
  85. $this->assertTrue($field->has('0'));
  86. $this->assertTrue($field->has('1'));
  87. $this->assertEquals('foo@bar.com', $field[0]->getData());
  88. $this->assertEquals(null, $field[1]->getData());
  89. }
  90. public function testResizedIfBoundWithMissingDataAndModifiable()
  91. {
  92. $field = $this->factory->create('collection', 'emails', array(
  93. 'type' => 'field',
  94. 'modifiable' => true,
  95. ));
  96. $field->setData(array('foo@foo.com', 'bar@bar.com'));
  97. $field->bind(array('foo@bar.com'));
  98. $this->assertTrue($field->has('0'));
  99. $this->assertFalse($field->has('1'));
  100. $this->assertEquals('foo@bar.com', $field[0]->getData());
  101. }
  102. public function testNotResizedIfBoundWithExtraData()
  103. {
  104. $field = $this->factory->create('collection', 'emails', array(
  105. 'type' => 'field',
  106. ));
  107. $field->setData(array('foo@bar.com'));
  108. $field->bind(array('foo@foo.com', 'bar@bar.com'));
  109. $this->assertTrue($field->has('0'));
  110. $this->assertFalse($field->has('1'));
  111. $this->assertEquals('foo@foo.com', $field[0]->getData());
  112. }
  113. public function testResizedUpIfBoundWithExtraDataAndModifiable()
  114. {
  115. $field = $this->factory->create('collection', 'emails', array(
  116. 'type' => 'field',
  117. 'modifiable' => true,
  118. ));
  119. $field->setData(array('foo@bar.com'));
  120. $field->bind(array('foo@foo.com', 'bar@bar.com'));
  121. $this->assertTrue($field->has('0'));
  122. $this->assertTrue($field->has('1'));
  123. $this->assertEquals('foo@foo.com', $field[0]->getData());
  124. $this->assertEquals('bar@bar.com', $field[1]->getData());
  125. $this->assertEquals(array('foo@foo.com', 'bar@bar.com'), $field->getData());
  126. }
  127. public function testResizedDownIfBoundWithLessDataAndModifiable()
  128. {
  129. $field = $this->factory->create('collection', 'emails', array(
  130. 'type' => 'field',
  131. 'modifiable' => true,
  132. ));
  133. $field->setData(array('foo@bar.com', 'bar@bar.com'));
  134. $field->bind(array('foo@foo.com'));
  135. $this->assertTrue($field->has('0'));
  136. $this->assertFalse($field->has('1'));
  137. $this->assertEquals('foo@foo.com', $field[0]->getData());
  138. $this->assertEquals(array('foo@foo.com'), $field->getData());
  139. }
  140. }