FormBuilderTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. use Symfony\Component\Form\FormFactory;
  12. use Symfony\Component\Form\FormBuilder;
  13. use Symfony\Component\Form\Type\Guesser\Guess;
  14. use Symfony\Component\Form\Type\Guesser\ValueGuess;
  15. use Symfony\Component\Form\Type\Guesser\TypeGuess;
  16. class FormBuilderTest extends \PHPUnit_Framework_TestCase
  17. {
  18. private $dispatcher;
  19. private $builder;
  20. public function setUp()
  21. {
  22. $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  23. $this->builder = new FormBuilder('name', $this->dispatcher);
  24. }
  25. /**
  26. * Changing the name is not allowed, otherwise the name and property path
  27. * are not synchronized anymore
  28. *
  29. * @see FieldType::buildForm
  30. */
  31. public function testNoSetName()
  32. {
  33. $this->assertFalse(method_exists($this->builder, 'setName'));
  34. }
  35. public function testAddNameNoString()
  36. {
  37. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  38. $this->builder->add(1234);
  39. }
  40. public function testAddTypeNoString()
  41. {
  42. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  43. $this->builder->add('foo', 1234);
  44. }
  45. public function testAddWithGuessFluent()
  46. {
  47. $this->builder = new FormBuilder('name', $this->dispatcher, 'stdClass');
  48. $builder = $this->builder->add('foo');
  49. $this->assertSame($builder, $this->builder);
  50. }
  51. public function testAddIsFluent()
  52. {
  53. $builder = $this->builder->add('foo', 'text', array('bar' => 'baz'));
  54. $this->assertSame($builder, $this->builder);
  55. }
  56. public function testAdd()
  57. {
  58. $this->assertFalse($this->builder->has('foo'));
  59. $this->builder->add('foo', 'text');
  60. $this->assertTrue($this->builder->has('foo'));
  61. }
  62. public function testAddFormType()
  63. {
  64. $this->assertFalse($this->builder->has('foo'));
  65. $this->builder->add('foo', $this->getMock('Symfony\Component\Form\Type\FormTypeInterface'));
  66. $this->assertTrue($this->builder->has('foo'));
  67. }
  68. public function testRemove()
  69. {
  70. $this->builder->add('foo', 'text');
  71. $this->builder->remove('foo');
  72. $this->assertFalse($this->builder->has('foo'));
  73. }
  74. public function testRemoveUnknown()
  75. {
  76. $this->builder->remove('foo');
  77. $this->assertFalse($this->builder->has('foo'));
  78. }
  79. public function testBuildNoTypeNoDataClass()
  80. {
  81. $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The data class must be set to automatically create children');
  82. $this->builder->build('foo');
  83. }
  84. public function testGetUnknown()
  85. {
  86. $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The field "foo" does not exist');
  87. $this->builder->get('foo');
  88. }
  89. public function testGetTyped()
  90. {
  91. $expectedType = 'text';
  92. $expectedName = 'foo';
  93. $expectedOptions = array('bar' => 'baz');
  94. $factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  95. $factory->expects($this->once())
  96. ->method('createBuilder')
  97. ->with($this->equalTo($expectedType), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
  98. ->will($this->returnValue($this->getFormBuilder()));
  99. $this->builder->setFormFactory($factory);
  100. $this->builder->add($expectedName, $expectedType, $expectedOptions);
  101. $builder = $this->builder->get($expectedName);
  102. $this->assertNotSame($builder, $this->builder);
  103. }
  104. public function testGetGuessed()
  105. {
  106. $expectedName = 'foo';
  107. $expectedOptions = array('bar' => 'baz');
  108. $factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  109. $factory->expects($this->once())
  110. ->method('createBuilderForProperty')
  111. ->with($this->equalTo('stdClass'), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
  112. ->will($this->returnValue($this->getFormBuilder()));
  113. $this->builder = new FormBuilder('name', $this->dispatcher, 'stdClass');
  114. $this->builder->setFormFactory($factory);
  115. $this->builder->add($expectedName, null, $expectedOptions);
  116. $builder = $this->builder->get($expectedName);
  117. $this->assertNotSame($builder, $this->builder);
  118. }
  119. private function getFormBuilder()
  120. {
  121. return $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. }
  125. }