CollectionFieldTest.php 5.6 KB

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