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
;
14 use Symfony\Component\Form\NativeRequestHandler
;
17 * @author Bernhard Schussek <bschussek@gmail.com>
19 class NativeRequestHandlerTest
extends AbstractRequestHandlerTest
21 private static $serverBackup;
23 public static function setUpBeforeClass()
25 self
::$serverBackup = $_SERVER;
28 protected function setUp()
36 // PHPUnit needs this entry
37 'SCRIPT_NAME' => self
::$serverBackup['SCRIPT_NAME'],
41 protected function tearDown()
48 $_SERVER = self
::$serverBackup;
52 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
54 public function testRequestShouldBeNull()
56 $this->requestHandler
->handleRequest($this->getMockForm('name', 'GET'), 'request');
59 public function testMethodOverrideHeaderTakesPrecedenceIfPost()
61 $form = $this->getMockForm('param1', 'PUT');
63 $this->setRequestData('POST', array(
67 $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
69 $form->expects($this->once())
73 $this->requestHandler
->handleRequest($form, $this->request
);
76 public function testConvertEmptyUploadedFilesToNull()
78 $form = $this->getMockForm('param1', 'POST', false);
80 $this->setRequestData('POST', array(), array('param1' => array(
84 'error' => UPLOAD_ERR_NO_FILE
,
88 $form->expects($this->once())
90 ->with($this->identicalTo(null));
92 $this->requestHandler
->handleRequest($form, $this->request
);
95 public function testFixBuggyFilesArray()
97 $form = $this->getMockForm('param1', 'POST', false);
99 $this->setRequestData('POST', array(), array('param1' => array(
101 'field' => 'upload.txt',
104 'field' => 'text/plain',
107 'field' => 'owfdskjasdfsa',
110 'field' => UPLOAD_ERR_OK
,
117 $form->expects($this->once())
121 'name' => 'upload.txt',
122 'type' => 'text/plain',
123 'tmp_name' => 'owfdskjasdfsa',
124 'error' => UPLOAD_ERR_OK
,
129 $this->requestHandler
->handleRequest($form, $this->request
);
132 public function testFixBuggyNestedFilesArray()
134 $form = $this->getMockForm('param1', 'POST');
136 $this->setRequestData('POST', array(), array('param1' => array(
138 'field' => array('subfield' => 'upload.txt'),
141 'field' => array('subfield' => 'text/plain'),
144 'field' => array('subfield' => 'owfdskjasdfsa'),
147 'field' => array('subfield' => UPLOAD_ERR_OK
),
150 'field' => array('subfield' => 100),
154 $form->expects($this->once())
159 'name' => 'upload.txt',
160 'type' => 'text/plain',
161 'tmp_name' => 'owfdskjasdfsa',
162 'error' => UPLOAD_ERR_OK
,
168 $this->requestHandler
->handleRequest($form, $this->request
);
171 public function testMethodOverrideHeaderIgnoredIfNotPost()
173 $form = $this->getMockForm('param1', 'POST');
175 $this->setRequestData('GET', array(
179 $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
181 $form->expects($this->never())
184 $this->requestHandler
->handleRequest($form, $this->request
);
187 protected function setRequestData($method, $data, $files = array())
189 if ('GET' === $method) {
198 'REQUEST_METHOD' => $method,
199 // PHPUnit needs this entry
200 'SCRIPT_NAME' => self
::$serverBackup['SCRIPT_NAME'],
204 protected function getRequestHandler()
206 return new NativeRequestHandler();
209 protected function getMockFile()
212 'name' => 'upload.txt',
213 'type' => 'text/plain',
214 'tmp_name' => 'owfdskjasdfsa',
215 'error' => UPLOAD_ERR_OK
,