FormTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Form;
  12. use Symfony\Component\Form\FormBuilder;
  13. use Symfony\Component\Form\FormError;
  14. class FormTest extends \PHPUnit_Framework_TestCase
  15. {
  16. private $dispatcher;
  17. private $builder;
  18. private $form;
  19. protected function setUp()
  20. {
  21. $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  22. $this->builder = new FormBuilder($this->dispatcher);
  23. $this->form = $this->builder->getForm();
  24. }
  25. public function testErrorsBubbleUpIfEnabled()
  26. {
  27. $error = new FormError('Error!');
  28. $parent = $this->form;
  29. $form = $this->builder->setErrorBubbling(true)->getForm();
  30. $form->setParent($parent);
  31. $form->addError($error);
  32. $this->assertEquals(array(), $form->getErrors());
  33. $this->assertEquals(array($error), $parent->getErrors());
  34. }
  35. public function testErrorsDontBubbleUpIfDisabled()
  36. {
  37. $error = new FormError('Error!');
  38. $parent = $this->form;
  39. $form = $this->builder->setErrorBubbling(false)->getForm();
  40. $form->setParent($parent);
  41. $form->addError($error);
  42. $this->assertEquals(array($error), $form->getErrors());
  43. $this->assertEquals(array(), $parent->getErrors());
  44. }
  45. public function testValidIfAllChildrenAreValid()
  46. {
  47. $this->form->add($this->getValidForm('firstName'));
  48. $this->form->add($this->getValidForm('lastName'));
  49. $this->form->bind(array(
  50. 'firstName' => 'Bernhard',
  51. 'lastName' => 'Schussek',
  52. ));
  53. $this->assertTrue($this->form->isValid());
  54. }
  55. public function testInvalidIfChildrenIsInvalid()
  56. {
  57. $this->form->add($this->getValidForm('firstName'));
  58. $this->form->add($this->getInvalidForm('lastName'));
  59. $this->form->bind(array(
  60. 'firstName' => 'Bernhard',
  61. 'lastName' => 'Schussek',
  62. ));
  63. $this->assertFalse($this->form->isValid());
  64. }
  65. public function testBind()
  66. {
  67. $child = $this->getMockForm('firstName');
  68. $this->form->add($child);
  69. $child->expects($this->once())
  70. ->method('bind')
  71. ->with($this->equalTo('Bernhard'));
  72. $this->form->bind(array('firstName' => 'Bernhard'));
  73. $this->assertEquals(array('firstName' => 'Bernhard'), $this->form->getData());
  74. }
  75. public function testBindForwardsNullIfValueIsMissing()
  76. {
  77. $child = $this->getMockForm('firstName');
  78. $this->form->add($child);
  79. $child->expects($this->once())
  80. ->method('bind')
  81. ->with($this->equalTo(null));
  82. $this->form->bind(array());
  83. }
  84. public function testAddSetsFieldParent()
  85. {
  86. $child = $this->getMockForm('firstName');
  87. $child->expects($this->once())
  88. ->method('setParent')
  89. ->with($this->equalTo($this->form));
  90. $this->form->add($child);
  91. }
  92. protected function getMockForm($name)
  93. {
  94. $form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
  95. $form->expects($this->any())
  96. ->method('getName')
  97. ->will($this->returnValue($name));
  98. return $form;
  99. }
  100. protected function getValidForm($name)
  101. {
  102. $form = $this->getMockForm($name);
  103. $form->expects($this->any())
  104. ->method('isValid')
  105. ->will($this->returnValue(true));
  106. return $form;
  107. }
  108. protected function getInvalidForm($name)
  109. {
  110. $form = $this->getMockForm($name);
  111. $form->expects($this->any())
  112. ->method('isValid')
  113. ->will($this->returnValue(false));
  114. return $form;
  115. }
  116. }