AbstractFormTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\EventDispatcher\EventDispatcher;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\Form\FormBuilder;
  15. abstract class AbstractFormTest extends TestCase
  16. {
  17. /**
  18. * @var EventDispatcherInterface
  19. */
  20. protected $dispatcher;
  21. /**
  22. * @var \Symfony\Component\Form\FormFactoryInterface
  23. */
  24. protected $factory;
  25. /**
  26. * @var \Symfony\Component\Form\FormInterface
  27. */
  28. protected $form;
  29. protected function setUp()
  30. {
  31. // We need an actual dispatcher to use the deprecated
  32. // bindRequest() method
  33. $this->dispatcher = new EventDispatcher();
  34. $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
  35. $this->form = $this->createForm();
  36. }
  37. protected function tearDown()
  38. {
  39. $this->dispatcher = null;
  40. $this->factory = null;
  41. $this->form = null;
  42. }
  43. /**
  44. * @return \Symfony\Component\Form\FormInterface
  45. */
  46. abstract protected function createForm();
  47. /**
  48. * @param string $name
  49. * @param EventDispatcherInterface $dispatcher
  50. * @param string $dataClass
  51. * @param array $options
  52. *
  53. * @return FormBuilder
  54. */
  55. protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null, $dataClass = null, array $options = array())
  56. {
  57. return new FormBuilder($name, $dataClass, $dispatcher ?: $this->dispatcher, $this->factory, $options);
  58. }
  59. /**
  60. * @param string $name
  61. *
  62. * @return \PHPUnit_Framework_MockObject_MockObject
  63. */
  64. protected function getMockForm($name = 'name')
  65. {
  66. $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
  67. $config = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')->getMock();
  68. $form->expects($this->any())
  69. ->method('getName')
  70. ->will($this->returnValue($name));
  71. $form->expects($this->any())
  72. ->method('getConfig')
  73. ->will($this->returnValue($config));
  74. return $form;
  75. }
  76. /**
  77. * @return \PHPUnit_Framework_MockObject_MockObject
  78. */
  79. protected function getDataMapper()
  80. {
  81. return $this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock();
  82. }
  83. /**
  84. * @return \PHPUnit_Framework_MockObject_MockObject
  85. */
  86. protected function getDataTransformer()
  87. {
  88. return $this->getMockBuilder('Symfony\Component\Form\DataTransformerInterface')->getMock();
  89. }
  90. /**
  91. * @return \PHPUnit_Framework_MockObject_MockObject
  92. */
  93. protected function getFormValidator()
  94. {
  95. return $this->getMockBuilder('Symfony\Component\Form\FormValidatorInterface')->getMock();
  96. }
  97. }