FormBuilderTest.php 4.6 KB

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