From 2dd4bf12835d286ee9b72edada87bbaf0677a157 Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Tue, 24 Jan 2012 19:46:37 +0100 Subject: [PATCH 1/3] Support for PATCH method in forms --- src/Symfony/Component/Form/Form.php | 1 + .../Symfony/Tests/Component/Form/FormTest.php | 1 + .../Component/HttpFoundation/RequestTest.php | 22 ++++++++++++++----- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index aa26db6bf6..f477211d67 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -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(), diff --git a/tests/Symfony/Tests/Component/Form/FormTest.php b/tests/Symfony/Tests/Component/Form/FormTest.php index e662848a4a..9975e0bed8 100644 --- a/tests/Symfony/Tests/Component/Form/FormTest.php +++ b/tests/Symfony/Tests/Component/Form/FormTest.php @@ -823,6 +823,7 @@ class FormTest extends \PHPUnit_Framework_TestCase array('POST'), array('PUT'), array('DELETE'), + array('PATCH'), ); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php index c165bfbba6..b44e46684b 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php @@ -585,7 +585,19 @@ class RequestTest extends \PHPUnit_Framework_TestCase ); } - public function testCreateFromGlobals() + public static function overloadedMethodProvider() + { + return array( + array('PUT'), + array('DELETE'), + array('PATCH'), + ); + } + + /** + * @dataProvider overloadedMethodProvider + */ + 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']); From c3f637b8343ce0cdde2edfb5dc01e623e01f559c Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Wed, 25 Jan 2012 14:54:48 +0100 Subject: [PATCH 2/3] PATCH support and tests for DELETE support --- src/Symfony/Component/DomCrawler/Form.php | 6 +-- .../Tests/Component/DomCrawler/FormTest.php | 42 +++++++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Form.php b/src/Symfony/Component/DomCrawler/Form.php index a235f953cc..b620d6290e 100644 --- a/src/Symfony/Component/DomCrawler/Form.php +++ b/src/Symfony/Component/DomCrawler/Form.php @@ -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)); } -} \ No newline at end of file +} diff --git a/tests/Symfony/Tests/Component/DomCrawler/FormTest.php b/tests/Symfony/Tests/Component/DomCrawler/FormTest.php index 01df4444b5..f36113eb7f 100644 --- a/tests/Symfony/Tests/Component/DomCrawler/FormTest.php +++ b/tests/Symfony/Tests/Component/DomCrawler/FormTest.php @@ -202,6 +202,12 @@ class FormTest extends \PHPUnit_Framework_TestCase $form = $this->createForm('
', 'put'); $this->assertEquals('PUT', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided'); + + $form = $this->createForm('
', 'delete'); + $this->assertEquals('DELETE', $form->getMethod(), '->getMethod() returns the method defined in the constructor if provided'); + + $form = $this->createForm('
', '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('
'); - $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('
', '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('
', '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('
', '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('
'); $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', + '
', + array(), + '/foo', + 'PUT' + ), + array( + 'does not append values if the method is delete', + '
', + array(), + '/foo', + 'DELETE' + ), + array( + 'does not append values if the method is put', + '
', + array(), + '/foo', + 'PATCH' + ), array( 'appends the form values to an existing query string', '
', From 746170bbe274e370b7fc4d3f59740c232b0f83bd Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Wed, 25 Jan 2012 14:56:27 +0100 Subject: [PATCH 3/3] Make method non static --- tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php index b44e46684b..4b208bb3ec 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php @@ -585,7 +585,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase ); } - public static function overloadedMethodProvider() + public function provideOverloadedMethods() { return array( array('PUT'), @@ -595,7 +595,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase } /** - * @dataProvider overloadedMethodProvider + * @dataProvider provideOverloadedMethods */ public function testCreateFromGlobals($method) {