Skip to content

Commit c3f637b

Browse files
committed
PATCH support and tests for DELETE support
1 parent 2dd4bf1 commit c3f637b

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/Symfony/Component/DomCrawler/Form.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function getValues()
103103
*/
104104
public function getFiles()
105105
{
106-
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE'))) {
106+
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
107107
return array();
108108
}
109109

@@ -173,7 +173,7 @@ public function getUri()
173173
{
174174
$uri = parent::getUri();
175175

176-
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE')) && $queryString = http_build_query($this->getValues(), null, '&')) {
176+
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH')) && $queryString = http_build_query($this->getValues(), null, '&')) {
177177
$sep = false === strpos($uri, '?') ? '?' : '&';
178178
$uri .= $sep.$queryString;
179179
}
@@ -576,4 +576,4 @@ private function getSegments($name)
576576

577577
throw new \InvalidArgumentException(sprintf('Malformed field path "%s"', $name));
578578
}
579-
}
579+
}

tests/Symfony/Tests/Component/DomCrawler/FormTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ public function testGetMethod()
202202

203203
$form = $this->createForm('<form method="post"><input type="submit" /></form>', 'put');
204204
$this->assertEquals('PUT', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
205+
206+
$form = $this->createForm('<form method="post"><input type="submit" /></form>', 'delete');
207+
$this->assertEquals('DELETE', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
208+
209+
$form = $this->createForm('<form method="post"><input type="submit" /></form>', 'patch');
210+
$this->assertEquals('PATCH', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
205211
}
206212

207213
public function testGetSetValue()
@@ -278,7 +284,16 @@ public function testGetFiles()
278284
$this->assertEquals(array(), $form->getFiles(), '->getFiles() returns an empty array if method is get');
279285

280286
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
281-
$this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields');
287+
$this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for POST');
288+
289+
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'put');
290+
$this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PUT');
291+
292+
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'delete');
293+
$this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for DELETE');
294+
295+
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'patch');
296+
$this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PATCH');
282297

283298
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
284299
$this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
@@ -293,9 +308,9 @@ public function testGetPhpFiles()
293308
/**
294309
* @dataProvider provideGetUriValues
295310
*/
296-
public function testGetUri($message, $form, $values, $uri)
311+
public function testGetUri($message, $form, $values, $uri, $method = null)
297312
{
298-
$form = $this->createForm($form);
313+
$form = $this->createForm($form, $method);
299314
$form->setValues($values);
300315

301316
$this->assertEquals('http://example.com'.$uri, $form->getUri(), '->getUri() '.$message);
@@ -387,6 +402,27 @@ public function provideGetUriValues()
387402
array(),
388403
'/foo'
389404
),
405+
array(
406+
'does not append values if the method is patch',
407+
'<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
408+
array(),
409+
'/foo',
410+
'PUT'
411+
),
412+
array(
413+
'does not append values if the method is delete',
414+
'<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
415+
array(),
416+
'/foo',
417+
'DELETE'
418+
),
419+
array(
420+
'does not append values if the method is put',
421+
'<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
422+
array(),
423+
'/foo',
424+
'PATCH'
425+
),
390426
array(
391427
'appends the form values to an existing query string',
392428
'<form action="/foo?bar=bar"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',

0 commit comments

Comments
 (0)