Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 582a494

Browse files
Add submitWithAdditionalValues() method in Symfony\Component\BrowserKit\Client
Extract code that converts fields to arrays in a new method convertFieldsToArray()
1 parent b0fffac commit 582a494

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,31 @@ public function submit(Form $form, array $values = array())
262262
return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles());
263263
}
264264

265+
/**
266+
* Submits a form.
267+
*
268+
* @param Form $form A Form instance
269+
* @param array $values An array of form field values
270+
* @param array $additionalValues An array of additional field values
271+
*
272+
* @return Crawler
273+
*/
274+
public function submitWithAdditionalValues(Form $form, array $values = array(), $additionalValues = array())
275+
{
276+
$form->setValues($values);
277+
278+
$values = $form->getPhpValues();
279+
280+
if (!empty($additionalValues)) {
281+
$values = array_merge_recursive(
282+
$values,
283+
$form->convertFieldsToArray($additionalValues)
284+
);
285+
}
286+
287+
return $this->request($form->getMethod(), $form->getUri(), $values, $form->getPhpFiles());
288+
}
289+
265290
/**
266291
* Calls a URI.
267292
*

src/Symfony/Component/BrowserKit/Tests/ClientTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,43 @@ public function testSubmit()
286286
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
287287
}
288288

289+
/**
290+
* @expectedException \InvalidArgumentException
291+
* @expectedExceptionMessage Unreachable field "foo"
292+
*/
293+
public function testSubmitMissingField()
294+
{
295+
$client = new TestClient();
296+
$client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
297+
$crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
298+
299+
$client->submit($crawler->filter('input')->form(), array('foo[0]' => 'bar'));
300+
}
301+
302+
/**
303+
* Submit the same data that in testSubmitMissingField() but
304+
* without triggering an Exception.
305+
*/
306+
public function testSubmitWithAdditionalValues()
307+
{
308+
$client = new TestClient();
309+
$client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
310+
$crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
311+
312+
$client->submitWithAdditionalValues($crawler->filter('input')->form(), array(), array('foo[0]' => 'bar'));
313+
314+
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
315+
316+
// The field "foo[0]" have been converted to an array "foo => 0".
317+
$this->assertEquals(
318+
array('foo' => array(
319+
'0' => 'bar',
320+
)),
321+
$client->getRequest()->getParameters(),
322+
'parameters have not been added'
323+
);
324+
}
325+
289326
public function testSubmitPreserveAuth()
290327
{
291328
$client = new TestClient(array('PHP_AUTH_USER' => 'foo', 'PHP_AUTH_PW' => 'bar'));

src/Symfony/Component/DomCrawler/Form.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,16 @@ public function getFiles()
130130
}
131131

132132
/**
133-
* Gets the field values as PHP.
134-
*
135133
* This method converts fields with the array notation
136134
* (like foo[bar] to arrays) like PHP does.
137135
*
138136
* @return array An array of field values.
139137
*/
140-
public function getPhpValues()
138+
public function convertFieldsToArray($fields)
141139
{
142140
$values = array();
143-
foreach ($this->getValues() as $name => $value) {
141+
142+
foreach ($fields as $name => $value) {
144143
$qs = http_build_query(array($name => $value), '', '&');
145144
if (!empty($qs)) {
146145
parse_str($qs, $expandedValue);
@@ -152,6 +151,16 @@ public function getPhpValues()
152151
return $values;
153152
}
154153

154+
/**
155+
* Gets the field values as PHP.
156+
*
157+
* @return array An array of field values.
158+
*/
159+
public function getPhpValues()
160+
{
161+
return $this->convertFieldsToArray($this->getValues());
162+
}
163+
155164
/**
156165
* Gets the file field values as PHP.
157166
*

0 commit comments

Comments
 (0)