]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/Extension/HttpFoundation/EventListener/BindRequestListenerTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / Extension / HttpFoundation / EventListener / BindRequestListenerTest.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\Extension\HttpFoundation\EventListener;
13
14 use Symfony\Component\Form\Extension\HttpFoundation\EventListener\BindRequestListener;
15 use Symfony\Component\Form\Form;
16 use Symfony\Component\Form\FormConfigBuilder;
17 use Symfony\Component\Form\FormEvent;
18 use Symfony\Component\Form\Test\DeprecationErrorHandler;
19 use Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\HttpFoundation\File\UploadedFile;
21
22 /**
23 * @author Bernhard Schussek <bschussek@gmail.com>
24 */
25 class BindRequestListenerTest extends \PHPUnit_Framework_TestCase
26 {
27 private $values;
28
29 private $filesPlain;
30
31 private $filesNested;
32
33 /**
34 * @var UploadedFile
35 */
36 private $uploadedFile;
37
38 protected function setUp()
39 {
40 $path = tempnam(sys_get_temp_dir(), 'sf2');
41 touch($path);
42
43 $this->values = array(
44 'name' => 'Bernhard',
45 'image' => array('filename' => 'foobar.png'),
46 );
47
48 $this->filesPlain = array(
49 'image' => array(
50 'error' => UPLOAD_ERR_OK,
51 'name' => 'upload.png',
52 'size' => 123,
53 'tmp_name' => $path,
54 'type' => 'image/png'
55 ),
56 );
57
58 $this->filesNested = array(
59 'error' => array('image' => UPLOAD_ERR_OK),
60 'name' => array('image' => 'upload.png'),
61 'size' => array('image' => 123),
62 'tmp_name' => array('image' => $path),
63 'type' => array('image' => 'image/png'),
64 );
65
66 $this->uploadedFile = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
67 }
68
69 protected function tearDown()
70 {
71 unlink($this->uploadedFile->getRealPath());
72 }
73
74 public function requestMethodProvider()
75 {
76 return array(
77 array('POST'),
78 array('PUT'),
79 array('DELETE'),
80 array('PATCH'),
81 );
82 }
83
84 /**
85 * @dataProvider requestMethodProvider
86 */
87 public function testSubmitRequest($method)
88 {
89 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
90 $this->markTestSkipped('The "HttpFoundation" component is not available');
91 }
92
93 $values = array('author' => $this->values);
94 $files = array('author' => $this->filesNested);
95 $request = new Request(array(), $values, array(), array(), $files, array(
96 'REQUEST_METHOD' => $method,
97 ));
98
99 $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
100 $config = new FormConfigBuilder('author', null, $dispatcher);
101 $form = new Form($config);
102 $event = new FormEvent($form, $request);
103
104 $listener = new BindRequestListener();
105 DeprecationErrorHandler::preBind($listener, $event);
106
107 $this->assertEquals(array(
108 'name' => 'Bernhard',
109 'image' => $this->uploadedFile,
110 ), $event->getData());
111 }
112
113 /**
114 * @dataProvider requestMethodProvider
115 */
116 public function testSubmitRequestWithEmptyName($method)
117 {
118 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
119 $this->markTestSkipped('The "HttpFoundation" component is not available');
120 }
121
122 $request = new Request(array(), $this->values, array(), array(), $this->filesPlain, array(
123 'REQUEST_METHOD' => $method,
124 ));
125
126 $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
127 $config = new FormConfigBuilder('', null, $dispatcher);
128 $form = new Form($config);
129 $event = new FormEvent($form, $request);
130
131 $listener = new BindRequestListener();
132 DeprecationErrorHandler::preBind($listener, $event);
133
134 $this->assertEquals(array(
135 'name' => 'Bernhard',
136 'image' => $this->uploadedFile,
137 ), $event->getData());
138 }
139
140 /**
141 * @dataProvider requestMethodProvider
142 */
143 public function testSubmitEmptyRequestToCompoundForm($method)
144 {
145 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
146 $this->markTestSkipped('The "HttpFoundation" component is not available');
147 }
148
149 $request = new Request(array(), array(), array(), array(), array(), array(
150 'REQUEST_METHOD' => $method,
151 ));
152
153 $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
154 $config = new FormConfigBuilder('author', null, $dispatcher);
155 $config->setCompound(true);
156 $config->setDataMapper($this->getMock('Symfony\Component\Form\DataMapperInterface'));
157 $form = new Form($config);
158 $event = new FormEvent($form, $request);
159
160 $listener = new BindRequestListener();
161 DeprecationErrorHandler::preBind($listener, $event);
162
163 // Default to empty array
164 $this->assertEquals(array(), $event->getData());
165 }
166
167 /**
168 * @dataProvider requestMethodProvider
169 */
170 public function testSubmitEmptyRequestToSimpleForm($method)
171 {
172 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
173 $this->markTestSkipped('The "HttpFoundation" component is not available');
174 }
175
176 $request = new Request(array(), array(), array(), array(), array(), array(
177 'REQUEST_METHOD' => $method,
178 ));
179
180 $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
181 $config = new FormConfigBuilder('author', null, $dispatcher);
182 $config->setCompound(false);
183 $form = new Form($config);
184 $event = new FormEvent($form, $request);
185
186 $listener = new BindRequestListener();
187 DeprecationErrorHandler::preBind($listener, $event);
188
189 // Default to null
190 $this->assertNull($event->getData());
191 }
192
193 public function testSubmitGetRequest()
194 {
195 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
196 $this->markTestSkipped('The "HttpFoundation" component is not available');
197 }
198
199 $values = array('author' => $this->values);
200 $request = new Request($values, array(), array(), array(), array(), array(
201 'REQUEST_METHOD' => 'GET',
202 ));
203
204 $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
205 $config = new FormConfigBuilder('author', null, $dispatcher);
206 $form = new Form($config);
207 $event = new FormEvent($form, $request);
208
209 $listener = new BindRequestListener();
210 DeprecationErrorHandler::preBind($listener, $event);
211
212 $this->assertEquals(array(
213 'name' => 'Bernhard',
214 'image' => array('filename' => 'foobar.png'),
215 ), $event->getData());
216 }
217
218 public function testSubmitGetRequestWithEmptyName()
219 {
220 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
221 $this->markTestSkipped('The "HttpFoundation" component is not available');
222 }
223
224 $request = new Request($this->values, array(), array(), array(), array(), array(
225 'REQUEST_METHOD' => 'GET',
226 ));
227
228 $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
229 $config = new FormConfigBuilder('', null, $dispatcher);
230 $form = new Form($config);
231 $event = new FormEvent($form, $request);
232
233 $listener = new BindRequestListener();
234 DeprecationErrorHandler::preBind($listener, $event);
235
236 $this->assertEquals(array(
237 'name' => 'Bernhard',
238 'image' => array('filename' => 'foobar.png'),
239 ), $event->getData());
240 }
241
242 public function testSubmitEmptyGetRequestToCompoundForm()
243 {
244 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
245 $this->markTestSkipped('The "HttpFoundation" component is not available');
246 }
247
248 $request = new Request(array(), array(), array(), array(), array(), array(
249 'REQUEST_METHOD' => 'GET',
250 ));
251
252 $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
253 $config = new FormConfigBuilder('author', null, $dispatcher);
254 $config->setCompound(true);
255 $config->setDataMapper($this->getMock('Symfony\Component\Form\DataMapperInterface'));
256 $form = new Form($config);
257 $event = new FormEvent($form, $request);
258
259 $listener = new BindRequestListener();
260 DeprecationErrorHandler::preBind($listener, $event);
261
262 $this->assertEquals(array(), $event->getData());
263 }
264
265 public function testSubmitEmptyGetRequestToSimpleForm()
266 {
267 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
268 $this->markTestSkipped('The "HttpFoundation" component is not available');
269 }
270
271 $request = new Request(array(), array(), array(), array(), array(), array(
272 'REQUEST_METHOD' => 'GET',
273 ));
274
275 $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
276 $config = new FormConfigBuilder('author', null, $dispatcher);
277 $config->setCompound(false);
278 $form = new Form($config);
279 $event = new FormEvent($form, $request);
280
281 $listener = new BindRequestListener();
282 DeprecationErrorHandler::preBind($listener, $event);
283
284 $this->assertNull($event->getData());
285 }
286 }