ButtonTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Component\Form\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Form\ButtonBuilder;
  13. use Symfony\Component\Form\FormBuilder;
  14. /**
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class ButtonTest extends TestCase
  18. {
  19. private $dispatcher;
  20. private $factory;
  21. protected function setUp()
  22. {
  23. $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  24. $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
  25. }
  26. /**
  27. * @expectedException \Symfony\Component\Form\Exception\AlreadySubmittedException
  28. */
  29. public function testSetParentOnSubmittedButton()
  30. {
  31. $button = $this->getButtonBuilder('button')
  32. ->getForm()
  33. ;
  34. $button->submit('');
  35. $button->setParent($this->getFormBuilder('form')->getForm());
  36. }
  37. /**
  38. * @dataProvider getDisabledStates
  39. */
  40. public function testDisabledIfParentIsDisabled($parentDisabled, $buttonDisabled, $result)
  41. {
  42. $form = $this->getFormBuilder('form')
  43. ->setDisabled($parentDisabled)
  44. ->getForm()
  45. ;
  46. $button = $this->getButtonBuilder('button')
  47. ->setDisabled($buttonDisabled)
  48. ->getForm()
  49. ;
  50. $button->setParent($form);
  51. $this->assertSame($result, $button->isDisabled());
  52. }
  53. public function getDisabledStates()
  54. {
  55. return array(
  56. // parent, button, result
  57. array(true, true, true),
  58. array(true, false, true),
  59. array(false, true, true),
  60. array(false, false, false),
  61. );
  62. }
  63. private function getButtonBuilder($name)
  64. {
  65. return new ButtonBuilder($name);
  66. }
  67. private function getFormBuilder($name)
  68. {
  69. return new FormBuilder($name, null, $this->dispatcher, $this->factory);
  70. }
  71. }