Skip to content

Commit

Permalink
PATCH support and tests for DELETE support
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Jan 25, 2012
1 parent 2dd4bf1 commit c3f637b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Component/DomCrawler/Form.php
Expand Up @@ -103,7 +103,7 @@ public function getValues()
*/
public function getFiles()
{
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE'))) {
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
return array();
}

Expand Down Expand Up @@ -173,7 +173,7 @@ public function getUri()
{
$uri = parent::getUri();

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

throw new \InvalidArgumentException(sprintf('Malformed field path "%s"', $name));
}
}
}
42 changes: 39 additions & 3 deletions tests/Symfony/Tests/Component/DomCrawler/FormTest.php
Expand Up @@ -202,6 +202,12 @@ public function testGetMethod()

$form = $this->createForm('<form method="post"><input type="submit" /></form>', 'put');
$this->assertEquals('PUT', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');

$form = $this->createForm('<form method="post"><input type="submit" /></form>', 'delete');
$this->assertEquals('DELETE', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');

$form = $this->createForm('<form method="post"><input type="submit" /></form>', 'patch');
$this->assertEquals('PATCH', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided');
}

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

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
$this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields');
$this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for POST');

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'put');
$this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PUT');

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'delete');
$this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for DELETE');

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>', 'patch');
$this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields for PATCH');

$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
$this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
Expand All @@ -293,9 +308,9 @@ public function testGetPhpFiles()
/**
* @dataProvider provideGetUriValues
*/
public function testGetUri($message, $form, $values, $uri)
public function testGetUri($message, $form, $values, $uri, $method = null)
{
$form = $this->createForm($form);
$form = $this->createForm($form, $method);
$form->setValues($values);

$this->assertEquals('http://example.com'.$uri, $form->getUri(), '->getUri() '.$message);
Expand Down Expand Up @@ -387,6 +402,27 @@ public function provideGetUriValues()
array(),
'/foo'
),
array(
'does not append values if the method is patch',
'<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
array(),
'/foo',
'PUT'
),
array(
'does not append values if the method is delete',
'<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
array(),
'/foo',
'DELETE'
),
array(
'does not append values if the method is put',
'<form action="/foo" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
array(),
'/foo',
'PATCH'
),
array(
'appends the form values to an existing query string',
'<form action="/foo?bar=bar"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
Expand Down

0 comments on commit c3f637b

Please sign in to comment.