FormBuilderTest.php 4.7 KB

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