FormBuilderTest.php 4.7 KB

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