FormConfigTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\FormConfigBuilder;
  13. /**
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class FormConfigTest extends TestCase
  17. {
  18. public function getHtml4Ids()
  19. {
  20. return array(
  21. array('z0'),
  22. array('A0'),
  23. array('A9'),
  24. array('Z0'),
  25. array('#', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
  26. array('a#', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
  27. array('a$', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
  28. array('a%', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
  29. array('a ', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
  30. array("a\t", 'Symfony\Component\Form\Exception\InvalidArgumentException'),
  31. array("a\n", 'Symfony\Component\Form\Exception\InvalidArgumentException'),
  32. array('a-'),
  33. array('a_'),
  34. array('a:'),
  35. // Periods are allowed by the HTML4 spec, but disallowed by us
  36. // because they break the generated property paths
  37. array('a.', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
  38. // Contrary to the HTML4 spec, we allow names starting with a
  39. // number, otherwise naming fields by collection indices is not
  40. // possible.
  41. // For root forms, leading digits will be stripped from the
  42. // "id" attribute to produce valid HTML4.
  43. array('0'),
  44. array('9'),
  45. // Contrary to the HTML4 spec, we allow names starting with an
  46. // underscore, since this is already a widely used practice in
  47. // Symfony.
  48. // For root forms, leading underscores will be stripped from the
  49. // "id" attribute to produce valid HTML4.
  50. array('_'),
  51. // Integers are allowed
  52. array(0),
  53. array(123),
  54. // NULL is allowed
  55. array(null),
  56. // Other types are not
  57. array(1.23, 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
  58. array(5., 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
  59. array(true, 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
  60. array(new \stdClass(), 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
  61. );
  62. }
  63. /**
  64. * @dataProvider getHtml4Ids
  65. */
  66. public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $expectedException = null)
  67. {
  68. $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  69. if (null !== $expectedException && method_exists($this, 'expectException')) {
  70. $this->expectException($expectedException);
  71. } elseif (null !== $expectedException) {
  72. $this->setExpectedException($expectedException);
  73. }
  74. $formConfigBuilder = new FormConfigBuilder($name, null, $dispatcher);
  75. $this->assertSame((string) $name, $formConfigBuilder->getName());
  76. }
  77. public function testGetRequestHandlerCreatesNativeRequestHandlerIfNotSet()
  78. {
  79. $config = $this->getConfigBuilder()->getFormConfig();
  80. $this->assertInstanceOf('Symfony\Component\Form\NativeRequestHandler', $config->getRequestHandler());
  81. }
  82. public function testGetRequestHandlerReusesNativeRequestHandlerInstance()
  83. {
  84. $config1 = $this->getConfigBuilder()->getFormConfig();
  85. $config2 = $this->getConfigBuilder()->getFormConfig();
  86. $this->assertSame($config1->getRequestHandler(), $config2->getRequestHandler());
  87. }
  88. public function testSetMethodAllowsGet()
  89. {
  90. $formConfigBuilder = $this->getConfigBuilder();
  91. $formConfigBuilder->setMethod('GET');
  92. self::assertSame('GET', $formConfigBuilder->getMethod());
  93. }
  94. public function testSetMethodAllowsPost()
  95. {
  96. $formConfigBuilder = $this->getConfigBuilder();
  97. $formConfigBuilder->setMethod('POST');
  98. self::assertSame('POST', $formConfigBuilder->getMethod());
  99. }
  100. public function testSetMethodAllowsPut()
  101. {
  102. $formConfigBuilder = $this->getConfigBuilder();
  103. $formConfigBuilder->setMethod('PUT');
  104. self::assertSame('PUT', $formConfigBuilder->getMethod());
  105. }
  106. public function testSetMethodAllowsDelete()
  107. {
  108. $formConfigBuilder = $this->getConfigBuilder();
  109. $formConfigBuilder->setMethod('DELETE');
  110. self::assertSame('DELETE', $formConfigBuilder->getMethod());
  111. }
  112. public function testSetMethodAllowsPatch()
  113. {
  114. $formConfigBuilder = $this->getConfigBuilder();
  115. $formConfigBuilder->setMethod('PATCH');
  116. self::assertSame('PATCH', $formConfigBuilder->getMethod());
  117. }
  118. /**
  119. * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
  120. */
  121. public function testSetMethodDoesNotAllowOtherValues()
  122. {
  123. $this->getConfigBuilder()->setMethod('foo');
  124. }
  125. private function getConfigBuilder($name = 'name')
  126. {
  127. $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  128. return new FormConfigBuilder($name, null, $dispatcher);
  129. }
  130. }