Skip to content

[Browserkit][Form][Tests] Add submitWithAdditionalValues() method in Symfony\Component\BrowserK… #18330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Symfony/Component/BrowserKit/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
46 changes: 46 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<html><form action="/foo"><input type="submit" /></form></html>'));
$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('<html><form action="/foo"><input type="submit" /></form></html>'));
$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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should you reverse the message meaning parameters should be added ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand your question. This text appears only if assertEquals() fail.


// Add an element to an existing collection.
$client->setNextResponse(new Response('<html><form action="/foo" method="post"><input type="text" name="foo[0][name]" value="foo" /><input type="text" name="foo[1][name]" value="bar" /><input type="submit" /></form></html>'));
$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'));
Expand Down
17 changes: 13 additions & 4 deletions src/Symfony/Component/DomCrawler/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
*
Expand Down