ソースを参照

[DomCrawler] added some tests

Fabien Potencier 15 年 前
コミット
e578dfdbec

+ 4 - 0
tests/Symfony/Tests/Components/DomCrawler/CrawlerTest.php

@@ -47,6 +47,10 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
         $crawler = new Crawler();
         $crawler->add($this->createNodeList()->item(0));
         $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from an \DOMNode');
+
+        $crawler = new Crawler();
+        $crawler->add('<html><body>Foo</body></html>');
+        $this->assertEquals('Foo', $crawler->filter('body')->text(), '->add() adds nodes from a string');
     }
 
     /**

+ 51 - 0
tests/Symfony/Tests/Components/DomCrawler/Field/ChoiceFormFieldTest.php

@@ -211,6 +211,57 @@ class ChoiceFormFieldTest extends FormFieldTestCase
         }
     }
 
+    public function testTick()
+    {
+        $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
+        $field = new ChoiceFormField($node);
+
+        try {
+            $field->tick();
+            $this->fail('->tick() throws a \LogicException for select boxes');
+        } catch (\LogicException $e) {
+            $this->assertTrue(true, '->tick() throws a \LogicException for select boxes');
+        }
+
+        $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
+        $field = new ChoiceFormField($node);
+        $field->tick();
+        $this->assertEquals(1, $field->getValue(), '->tick() ticks checkoxes');
+    }
+
+    public function testUntick()
+    {
+        $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
+        $field = new ChoiceFormField($node);
+
+        try {
+            $field->untick();
+            $this->fail('->untick() throws a \LogicException for select boxes');
+        } catch (\LogicException $e) {
+            $this->assertTrue(true, '->untick() throws a \LogicException for select boxes');
+        }
+
+        $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
+        $field = new ChoiceFormField($node);
+        $field->untick();
+        $this->assertEquals(null, $field->getValue(), '->untick() unticks checkoxes');
+    }
+
+    public function testSelect()
+    {
+        $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
+        $field = new ChoiceFormField($node);
+        $field->select(true);
+        $this->assertEquals(1, $field->getValue(), '->select() changes the value of the field');
+        $field->select(false);
+        $this->assertEquals(null, $field->getValue(), '->select() changes the value of the field');
+
+        $node = $this->createSelectNode(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();

+ 12 - 0
tests/Symfony/Tests/Components/DomCrawler/Field/FileFormFieldTest.php

@@ -53,6 +53,18 @@ class FileFormFieldTest extends FormFieldTestCase
         $this->assertEquals(array('name' => 'FileFormFieldTest.php', 'type' => '', 'tmp_name' => __FILE__, 'error' => 0, 'size' => filesize(__FILE__)), $field->getValue(), '->setValue() sets the value to the given file');
     }
 
+    public function testUpload()
+    {
+        $node = $this->createNode('input', '', array('type' => 'file'));
+        $field = new FileFormField($node);
+
+        $field->upload(null);
+        $this->assertEquals(array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0), $field->getValue(), '->upload() clears the uploaded file if the value is null');
+
+        $field->upload(__FILE__);
+        $this->assertEquals(array('name' => 'FileFormFieldTest.php', 'type' => '', 'tmp_name' => __FILE__, 'error' => 0, 'size' => filesize(__FILE__)), $field->getValue(), '->upload() sets the value to the given file');
+    }
+
     public function testSetErrorCode()
     {
         $node = $this->createNode('input', '', array('type' => 'file'));

+ 17 - 0
tests/Symfony/Tests/Components/DomCrawler/FormTest.php

@@ -176,6 +176,23 @@ class FormTest extends \PHPUnit_Framework_TestCase
         }
     }
 
+    /**
+     * @expectedException LogicException
+     */
+    public function testOffsetUnset()
+    {
+        $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
+        unset($form['foo']);
+    }
+
+    public function testOffsetIsset()
+    {
+        $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');
+    }
+
     public function testGetValues()
     {
         $form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');