Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitad02c38

Browse files
Add submitWithAdditionalValues() method in Symfony\Component\BrowserKit\Client
Extract code that converts fields to arrays in a new method convertFieldsToArray()
1 parentc5c63dc commitad02c38

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+
publicfunctionsubmitWithAdditionalValues(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+
publicfunctiontestSubmitMissingField()
294+
{
295+
$client =newTestClient();
296+
$client->setNextResponse(newResponse('<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+
publicfunctiontestSubmitWithAdditionalValues()
307+
{
308+
$client =newTestClient();
309+
$client->setNextResponse(newResponse('<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
publicfunctiontestSubmitPreserveAuth()
290327
{
291328
$client =newTestClient(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-
publicfunctiongetPhpValues()
138+
publicfunctionconvertFieldsToArray($fields)
141139
{
142140
$values =array();
143-
foreach ($this->getValues()as$name =>$value) {
141+
142+
foreach ($fieldsas$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+
publicfunctiongetPhpValues()
160+
{
161+
return$this->convertFieldsToArray($this->getValues());
162+
}
163+
155164
/**
156165
* Gets the file field values as PHP.
157166
*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp