merged branch lstrojny/form/patch-method-support (PR #3186)

Commits
-------

746170b Make method non static
c3f637b PATCH support and tests for DELETE support
2dd4bf1 Support for PATCH method in forms

Discussion
----------

[form] Support for PATCH method in forms

Bug fix: no
Feature addition: yes
Backwards compatibility break: yes
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -

Adds support for PATCH method. Accidentally I refactored the test RequestTest a bit to verify PUT, DELETE and PATCH and not only PUT.

---------------------------------------------------------------------------

by lanthaler at 2012-01-25T13:26:26Z

Why do you classify this as something that breaks backwards compatibility? I don't think it breaks anything.

---------------------------------------------------------------------------

by lstrojny at 2012-01-25T13:28:28Z

It’s not that relevant here, but before using patch throws an exception which it no longer does.

---------------------------------------------------------------------------

by lanthaler at 2012-01-25T13:31:35Z

Oh OK..

---------------------------------------------------------------------------

by vicb at 2012-01-25T13:36:39Z

Maybe you can update the [DomCrawler](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DomCrawler/Form.php) at the same time ?

---------------------------------------------------------------------------

by lstrojny at 2012-01-25T13:55:21Z

Done
This commit is contained in:
Fabien Potencier 2012-01-25 16:06:27 +01:00
commit 9dcdcfa037
5 changed files with 61 additions and 11 deletions

View File

@ -103,7 +103,7 @@ class Form extends Link implements \ArrayAccess
*/
public function getFiles()
{
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE'))) {
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
return array();
}
@ -173,7 +173,7 @@ class Form extends Link implements \ArrayAccess
{
$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;
}
@ -576,4 +576,4 @@ class FormFieldRegistry
throw new \InvalidArgumentException(sprintf('Malformed field path "%s"', $name));
}
}
}

View File

@ -582,6 +582,7 @@ class Form implements \IteratorAggregate, FormInterface
case 'POST':
case 'PUT':
case 'DELETE':
case 'PATCH':
if ('' === $this->getName()) {
$data = array_replace_recursive(
$request->request->all(),

View File

@ -202,6 +202,12 @@ class FormTest extends \PHPUnit_Framework_TestCase
$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()
@ -278,7 +284,16 @@ class FormTest extends \PHPUnit_Framework_TestCase
$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');
@ -293,9 +308,9 @@ class FormTest extends \PHPUnit_Framework_TestCase
/**
* @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);
@ -387,6 +402,27 @@ class FormTest extends \PHPUnit_Framework_TestCase
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>',

View File

@ -823,6 +823,7 @@ class FormTest extends \PHPUnit_Framework_TestCase
array('POST'),
array('PUT'),
array('DELETE'),
array('PATCH'),
);
}

View File

@ -585,7 +585,19 @@ class RequestTest extends \PHPUnit_Framework_TestCase
);
}
public function testCreateFromGlobals()
public function provideOverloadedMethods()
{
return array(
array('PUT'),
array('DELETE'),
array('PATCH'),
);
}
/**
* @dataProvider provideOverloadedMethods
*/
public function testCreateFromGlobals($method)
{
$_GET['foo1'] = 'bar1';
$_POST['foo2'] = 'bar2';
@ -602,19 +614,19 @@ class RequestTest extends \PHPUnit_Framework_TestCase
unset($_GET['foo1'], $_POST['foo2'], $_COOKIE['foo3'], $_FILES['foo4'], $_SERVER['foo5']);
$_SERVER['REQUEST_METHOD'] = 'PUT';
$_SERVER['REQUEST_METHOD'] = $method;
$_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
$request = RequestContentProxy::createFromGlobals();
$this->assertEquals('PUT', $request->getMethod());
$this->assertEquals($method, $request->getMethod());
$this->assertEquals('mycontent', $request->request->get('content'));
unset($_SERVER['REQUEST_METHOD'], $_SERVER['CONTENT_TYPE']);
$_POST['_method'] = 'PUT';
$_POST['_method'] = $method;
$_POST['foo6'] = 'bar6';
$_SERVER['REQUEST_METHOD'] = 'POST';
$request = Request::createFromGlobals();
$this->assertEquals('PUT', $request->getMethod());
$this->assertEquals($method, $request->getMethod());
$this->assertEquals('bar6', $request->request->get('foo6'));
unset($_POST['_method'], $_POST['foo6'], $_SERVER['REQUEST_METHOD']);