Sfoglia il codice sorgente

merged branch stealth35/patch-11 (PR #2224)

Commits
-------

cf736cc [DomCrawler] Add test for ChoiceFormField without value
bca551e [DomCrawler] ChoiceFormField should take the content when value is unavailable

Discussion
----------

[DomCrawler] ChoiceFormField should take the content when value is unavai

[DomCrawler] ChoiceFormField should take the content when value is unavailable

---------------------------------------------------------------------------

by fabpot at 2011/09/20 09:15:21 -0700

Can you some unit tests?
Fabien Potencier 13 anni fa
parent
commit
4ec8798ab2

+ 1 - 1
src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php

@@ -207,7 +207,7 @@ class ChoiceFormField extends FormField
 
             $found = false;
             foreach ($this->xpath->query('descendant::option', $this->node) as $option) {
-                $this->options[] = $option->getAttribute('value');
+                $this->options[] = $option->hasAttribute('value') ? $option->getAttribute('value') : $option->nodeValue;
 
                 if ($option->getAttribute('selected')) {
                     $found = true;

+ 29 - 0
tests/Symfony/Tests/Component/DomCrawler/Field/ChoiceFormFieldTest.php

@@ -262,6 +262,14 @@ class ChoiceFormFieldTest extends FormFieldTestCase
         $this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
     }
 
+    public function testOptionWithNoValue()
+    {
+        $node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
+        $field = new ChoiceFormField($node);
+        $field->select('foo');
+        $this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
+    }
+
     protected function createSelectNode($options, $attributes = array())
     {
         $document = new \DOMDocument();
@@ -283,4 +291,25 @@ class ChoiceFormFieldTest extends FormFieldTestCase
 
         return $node;
     }
+
+    protected function createSelectNodeWithEmptyOption($options, $attributes = array())
+    {
+        $document = new \DOMDocument();
+        $node = $document->createElement('select');
+
+        foreach ($attributes as $name => $value) {
+            $node->setAttribute($name, $value);
+        }
+        $node->setAttribute('name', 'name');
+
+        foreach ($options as $value => $selected) {
+            $option = $document->createElement('option', $value);
+            if ($selected) {
+                $option->setAttribute('selected', 'selected');
+            }
+            $node->appendChild($option);
+        }
+
+        return $node;
+    }
 }