FormBuilderTest.php 4.7 KB

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