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\Extension\HttpFoundation\EventListener
;
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
;
23 * @author Bernhard Schussek <bschussek@gmail.com>
25 class BindRequestListenerTest
extends \PHPUnit_Framework_TestCase
36 private $uploadedFile;
38 protected function setUp()
40 $path = tempnam(sys_get_temp_dir(), 'sf2');
43 $this->values
= array(
45 'image' => array('filename' => 'foobar.png'),
48 $this->filesPlain
= array(
50 'error' => UPLOAD_ERR_OK
,
51 'name' => 'upload.png',
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'),
66 $this->uploadedFile
= new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK
);
69 protected function tearDown()
71 unlink($this->uploadedFile
->getRealPath());
74 public function requestMethodProvider()
85 * @dataProvider requestMethodProvider
87 public function testSubmitRequest($method)
89 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
90 $this->markTestSkipped('The "HttpFoundation" component is not available');
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,
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);
104 $listener = new BindRequestListener();
105 DeprecationErrorHandler
::preBind($listener, $event);
107 $this->assertEquals(array(
108 'name' => 'Bernhard',
109 'image' => $this->uploadedFile
,
110 ), $event->getData());
114 * @dataProvider requestMethodProvider
116 public function testSubmitRequestWithEmptyName($method)
118 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
119 $this->markTestSkipped('The "HttpFoundation" component is not available');
122 $request = new Request(array(), $this->values
, array(), array(), $this->filesPlain
, array(
123 'REQUEST_METHOD' => $method,
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);
131 $listener = new BindRequestListener();
132 DeprecationErrorHandler
::preBind($listener, $event);
134 $this->assertEquals(array(
135 'name' => 'Bernhard',
136 'image' => $this->uploadedFile
,
137 ), $event->getData());
141 * @dataProvider requestMethodProvider
143 public function testSubmitEmptyRequestToCompoundForm($method)
145 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
146 $this->markTestSkipped('The "HttpFoundation" component is not available');
149 $request = new Request(array(), array(), array(), array(), array(), array(
150 'REQUEST_METHOD' => $method,
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);
160 $listener = new BindRequestListener();
161 DeprecationErrorHandler
::preBind($listener, $event);
163 // Default to empty array
164 $this->assertEquals(array(), $event->getData());
168 * @dataProvider requestMethodProvider
170 public function testSubmitEmptyRequestToSimpleForm($method)
172 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
173 $this->markTestSkipped('The "HttpFoundation" component is not available');
176 $request = new Request(array(), array(), array(), array(), array(), array(
177 'REQUEST_METHOD' => $method,
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);
186 $listener = new BindRequestListener();
187 DeprecationErrorHandler
::preBind($listener, $event);
190 $this->assertNull($event->getData());
193 public function testSubmitGetRequest()
195 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
196 $this->markTestSkipped('The "HttpFoundation" component is not available');
199 $values = array('author' => $this->values
);
200 $request = new Request($values, array(), array(), array(), array(), array(
201 'REQUEST_METHOD' => 'GET',
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);
209 $listener = new BindRequestListener();
210 DeprecationErrorHandler
::preBind($listener, $event);
212 $this->assertEquals(array(
213 'name' => 'Bernhard',
214 'image' => array('filename' => 'foobar.png'),
215 ), $event->getData());
218 public function testSubmitGetRequestWithEmptyName()
220 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
221 $this->markTestSkipped('The "HttpFoundation" component is not available');
224 $request = new Request($this->values
, array(), array(), array(), array(), array(
225 'REQUEST_METHOD' => 'GET',
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);
233 $listener = new BindRequestListener();
234 DeprecationErrorHandler
::preBind($listener, $event);
236 $this->assertEquals(array(
237 'name' => 'Bernhard',
238 'image' => array('filename' => 'foobar.png'),
239 ), $event->getData());
242 public function testSubmitEmptyGetRequestToCompoundForm()
244 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
245 $this->markTestSkipped('The "HttpFoundation" component is not available');
248 $request = new Request(array(), array(), array(), array(), array(), array(
249 'REQUEST_METHOD' => 'GET',
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);
259 $listener = new BindRequestListener();
260 DeprecationErrorHandler
::preBind($listener, $event);
262 $this->assertEquals(array(), $event->getData());
265 public function testSubmitEmptyGetRequestToSimpleForm()
267 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
268 $this->markTestSkipped('The "HttpFoundation" component is not available');
271 $request = new Request(array(), array(), array(), array(), array(), array(
272 'REQUEST_METHOD' => 'GET',
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);
281 $listener = new BindRequestListener();
282 DeprecationErrorHandler
::preBind($listener, $event);
284 $this->assertNull($event->getData());