Skip to content

Commit

Permalink
[DomCrawler] added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jun 27, 2010
1 parent 9895eaf commit e578dfd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/Symfony/Tests/Components/DomCrawler/CrawlerTest.php
Expand Up @@ -47,6 +47,10 @@ public function testAdd()
$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');
}

/**
Expand Down
Expand Up @@ -211,6 +211,57 @@ public function testCheckboxes()
}
}

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();
Expand Down
Expand Up @@ -53,6 +53,18 @@ public function testSetValue()
$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'));
Expand Down
17 changes: 17 additions & 0 deletions tests/Symfony/Tests/Components/DomCrawler/FormTest.php
Expand Up @@ -176,6 +176,23 @@ public function testGetSetValue()
}
}

/**
* @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>');
Expand Down

0 comments on commit e578dfd

Please sign in to comment.