|
20 | 20 | use Symfony\Component\Form\HiddenField;
|
21 | 21 | use Symfony\Component\Form\FieldGroup;
|
22 | 22 | use Symfony\Component\Form\PropertyPath;
|
| 23 | +use Symfony\Component\HttpFoundation\Request; |
| 24 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
23 | 25 | use Symfony\Component\Validator\ConstraintViolation;
|
24 | 26 | use Symfony\Component\Validator\ConstraintViolationList;
|
25 | 27 | use Symfony\Tests\Component\Form\Fixtures\Author;
|
@@ -222,26 +224,75 @@ public function testBindThrowsExceptionIfNoValidatorIsSet()
|
222 | 224 | $form->bind(array()); // irrelevant
|
223 | 225 | }
|
224 | 226 |
|
225 |
| - public function testMultipartFormsWithoutParentsRequireFiles() |
| 227 | + public function testBindRequest() |
226 | 228 | {
|
227 |
| - $form = new Form('author', new Author(), $this->validator); |
228 |
| - $form->add($this->createMultipartMockField('file')); |
| 229 | + $values = array( |
| 230 | + 'author' => array( |
| 231 | + 'name' => 'Bernhard', |
| 232 | + 'image' => array('filename' => 'foobar.png'), |
| 233 | + ), |
| 234 | + ); |
| 235 | + $files = array( |
| 236 | + 'author' => array( |
| 237 | + 'error' => array('image' => array('file' => UPLOAD_ERR_OK)), |
| 238 | + 'name' => array('image' => array('file' => 'upload.png')), |
| 239 | + 'size' => array('image' => array('file' => 123)), |
| 240 | + 'tmp_name' => array('image' => array('file' => 'abcdef.png')), |
| 241 | + 'type' => array('image' => array('file' => 'image/png')), |
| 242 | + ), |
| 243 | + ); |
| 244 | + |
| 245 | + $request = new Request(array(), $values, array(), array(), $files); |
| 246 | + |
| 247 | + $form = new Form('author', null, $this->createMockValidator()); |
| 248 | + $form->add(new TestField('name')); |
| 249 | + $imageForm = new FieldGroup('image'); |
| 250 | + $imageForm->add(new TestField('file')); |
| 251 | + $imageForm->add(new TestField('filename')); |
| 252 | + $form->add($imageForm); |
| 253 | + |
| 254 | + $form->bindRequest($request); |
229 | 255 |
|
230 |
| - $this->setExpectedException('InvalidArgumentException'); |
| 256 | + $file = new UploadedFile('abcdef.png', 'upload.png', 'image/png', 123, UPLOAD_ERR_OK); |
231 | 257 |
|
232 |
| - // should be given in second argument |
233 |
| - $form->bind(array('file' => 'test.txt')); |
| 258 | + $this->assertEquals('Bernhard', $form['name']->getData()); |
| 259 | + $this->assertEquals('foobar.png', $form['image']['filename']->getData()); |
| 260 | + $this->assertEquals($file, $form['image']['file']->getData()); |
234 | 261 | }
|
235 | 262 |
|
236 |
| - public function testMultipartFormsWithParentsRequireNoFiles() |
| 263 | + public function testBindGlobals() |
237 | 264 | {
|
238 |
| - $form = new Form('author', new Author(), $this->validator); |
239 |
| - $form->add($this->createMultipartMockField('file')); |
| 265 | + $_POST = array( |
| 266 | + 'author' => array( |
| 267 | + 'name' => 'Bernhard', |
| 268 | + 'image' => array('filename' => 'foobar.png'), |
| 269 | + ), |
| 270 | + ); |
| 271 | + $_FILES = array( |
| 272 | + 'author' => array( |
| 273 | + 'error' => array('image' => array('file' => UPLOAD_ERR_OK)), |
| 274 | + 'name' => array('image' => array('file' => 'upload.png')), |
| 275 | + 'size' => array('image' => array('file' => 123)), |
| 276 | + 'tmp_name' => array('image' => array('file' => 'abcdef.png')), |
| 277 | + 'type' => array('image' => array('file' => 'image/png')), |
| 278 | + ), |
| 279 | + ); |
| 280 | + |
| 281 | + |
| 282 | + $form = new Form('author', null, $this->createMockValidator()); |
| 283 | + $form->add(new TestField('name')); |
| 284 | + $imageForm = new FieldGroup('image'); |
| 285 | + $imageForm->add(new TestField('file')); |
| 286 | + $imageForm->add(new TestField('filename')); |
| 287 | + $form->add($imageForm); |
| 288 | + |
| 289 | + $form->bindGlobals(); |
240 | 290 |
|
241 |
| - $form->setParent($this->createMockField('group')); |
| 291 | + $file = new UploadedFile('abcdef.png', 'upload.png', 'image/png', 123, UPLOAD_ERR_OK); |
242 | 292 |
|
243 |
| - // files are expected to be converted by the parent |
244 |
| - $form->bind(array('file' => 'test.txt')); |
| 293 | + $this->assertEquals('Bernhard', $form['name']->getData()); |
| 294 | + $this->assertEquals('foobar.png', $form['image']['filename']->getData()); |
| 295 | + $this->assertEquals($file, $form['image']['file']->getData()); |
245 | 296 | }
|
246 | 297 |
|
247 | 298 | public function testUpdateFromPropertyIsIgnoredIfFormHasObject()
|
|
0 commit comments