ソースを参照

[DomCrawler] enabled removal of form fields to allow emulating javascript in a functional test

Kris Wallsmith 14 年 前
コミット
e596931dc8

+ 12 - 2
src/Symfony/Component/DomCrawler/Form.php

@@ -229,6 +229,16 @@ class Form implements \ArrayAccess
         return isset($this->fields[$name]);
     }
 
+    /**
+     * Removes a field from the form.
+     *
+     * @param string $name The field name
+     */
+    public function remove($name)
+    {
+        unset($this->fields[$name]);
+    }
+
     /**
      * Gets a named field.
      *
@@ -356,12 +366,12 @@ class Form implements \ArrayAccess
     }
 
     /**
-     * Unimplemented.
+     * Removes a field from the form.
      *
      * @param string $name The field name
      */
     public function offsetUnset($name)
     {
-        throw new \LogicException('The Form fields cannot be removed.');
+        $this->remove($name);
     }
 }

+ 25 - 20
tests/Symfony/Tests/Component/DomCrawler/FormTest.php

@@ -155,42 +155,40 @@ class FormTest extends \PHPUnit_Framework_TestCase
     {
         $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
 
-        $this->assertEquals('foo', $form['foo']->getValue(), '->__offsetGet() returns the value of a form field');
+        $this->assertEquals('foo', $form['foo']->getValue(), '->offsetGet() returns the value of a form field');
 
         $form['foo'] = 'bar';
 
-        $this->assertEquals('bar', $form['foo']->getValue(), '->__offsetSet() changes the value of a form field');
+        $this->assertEquals('bar', $form['foo']->getValue(), '->offsetSet() changes the value of a form field');
 
         try {
             $form['foobar'] = 'bar';
-            $this->pass('->__offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
+            $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
         } catch (\InvalidArgumentException $e) {
-            $this->assertTrue(true, '->__offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
+            $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
         }
 
         try {
             $form['foobar'];
-            $this->pass('->__offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
+            $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
         } catch (\InvalidArgumentException $e) {
-            $this->assertTrue(true, '->__offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
+            $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the field does not exist');
         }
     }
 
-    /**
-     * @expectedException LogicException
-     */
     public function testOffsetUnset()
     {
         $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
         unset($form['foo']);
+        $this->assertFalse(isset($form['foo']), '->offsetUnset() removes a field');
     }
 
-    public function testOffsetIsset()
+    public function testOffsetExists()
     {
         $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
 
-        $this->assertTrue(isset($form['foo']), '->offsetIsset() return true if the field exists');
-        $this->assertFalse(isset($form['bar']), '->offsetIsset() return false if the field does not exist');
+        $this->assertTrue(isset($form['foo']), '->offsetExists() return true if the field exists');
+        $this->assertFalse(isset($form['bar']), '->offsetExists() return false if the field does not exist');
     }
 
     public function testGetValues()
@@ -358,6 +356,13 @@ class FormTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($form->has('bar'), '->has() returns true if a field is in the form');
     }
 
+    public function testRemove()
+    {
+        $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
+        $form->remove('bar');
+        $this->assertFalse($form->has('bar'), '->remove() removes a field');
+    }
+
     public function testGet()
     {
         $form = $this->createForm('<form method="post"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
@@ -381,23 +386,23 @@ class FormTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('Symfony\\Component\\DomCrawler\\Field\\InputFormField', get_class($fields['bar']), '->all() return an array of form field objects');
     }
 
-    protected function createForm($form, $method = null, $host = null, $path = '/')
+    public function testBase()
     {
         $dom = new \DOMDocument();
-        $dom->loadHTML('<html>'.$form.'</html>');
+        $dom->loadHTML('<html><form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form></html>');
 
         $nodes = $dom->getElementsByTagName('input');
-
-        return new Form($nodes->item($nodes->length - 1), $method, $host, $path);
+        $form = new Form($nodes->item($nodes->length - 1), null, 'http://www.bar.com/foobar/', '/', 'http://www.foo.com/');
+        $this->assertEquals('http://www.foo.com/foo.php', $form->getUri());
     }
 
-    public function testBase()
+    protected function createForm($form, $method = null, $host = null, $path = '/')
     {
         $dom = new \DOMDocument();
-        $dom->loadHTML('<html><form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form></html>');
+        $dom->loadHTML('<html>'.$form.'</html>');
 
         $nodes = $dom->getElementsByTagName('input');
-        $form = new Form($nodes->item($nodes->length - 1), null, 'http://www.bar.com/foobar/', '/', 'http://www.foo.com/');
-        $this->assertEquals('http://www.foo.com/foo.php', $form->getUri());
+
+        return new Form($nodes->item($nodes->length - 1), $method, $host, $path);
     }
 }