FormBuilderTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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->factory->expects($this->once())
  84. ->method('createNamedBuilder')
  85. ->with('text', 'foo', null, array())
  86. ;
  87. $builder = $this->builder->create('foo');
  88. }
  89. public function testGetUnknown()
  90. {
  91. $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The field "foo" does not exist');
  92. $this->builder->get('foo');
  93. }
  94. public function testGetTyped()
  95. {
  96. $expectedType = 'text';
  97. $expectedName = 'foo';
  98. $expectedOptions = array('bar' => 'baz');
  99. $this->factory->expects($this->once())
  100. ->method('createNamedBuilder')
  101. ->with($expectedType, $expectedName, null, $expectedOptions)
  102. ->will($this->returnValue($this->getFormBuilder()));
  103. $this->builder->add($expectedName, $expectedType, $expectedOptions);
  104. $builder = $this->builder->get($expectedName);
  105. $this->assertNotSame($builder, $this->builder);
  106. }
  107. public function testGetGuessed()
  108. {
  109. $expectedName = 'foo';
  110. $expectedOptions = array('bar' => 'baz');
  111. $this->factory->expects($this->once())
  112. ->method('createBuilderForProperty')
  113. ->with('stdClass', $expectedName, null, $expectedOptions)
  114. ->will($this->returnValue($this->getFormBuilder()));
  115. $this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
  116. $this->builder->add($expectedName, null, $expectedOptions);
  117. $builder = $this->builder->get($expectedName);
  118. $this->assertNotSame($builder, $this->builder);
  119. }
  120. private function getFormBuilder()
  121. {
  122. return $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. }
  126. }