]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Symfony\Component\Form\Tests; | |
13 | ||
14 | use Symfony\Component\Form\Exception\UnexpectedTypeException; | |
15 | use Symfony\Component\Form\FormConfigBuilder; | |
16 | use Symfony\Component\Form\Exception\InvalidArgumentException; | |
17 | ||
18 | /** | |
19 | * @author Bernhard Schussek <bschussek@gmail.com> | |
20 | */ | |
21 | class FormConfigTest extends \PHPUnit_Framework_TestCase | |
22 | { | |
23 | public function getHtml4Ids() | |
24 | { | |
25 | return array( | |
26 | array('z0', true), | |
27 | array('A0', true), | |
28 | array('A9', true), | |
29 | array('Z0', true), | |
30 | array('#', false), | |
31 | array('a#', false), | |
32 | array('a$', false), | |
33 | array('a%', false), | |
34 | array('a ', false), | |
35 | array("a\t", false), | |
36 | array("a\n", false), | |
37 | array('a-', true), | |
38 | array('a_', true), | |
39 | array('a:', true), | |
40 | // Periods are allowed by the HTML4 spec, but disallowed by us | |
41 | // because they break the generated property paths | |
42 | array('a.', false), | |
43 | // Contrary to the HTML4 spec, we allow names starting with a | |
44 | // number, otherwise naming fields by collection indices is not | |
45 | // possible. | |
46 | // For root forms, leading digits will be stripped from the | |
47 | // "id" attribute to produce valid HTML4. | |
48 | array('0', true), | |
49 | array('9', true), | |
50 | // Contrary to the HTML4 spec, we allow names starting with an | |
51 | // underscore, since this is already a widely used practice in | |
52 | // Symfony2. | |
53 | // For root forms, leading underscores will be stripped from the | |
54 | // "id" attribute to produce valid HTML4. | |
55 | array('_', true), | |
56 | // Integers are allowed | |
57 | array(0, true), | |
58 | array(123, true), | |
59 | // NULL is allowed | |
60 | array(null, true), | |
61 | // Other types are not | |
62 | array(1.23, false), | |
63 | array(5., false), | |
64 | array(true, false), | |
65 | array(new \stdClass(), false), | |
66 | ); | |
67 | } | |
68 | ||
69 | /** | |
70 | * @dataProvider getHtml4Ids | |
71 | */ | |
72 | public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $accepted) | |
73 | { | |
74 | $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); | |
75 | ||
76 | try { | |
77 | new FormConfigBuilder($name, null, $dispatcher); | |
78 | if (!$accepted) { | |
79 | $this->fail(sprintf('The value "%s" should not be accepted', $name)); | |
80 | } | |
81 | } catch (UnexpectedTypeException $e) { | |
82 | // if the value was not accepted, but should be, rethrow exception | |
83 | if ($accepted) { | |
84 | throw $e; | |
85 | } | |
86 | } catch (InvalidArgumentException $e) { | |
87 | // if the value was not accepted, but should be, rethrow exception | |
88 | if ($accepted) { | |
89 | throw $e; | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | public function testGetRequestHandlerCreatesNativeRequestHandlerIfNotSet() | |
95 | { | |
96 | $config = $this->getConfigBuilder()->getFormConfig(); | |
97 | ||
98 | $this->assertInstanceOf('Symfony\Component\Form\NativeRequestHandler', $config->getRequestHandler()); | |
99 | } | |
100 | ||
101 | public function testGetRequestHandlerReusesNativeRequestHandlerInstance() | |
102 | { | |
103 | $config1 = $this->getConfigBuilder()->getFormConfig(); | |
104 | $config2 = $this->getConfigBuilder()->getFormConfig(); | |
105 | ||
106 | $this->assertSame($config1->getRequestHandler(), $config2->getRequestHandler()); | |
107 | } | |
108 | ||
109 | public function testSetMethodAllowsGet() | |
110 | { | |
111 | $this->getConfigBuilder()->setMethod('GET'); | |
112 | } | |
113 | ||
114 | public function testSetMethodAllowsPost() | |
115 | { | |
116 | $this->getConfigBuilder()->setMethod('POST'); | |
117 | } | |
118 | ||
119 | public function testSetMethodAllowsPut() | |
120 | { | |
121 | $this->getConfigBuilder()->setMethod('PUT'); | |
122 | } | |
123 | ||
124 | public function testSetMethodAllowsDelete() | |
125 | { | |
126 | $this->getConfigBuilder()->setMethod('DELETE'); | |
127 | } | |
128 | ||
129 | public function testSetMethodAllowsPatch() | |
130 | { | |
131 | $this->getConfigBuilder()->setMethod('PATCH'); | |
132 | } | |
133 | ||
134 | /** | |
135 | * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException | |
136 | */ | |
137 | public function testSetMethodDoesNotAllowOtherValues() | |
138 | { | |
139 | $this->getConfigBuilder()->setMethod('foo'); | |
140 | } | |
141 | ||
142 | private function getConfigBuilder($name = 'name') | |
143 | { | |
144 | $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); | |
145 | ||
146 | return new FormConfigBuilder($name, null, $dispatcher); | |
147 | } | |
148 | } |