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
;
15 * @author Bernhard Schussek <bschussek@gmail.com>
17 abstract class AbstractRequestHandlerTest
extends \PHPUnit_Framework_TestCase
20 * @var \Symfony\Component\Form\RequestHandlerInterface
22 protected $requestHandler;
26 protected function setUp()
28 $this->requestHandler
= $this->getRequestHandler();
29 $this->request
= null;
32 public function methodExceptGetProvider()
42 public function methodProvider()
44 return array_merge(array(
46 ), $this->methodExceptGetProvider());
50 * @dataProvider methodProvider
52 public function testSubmitIfNameInRequest($method)
54 $form = $this->getMockForm('param1', $method);
56 $this->setRequestData($method, array(
60 $form->expects($this->once())
62 ->with('DATA', 'PATCH' !== $method);
64 $this->requestHandler
->handleRequest($form, $this->request
);
68 * @dataProvider methodProvider
70 public function testDoNotSubmitIfWrongRequestMethod($method)
72 $form = $this->getMockForm('param1', $method);
74 $otherMethod = 'POST' === $method ? 'PUT' : 'POST';
76 $this->setRequestData($otherMethod, array(
80 $form->expects($this->never())
83 $this->requestHandler
->handleRequest($form, $this->request
);
87 * @dataProvider methodExceptGetProvider
89 public function testSubmitSimpleFormWithNullIfNameNotInRequestAndNotGetRequest($method)
91 $form = $this->getMockForm('param1', $method, false);
93 $this->setRequestData($method, array(
97 $form->expects($this->once())
99 ->with($this->identicalTo(null), 'PATCH' !== $method);
101 $this->requestHandler
->handleRequest($form, $this->request
);
105 * @dataProvider methodExceptGetProvider
107 public function testSubmitCompoundFormWithArrayIfNameNotInRequestAndNotGetRequest($method)
109 $form = $this->getMockForm('param1', $method, true);
111 $this->setRequestData($method, array(
115 $form->expects($this->once())
117 ->with($this->identicalTo(array()), 'PATCH' !== $method);
119 $this->requestHandler
->handleRequest($form, $this->request
);
122 public function testDoNotSubmitIfNameNotInRequestAndGetRequest()
124 $form = $this->getMockForm('param1', 'GET');
126 $this->setRequestData('GET', array(
130 $form->expects($this->never())
133 $this->requestHandler
->handleRequest($form, $this->request
);
137 * @dataProvider methodProvider
139 public function testSubmitFormWithEmptyNameIfAtLeastOneFieldInRequest($method)
141 $form = $this->getMockForm('', $method);
142 $form->expects($this->any())
144 ->will($this->returnValue(array(
145 'param1' => $this->getMockForm('param1'),
146 'param2' => $this->getMockForm('param2'),
149 $this->setRequestData($method, $requestData = array(
150 'param1' => 'submitted value',
151 'paramx' => 'submitted value',
154 $form->expects($this->once())
156 ->with($requestData, 'PATCH' !== $method);
158 $this->requestHandler
->handleRequest($form, $this->request
);
162 * @dataProvider methodProvider
164 public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest($method)
166 $form = $this->getMockForm('', $method);
167 $form->expects($this->any())
169 ->will($this->returnValue(array(
170 'param1' => $this->getMockForm('param1'),
171 'param2' => $this->getMockForm('param2'),
174 $this->setRequestData($method, array(
175 'paramx' => 'submitted value',
178 $form->expects($this->never())
181 $this->requestHandler
->handleRequest($form, $this->request
);
185 * @dataProvider methodExceptGetProvider
187 public function testMergeParamsAndFiles($method)
189 $form = $this->getMockForm('param1', $method);
190 $file = $this->getMockFile();
192 $this->setRequestData($method, array(
202 $form->expects($this->once())
207 ), 'PATCH' !== $method);
209 $this->requestHandler
->handleRequest($form, $this->request
);
213 * @dataProvider methodExceptGetProvider
215 public function testParamTakesPrecedenceOverFile($method)
217 $form = $this->getMockForm('param1', $method);
218 $file = $this->getMockFile();
220 $this->setRequestData($method, array(
226 $form->expects($this->once())
228 ->with('DATA', 'PATCH' !== $method);
230 $this->requestHandler
->handleRequest($form, $this->request
);
234 * @dataProvider methodExceptGetProvider
236 public function testSubmitFileIfNoParam($method)
238 $form = $this->getMockForm('param1', $method);
239 $file = $this->getMockFile();
241 $this->setRequestData($method, array(
247 $form->expects($this->once())
249 ->with($file, 'PATCH' !== $method);
251 $this->requestHandler
->handleRequest($form, $this->request
);
254 abstract protected function setRequestData($method, $data, $files = array());
256 abstract protected function getRequestHandler();
258 abstract protected function getMockFile();
260 protected function getMockForm($name, $method = null, $compound = true)
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));
270 $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
271 $form->expects($this->any())
273 ->will($this->returnValue($name));
274 $form->expects($this->any())
275 ->method('getConfig')
276 ->will($this->returnValue($config));