FormBuilderTest.php 4.5 KB

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