FormBuilderTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 $builder;
  19. public function setUp()
  20. {
  21. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  22. $this->builder = new FormBuilder($dispatcher);
  23. }
  24. public function testAddNameNoString()
  25. {
  26. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  27. $this->builder->add(1234);
  28. }
  29. public function testAddTypeNoString()
  30. {
  31. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  32. $this->builder->add("foo", 1234);
  33. }
  34. public function testAddWithGuessFluent()
  35. {
  36. $this->builder->setDataClass('stdClass');
  37. $builder = $this->builder->add('foo');
  38. $this->assertSame($builder, $this->builder);
  39. }
  40. public function testAddIsFluent()
  41. {
  42. $builder = $this->builder->add("foo", "text", array("bar" => "baz"));
  43. $this->assertSame($builder, $this->builder);
  44. }
  45. public function testAdd()
  46. {
  47. $this->assertFalse($this->builder->has('foo'));
  48. $this->builder->add('foo', 'text');
  49. $this->assertTrue($this->builder->has('foo'));
  50. }
  51. public function testAddFormType()
  52. {
  53. $this->assertFalse($this->builder->has('foo'));
  54. $this->builder->add('foo', $this->getMock('Symfony\Component\Form\Type\FormTypeInterface'));
  55. $this->assertTrue($this->builder->has('foo'));
  56. }
  57. public function testRemove()
  58. {
  59. $this->builder->add('foo', 'text');
  60. $this->builder->remove('foo');
  61. $this->assertFalse($this->builder->has('foo'));
  62. }
  63. public function testRemoveUnknown()
  64. {
  65. $this->builder->remove('foo');
  66. $this->assertFalse($this->builder->has('foo'));
  67. }
  68. public function testBuildNoTypeNoDataClass()
  69. {
  70. $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The data class must be set to automatically create children');
  71. $this->builder->build("foo");
  72. }
  73. public function testGetUnknown()
  74. {
  75. $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The field "foo" does not exist');
  76. $this->builder->get('foo');
  77. }
  78. public function testGetTyped()
  79. {
  80. $expectedType = 'text';
  81. $expectedName = 'foo';
  82. $expectedOptions = array('bar' => 'baz');
  83. $factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  84. $factory->expects($this->once())
  85. ->method('createBuilder')
  86. ->with($this->equalTo($expectedType), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
  87. ->will($this->returnValue($this->getFormBuilder()));
  88. $this->builder->setFormFactory($factory);
  89. $this->builder->add($expectedName, $expectedType, $expectedOptions);
  90. $builder = $this->builder->get($expectedName);
  91. $this->assertNotSame($builder, $this->builder);
  92. }
  93. public function testGetGuessed()
  94. {
  95. $expectedName = 'foo';
  96. $expectedOptions = array('bar' => 'baz');
  97. $factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  98. $factory->expects($this->once())
  99. ->method('createBuilderForProperty')
  100. ->with($this->equalTo('stdClass'), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
  101. ->will($this->returnValue($this->getFormBuilder()));
  102. $this->builder->setFormFactory($factory);
  103. $this->builder->setDataClass('stdClass');
  104. $this->builder->add($expectedName, null, $expectedOptions);
  105. $builder = $this->builder->get($expectedName);
  106. $this->assertNotSame($builder, $this->builder);
  107. }
  108. private function getFormBuilder()
  109. {
  110. return $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. }
  114. }