Skip to content

Commit

Permalink
[DomCrawler] Fixed incorrect value name conversion in getPhpValues() …
Browse files Browse the repository at this point in the history
…and getPhpFiles()
  • Loading branch information
robbertkl authored and romainneutron committed Mar 13, 2014
1 parent 2c55a2d commit e961f57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Symfony/Component/DomCrawler/Form.php
Expand Up @@ -143,8 +143,13 @@ public function getFiles()
*/
public function getPhpValues()
{
$qs = http_build_query($this->getValues(), '', '&');
parse_str($qs, $values);
$values = array();
foreach ($this->getValues() as $name => $value) {
$qs = http_build_query(array($name => $value), '', '&');
parse_str($qs, $expandedValue);
$varName = substr($name, 0, strlen(key($expandedValue)));
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
}

return $values;
}
Expand All @@ -161,8 +166,13 @@ public function getPhpValues()
*/
public function getPhpFiles()
{
$qs = http_build_query($this->getFiles(), '', '&');
parse_str($qs, $values);
$values = array();
foreach ($this->getFiles() as $name => $value) {
$qs = http_build_query(array($name => $value), '', '&');
parse_str($qs, $expandedValue);
$varName = substr($name, 0, strlen(key($expandedValue)));
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
}

return $values;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/FormTest.php
Expand Up @@ -391,6 +391,9 @@ public function testGetPhpValues()
{
$form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
$this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), '->getPhpValues() converts keys with [] to arrays');

$form = $this->createForm('<form><input type="text" name="fo.o[ba.r]" value="foo" /><input type="text" name="ba r" value="bar" /><input type="submit" /></form>');
$this->assertEquals(array('fo.o' => array('ba.r' => 'foo'), 'ba r' => 'bar'), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names');
}

public function testGetFiles()
Expand Down Expand Up @@ -418,6 +421,9 @@ public function testGetPhpFiles()
{
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
$this->assertEquals(array('foo' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() converts keys with [] to arrays');

$form = $this->createForm('<form method="post"><input type="file" name="f.o o[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
$this->assertEquals(array('f.o o' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() preserves periods and spaces in names');
}

/**
Expand Down

0 comments on commit e961f57

Please sign in to comment.