From 991e65c96f82074c0178c1957025ecfcfa6801cb Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Tue, 12 May 2015 18:59:01 +0100 Subject: [PATCH] [DomCrawler] Throw an exception if a form field path is incomplete. --- .../DomCrawler/FormFieldRegistry.php | 6 +++-- .../Component/DomCrawler/Tests/FormTest.php | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/FormFieldRegistry.php b/src/Symfony/Component/DomCrawler/FormFieldRegistry.php index 6a38e86691..edb2788910 100644 --- a/src/Symfony/Component/DomCrawler/FormFieldRegistry.php +++ b/src/Symfony/Component/DomCrawler/FormFieldRegistry.php @@ -124,13 +124,15 @@ class FormFieldRegistry public function set($name, $value) { $target = &$this->get($name); - if (!is_array($value) || $target instanceof Field\ChoiceFormField) { + if ((!is_array($value) && $target instanceof Field\FormField) || $target instanceof Field\ChoiceFormField) { $target->setValue($value); - } else { + } elseif (is_array($value)) { $fields = self::create($name, $value); foreach ($fields->all() as $k => $v) { $this->set($k, $v); } + } else { + throw new \InvalidArgumentException(sprintf('Cannot set value on a compound field "%s".', $name)); } } diff --git a/src/Symfony/Component/DomCrawler/Tests/FormTest.php b/src/Symfony/Component/DomCrawler/Tests/FormTest.php index a85a846ba2..c657e3639e 100644 --- a/src/Symfony/Component/DomCrawler/Tests/FormTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/FormTest.php @@ -787,6 +787,31 @@ class FormTest extends \PHPUnit_Framework_TestCase )); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Cannot set value on a compound field "foo[bar]". + */ + public function testFormRegistrySetValueOnCompoundField() + { + $registry = new FormFieldRegistry(); + $registry->add($this->getFormFieldMock('foo[bar][baz]')); + + $registry->set('foo[bar]', 'fbb'); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Unreachable field "0" + */ + public function testFormRegistrySetArrayOnNotCompoundField() + { + + $registry = new FormFieldRegistry(); + $registry->add($this->getFormFieldMock('bar')); + + $registry->set('bar', array('baz')); + } + public function testDifferentFieldTypesWithSameName() { $dom = new \DOMDocument();