]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / AbstractRequestHandlerTest.php
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 /**
15 * @author Bernhard Schussek <bschussek@gmail.com>
16 */
17 abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
18 {
19 /**
20 * @var \Symfony\Component\Form\RequestHandlerInterface
21 */
22 protected $requestHandler;
23
24 protected $request;
25
26 protected function setUp()
27 {
28 $this->requestHandler = $this->getRequestHandler();
29 $this->request = null;
30 }
31
32 public function methodExceptGetProvider()
33 {
34 return array(
35 array('POST'),
36 array('PUT'),
37 array('DELETE'),
38 array('PATCH'),
39 );
40 }
41
42 public function methodProvider()
43 {
44 return array_merge(array(
45 array('GET'),
46 ), $this->methodExceptGetProvider());
47 }
48
49 /**
50 * @dataProvider methodProvider
51 */
52 public function testSubmitIfNameInRequest($method)
53 {
54 $form = $this->getMockForm('param1', $method);
55
56 $this->setRequestData($method, array(
57 'param1' => 'DATA',
58 ));
59
60 $form->expects($this->once())
61 ->method('submit')
62 ->with('DATA', 'PATCH' !== $method);
63
64 $this->requestHandler->handleRequest($form, $this->request);
65 }
66
67 /**
68 * @dataProvider methodProvider
69 */
70 public function testDoNotSubmitIfWrongRequestMethod($method)
71 {
72 $form = $this->getMockForm('param1', $method);
73
74 $otherMethod = 'POST' === $method ? 'PUT' : 'POST';
75
76 $this->setRequestData($otherMethod, array(
77 'param1' => 'DATA',
78 ));
79
80 $form->expects($this->never())
81 ->method('submit');
82
83 $this->requestHandler->handleRequest($form, $this->request);
84 }
85
86 /**
87 * @dataProvider methodExceptGetProvider
88 */
89 public function testSubmitSimpleFormWithNullIfNameNotInRequestAndNotGetRequest($method)
90 {
91 $form = $this->getMockForm('param1', $method, false);
92
93 $this->setRequestData($method, array(
94 'paramx' => array(),
95 ));
96
97 $form->expects($this->once())
98 ->method('submit')
99 ->with($this->identicalTo(null), 'PATCH' !== $method);
100
101 $this->requestHandler->handleRequest($form, $this->request);
102 }
103
104 /**
105 * @dataProvider methodExceptGetProvider
106 */
107 public function testSubmitCompoundFormWithArrayIfNameNotInRequestAndNotGetRequest($method)
108 {
109 $form = $this->getMockForm('param1', $method, true);
110
111 $this->setRequestData($method, array(
112 'paramx' => array(),
113 ));
114
115 $form->expects($this->once())
116 ->method('submit')
117 ->with($this->identicalTo(array()), 'PATCH' !== $method);
118
119 $this->requestHandler->handleRequest($form, $this->request);
120 }
121
122 public function testDoNotSubmitIfNameNotInRequestAndGetRequest()
123 {
124 $form = $this->getMockForm('param1', 'GET');
125
126 $this->setRequestData('GET', array(
127 'paramx' => array(),
128 ));
129
130 $form->expects($this->never())
131 ->method('submit');
132
133 $this->requestHandler->handleRequest($form, $this->request);
134 }
135
136 /**
137 * @dataProvider methodProvider
138 */
139 public function testSubmitFormWithEmptyNameIfAtLeastOneFieldInRequest($method)
140 {
141 $form = $this->getMockForm('', $method);
142 $form->expects($this->any())
143 ->method('all')
144 ->will($this->returnValue(array(
145 'param1' => $this->getMockForm('param1'),
146 'param2' => $this->getMockForm('param2'),
147 )));
148
149 $this->setRequestData($method, $requestData = array(
150 'param1' => 'submitted value',
151 'paramx' => 'submitted value',
152 ));
153
154 $form->expects($this->once())
155 ->method('submit')
156 ->with($requestData, 'PATCH' !== $method);
157
158 $this->requestHandler->handleRequest($form, $this->request);
159 }
160
161 /**
162 * @dataProvider methodProvider
163 */
164 public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest($method)
165 {
166 $form = $this->getMockForm('', $method);
167 $form->expects($this->any())
168 ->method('all')
169 ->will($this->returnValue(array(
170 'param1' => $this->getMockForm('param1'),
171 'param2' => $this->getMockForm('param2'),
172 )));
173
174 $this->setRequestData($method, array(
175 'paramx' => 'submitted value',
176 ));
177
178 $form->expects($this->never())
179 ->method('submit');
180
181 $this->requestHandler->handleRequest($form, $this->request);
182 }
183
184 /**
185 * @dataProvider methodExceptGetProvider
186 */
187 public function testMergeParamsAndFiles($method)
188 {
189 $form = $this->getMockForm('param1', $method);
190 $file = $this->getMockFile();
191
192 $this->setRequestData($method, array(
193 'param1' => array(
194 'field1' => 'DATA',
195 ),
196 ), array(
197 'param1' => array(
198 'field2' => $file,
199 ),
200 ));
201
202 $form->expects($this->once())
203 ->method('submit')
204 ->with(array(
205 'field1' => 'DATA',
206 'field2' => $file,
207 ), 'PATCH' !== $method);
208
209 $this->requestHandler->handleRequest($form, $this->request);
210 }
211
212 /**
213 * @dataProvider methodExceptGetProvider
214 */
215 public function testParamTakesPrecedenceOverFile($method)
216 {
217 $form = $this->getMockForm('param1', $method);
218 $file = $this->getMockFile();
219
220 $this->setRequestData($method, array(
221 'param1' => 'DATA',
222 ), array(
223 'param1' => $file,
224 ));
225
226 $form->expects($this->once())
227 ->method('submit')
228 ->with('DATA', 'PATCH' !== $method);
229
230 $this->requestHandler->handleRequest($form, $this->request);
231 }
232
233 /**
234 * @dataProvider methodExceptGetProvider
235 */
236 public function testSubmitFileIfNoParam($method)
237 {
238 $form = $this->getMockForm('param1', $method);
239 $file = $this->getMockFile();
240
241 $this->setRequestData($method, array(
242 'param1' => null,
243 ), array(
244 'param1' => $file,
245 ));
246
247 $form->expects($this->once())
248 ->method('submit')
249 ->with($file, 'PATCH' !== $method);
250
251 $this->requestHandler->handleRequest($form, $this->request);
252 }
253
254 abstract protected function setRequestData($method, $data, $files = array());
255
256 abstract protected function getRequestHandler();
257
258 abstract protected function getMockFile();
259
260 protected function getMockForm($name, $method = null, $compound = true)
261 {
262 $config = $this->getMock('Symfony\Component\Form\FormConfigInterface');
263 $config->expects($this->any())
264 ->method('getMethod')
265 ->will($this->returnValue($method));
266 $config->expects($this->any())
267 ->method('getCompound')
268 ->will($this->returnValue($compound));
269
270 $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
271 $form->expects($this->any())
272 ->method('getName')
273 ->will($this->returnValue($name));
274 $form->expects($this->any())
275 ->method('getConfig')
276 ->will($this->returnValue($config));
277
278 return $form;
279 }
280 }