From 3823aeb1b7a4dc2c8d57859051d551e945fda680 Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Wed, 9 Mar 2016 00:23:30 +0100 Subject: [PATCH] Add submitWithAdditionalValues() method in Symfony\Component\BrowserKit\Client Extract code that converts fields to arrays in a new method convertFieldsToArray() --- src/Symfony/Component/BrowserKit/Client.php | 25 ++++++++++ .../Component/BrowserKit/Tests/ClientTest.php | 46 +++++++++++++++++++ src/Symfony/Component/DomCrawler/Form.php | 17 +++++-- 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 858acba9570c6..d81efd65b168a 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -262,6 +262,31 @@ public function submit(Form $form, array $values = array()) return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles()); } + /** + * Submits a form with additional values. + * + * @param Form $form A Form instance + * @param array $values An array of form field values + * @param array $additionalValues An array of additional field values + * + * @return Crawler + */ + public function submitWithAdditionalValues(Form $form, array $values = array(), array $additionalValues = array()) + { + $form->setValues($values); + + $values = $form->getPhpValues(); + + if (!empty($additionalValues)) { + $values = array_merge( + $values, + $form->convertFieldsToArray($additionalValues) + ); + } + + return $this->request($form->getMethod(), $form->getUri(), $values, $form->getPhpFiles()); + } + /** * Calls a URI. * diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index 132d4bc51e43d..b57f4806e7f07 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -286,6 +286,52 @@ public function testSubmit() $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms'); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Unreachable field "foo" + */ + public function testSubmitMissingField() + { + $client = new TestClient(); + $client->setNextResponse(new Response('
')); + $crawler = $client->request('GET', 'http://www.example.com/foo/foobar'); + + $client->submit($crawler->filter('input')->form(), array('foo[0]' => 'bar')); + } + + /** + * Submit the same data that in testSubmitMissingField() but + * without triggering an Exception. + */ + public function testSubmitWithAdditionalValues() + { + $client = new TestClient(); + $client->setNextResponse(new Response('
')); + $crawler = $client->request('GET', 'http://www.example.com/foo/foobar'); + + $additionalValues = array('foo[0]' => 'bar'); + // The field "foo[0]" will be converted to an array "foo => 0". + $expectedParameters = array('foo' => array('0' => 'bar')); + + $client->submitWithAdditionalValues($crawler->filter('input')->form(), array(), $additionalValues); + + $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms'); + $this->assertEquals($expectedParameters, $client->getRequest()->getParameters(), 'parameters have not been added'); + + // Add an element to an existing collection. + $client->setNextResponse(new Response('
')); + $crawler = $client->request('GET', 'http://www.example.com/foo/foobar'); + + $additionalValues = array('foo[2][name]' => 'foobar'); + $expectedParameters = array('foo' => array('2' => array('name' => 'foobar'))); + + $form = $crawler->filter('input[type=submit]')->form(); + $client->submitWithAdditionalValues($form, $form->getPhpValues(), $additionalValues); + + $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms'); + $this->assertEquals($expectedParameters, $client->getRequest()->getParameters(), 'parameters have not been added'); + } + public function testSubmitPreserveAuth() { $client = new TestClient(array('PHP_AUTH_USER' => 'foo', 'PHP_AUTH_PW' => 'bar')); diff --git a/src/Symfony/Component/DomCrawler/Form.php b/src/Symfony/Component/DomCrawler/Form.php index c9c3c139b112b..ea0c8a4d6e57c 100644 --- a/src/Symfony/Component/DomCrawler/Form.php +++ b/src/Symfony/Component/DomCrawler/Form.php @@ -130,17 +130,16 @@ public function getFiles() } /** - * Gets the field values as PHP. - * * This method converts fields with the array notation * (like foo[bar] to arrays) like PHP does. * * @return array An array of field values. */ - public function getPhpValues() + public function convertFieldsToArray($fields) { $values = array(); - foreach ($this->getValues() as $name => $value) { + + foreach ($fields as $name => $value) { $qs = http_build_query(array($name => $value), '', '&'); if (!empty($qs)) { parse_str($qs, $expandedValue); @@ -152,6 +151,16 @@ public function getPhpValues() return $values; } + /** + * Gets the field values as PHP. + * + * @return array An array of field values. + */ + public function getPhpValues() + { + return $this->convertFieldsToArray($this->getValues()); + } + /** * Gets the file field values as PHP. *