]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
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; | |
13 | ||
14 | use Symfony\Component\Form\NativeRequestHandler; | |
15 | ||
16 | /** | |
17 | * @author Bernhard Schussek <bschussek@gmail.com> | |
18 | */ | |
19 | class NativeRequestHandlerTest extends AbstractRequestHandlerTest | |
20 | { | |
21 | private static $serverBackup; | |
22 | ||
23 | public static function setUpBeforeClass() | |
24 | { | |
25 | self::$serverBackup = $_SERVER; | |
26 | } | |
27 | ||
28 | protected function setUp() | |
29 | { | |
30 | parent::setUp(); | |
31 | ||
32 | $_GET = array(); | |
33 | $_POST = array(); | |
34 | $_FILES = array(); | |
35 | $_SERVER = array( | |
36 | // PHPUnit needs this entry | |
37 | 'SCRIPT_NAME' => self::$serverBackup['SCRIPT_NAME'], | |
38 | ); | |
39 | } | |
40 | ||
41 | protected function tearDown() | |
42 | { | |
43 | parent::tearDown(); | |
44 | ||
45 | $_GET = array(); | |
46 | $_POST = array(); | |
47 | $_FILES = array(); | |
48 | $_SERVER = self::$serverBackup; | |
49 | } | |
50 | ||
51 | /** | |
52 | * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException | |
53 | */ | |
54 | public function testRequestShouldBeNull() | |
55 | { | |
56 | $this->requestHandler->handleRequest($this->getMockForm('name', 'GET'), 'request'); | |
57 | } | |
58 | ||
59 | public function testMethodOverrideHeaderTakesPrecedenceIfPost() | |
60 | { | |
61 | $form = $this->getMockForm('param1', 'PUT'); | |
62 | ||
63 | $this->setRequestData('POST', array( | |
64 | 'param1' => 'DATA', | |
65 | )); | |
66 | ||
67 | $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT'; | |
68 | ||
69 | $form->expects($this->once()) | |
70 | ->method('submit') | |
71 | ->with('DATA'); | |
72 | ||
73 | $this->requestHandler->handleRequest($form, $this->request); | |
74 | } | |
75 | ||
76 | public function testConvertEmptyUploadedFilesToNull() | |
77 | { | |
78 | $form = $this->getMockForm('param1', 'POST', false); | |
79 | ||
80 | $this->setRequestData('POST', array(), array('param1' => array( | |
81 | 'name' => '', | |
82 | 'type' => '', | |
83 | 'tmp_name' => '', | |
84 | 'error' => UPLOAD_ERR_NO_FILE, | |
85 | 'size' => 0 | |
86 | ))); | |
87 | ||
88 | $form->expects($this->once()) | |
89 | ->method('submit') | |
90 | ->with($this->identicalTo(null)); | |
91 | ||
92 | $this->requestHandler->handleRequest($form, $this->request); | |
93 | } | |
94 | ||
95 | public function testFixBuggyFilesArray() | |
96 | { | |
97 | $form = $this->getMockForm('param1', 'POST', false); | |
98 | ||
99 | $this->setRequestData('POST', array(), array('param1' => array( | |
100 | 'name' => array( | |
101 | 'field' => 'upload.txt', | |
102 | ), | |
103 | 'type' => array( | |
104 | 'field' => 'text/plain', | |
105 | ), | |
106 | 'tmp_name' => array( | |
107 | 'field' => 'owfdskjasdfsa', | |
108 | ), | |
109 | 'error' => array( | |
110 | 'field' => UPLOAD_ERR_OK, | |
111 | ), | |
112 | 'size' => array( | |
113 | 'field' => 100, | |
114 | ), | |
115 | ))); | |
116 | ||
117 | $form->expects($this->once()) | |
118 | ->method('submit') | |
119 | ->with(array( | |
120 | 'field' => array( | |
121 | 'name' => 'upload.txt', | |
122 | 'type' => 'text/plain', | |
123 | 'tmp_name' => 'owfdskjasdfsa', | |
124 | 'error' => UPLOAD_ERR_OK, | |
125 | 'size' => 100, | |
126 | ), | |
127 | )); | |
128 | ||
129 | $this->requestHandler->handleRequest($form, $this->request); | |
130 | } | |
131 | ||
132 | public function testFixBuggyNestedFilesArray() | |
133 | { | |
134 | $form = $this->getMockForm('param1', 'POST'); | |
135 | ||
136 | $this->setRequestData('POST', array(), array('param1' => array( | |
137 | 'name' => array( | |
138 | 'field' => array('subfield' => 'upload.txt'), | |
139 | ), | |
140 | 'type' => array( | |
141 | 'field' => array('subfield' => 'text/plain'), | |
142 | ), | |
143 | 'tmp_name' => array( | |
144 | 'field' => array('subfield' => 'owfdskjasdfsa'), | |
145 | ), | |
146 | 'error' => array( | |
147 | 'field' => array('subfield' => UPLOAD_ERR_OK), | |
148 | ), | |
149 | 'size' => array( | |
150 | 'field' => array('subfield' => 100), | |
151 | ), | |
152 | ))); | |
153 | ||
154 | $form->expects($this->once()) | |
155 | ->method('submit') | |
156 | ->with(array( | |
157 | 'field' => array( | |
158 | 'subfield' => array( | |
159 | 'name' => 'upload.txt', | |
160 | 'type' => 'text/plain', | |
161 | 'tmp_name' => 'owfdskjasdfsa', | |
162 | 'error' => UPLOAD_ERR_OK, | |
163 | 'size' => 100, | |
164 | ), | |
165 | ), | |
166 | )); | |
167 | ||
168 | $this->requestHandler->handleRequest($form, $this->request); | |
169 | } | |
170 | ||
171 | public function testMethodOverrideHeaderIgnoredIfNotPost() | |
172 | { | |
173 | $form = $this->getMockForm('param1', 'POST'); | |
174 | ||
175 | $this->setRequestData('GET', array( | |
176 | 'param1' => 'DATA', | |
177 | )); | |
178 | ||
179 | $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT'; | |
180 | ||
181 | $form->expects($this->never()) | |
182 | ->method('submit'); | |
183 | ||
184 | $this->requestHandler->handleRequest($form, $this->request); | |
185 | } | |
186 | ||
187 | protected function setRequestData($method, $data, $files = array()) | |
188 | { | |
189 | if ('GET' === $method) { | |
190 | $_GET = $data; | |
191 | $_FILES = array(); | |
192 | } else { | |
193 | $_POST = $data; | |
194 | $_FILES = $files; | |
195 | } | |
196 | ||
197 | $_SERVER = array( | |
198 | 'REQUEST_METHOD' => $method, | |
199 | // PHPUnit needs this entry | |
200 | 'SCRIPT_NAME' => self::$serverBackup['SCRIPT_NAME'], | |
201 | ); | |
202 | } | |
203 | ||
204 | protected function getRequestHandler() | |
205 | { | |
206 | return new NativeRequestHandler(); | |
207 | } | |
208 | ||
209 | protected function getMockFile() | |
210 | { | |
211 | return array( | |
212 | 'name' => 'upload.txt', | |
213 | 'type' => 'text/plain', | |
214 | 'tmp_name' => 'owfdskjasdfsa', | |
215 | 'error' => UPLOAD_ERR_OK, | |
216 | 'size' => 100, | |
217 | ); | |
218 | } | |
219 | } |