4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests
;
14 use Symfony\Component\Form\Exception\UnexpectedTypeException
;
15 use Symfony\Component\Form\FormConfigBuilder
;
16 use Symfony\Component\Form\Exception\InvalidArgumentException
;
19 * @author Bernhard Schussek <bschussek@gmail.com>
21 class FormConfigTest
extends \PHPUnit_Framework_TestCase
23 public function getHtml4Ids()
40 // Periods are allowed by the HTML4 spec, but disallowed by us
41 // because they break the generated property paths
43 // Contrary to the HTML4 spec, we allow names starting with a
44 // number, otherwise naming fields by collection indices is not
46 // For root forms, leading digits will be stripped from the
47 // "id" attribute to produce valid HTML4.
50 // Contrary to the HTML4 spec, we allow names starting with an
51 // underscore, since this is already a widely used practice in
53 // For root forms, leading underscores will be stripped from the
54 // "id" attribute to produce valid HTML4.
56 // Integers are allowed
61 // Other types are not
65 array(new \stdClass(), false),
70 * @dataProvider getHtml4Ids
72 public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $accepted)
74 $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface
');
77 new FormConfigBuilder($name, null, $dispatcher);
79 $this->fail(sprintf('The value
"%s" should not be accepted
', $name));
81 } catch (UnexpectedTypeException $e) {
82 // if the value was not accepted, but should be, rethrow exception
86 } catch (InvalidArgumentException $e) {
87 // if the value was not accepted, but should be, rethrow exception
94 public function testGetRequestHandlerCreatesNativeRequestHandlerIfNotSet()
96 $config = $this->getConfigBuilder()->getFormConfig();
98 $this->assertInstanceOf('Symfony\Component\Form\NativeRequestHandler
', $config->getRequestHandler());
101 public function testGetRequestHandlerReusesNativeRequestHandlerInstance()
103 $config1 = $this->getConfigBuilder()->getFormConfig();
104 $config2 = $this->getConfigBuilder()->getFormConfig();
106 $this->assertSame($config1->getRequestHandler(), $config2->getRequestHandler());
109 public function testSetMethodAllowsGet()
111 $this->getConfigBuilder()->setMethod('GET
');
114 public function testSetMethodAllowsPost()
116 $this->getConfigBuilder()->setMethod('POST
');
119 public function testSetMethodAllowsPut()
121 $this->getConfigBuilder()->setMethod('PUT
');
124 public function testSetMethodAllowsDelete()
126 $this->getConfigBuilder()->setMethod('DELETE
');
129 public function testSetMethodAllowsPatch()
131 $this->getConfigBuilder()->setMethod('PATCH
');
135 * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
137 public function testSetMethodDoesNotAllowOtherValues()
139 $this->getConfigBuilder()->setMethod('foo
');
142 private function getConfigBuilder($name = 'name
')
144 $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface
');
146 return new FormConfigBuilder($name, null, $dispatcher);