FormBuilderTest.php 4.8 KB

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