You are here

public function ChoiceFormFieldTest::testCheckboxes in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/dom-crawler/Tests/Field/ChoiceFormFieldTest.php \Symfony\Component\DomCrawler\Tests\Field\ChoiceFormFieldTest::testCheckboxes()

File

vendor/symfony/dom-crawler/Tests/Field/ChoiceFormFieldTest.php, line 215

Class

ChoiceFormFieldTest

Namespace

Symfony\Component\DomCrawler\Tests\Field

Code

public function testCheckboxes() {
  $node = $this
    ->createNode('input', '', array(
    'type' => 'checkbox',
    'name' => 'name',
  ));
  $field = new ChoiceFormField($node);
  $this
    ->assertFalse($field
    ->hasValue(), '->hasValue() returns false when the checkbox is not checked');
  $this
    ->assertNull($field
    ->getValue(), '->getValue() returns null if the checkbox is not checked');
  $this
    ->assertFalse($field
    ->isMultiple(), '->hasValue() returns false for checkboxes');
  try {
    $field
      ->addChoice(new \DOMElement('input'));
    $this
      ->fail('->addChoice() throws a \\LogicException for checkboxes');
  } catch (\LogicException $e) {
    $this
      ->assertTrue(true, '->initialize() throws a \\LogicException for checkboxes');
  }
  $node = $this
    ->createNode('input', '', array(
    'type' => 'checkbox',
    'name' => 'name',
    'checked' => 'checked',
  ));
  $field = new ChoiceFormField($node);
  $this
    ->assertTrue($field
    ->hasValue(), '->hasValue() returns true when the checkbox is checked');
  $this
    ->assertEquals('on', $field
    ->getValue(), '->getValue() returns 1 if the checkbox is checked and has no value attribute');
  $node = $this
    ->createNode('input', '', array(
    'type' => 'checkbox',
    'name' => 'name',
    'checked' => 'checked',
    'value' => 'foo',
  ));
  $field = new ChoiceFormField($node);
  $this
    ->assertEquals('foo', $field
    ->getValue(), '->getValue() returns the value attribute if the checkbox is checked');
  $node = $this
    ->createNode('input', '', array(
    'type' => 'checkbox',
    'name' => 'name',
    'checked' => 'checked',
    'value' => 'foo',
  ));
  $field = new ChoiceFormField($node);
  $field
    ->setValue(false);
  $this
    ->assertNull($field
    ->getValue(), '->setValue() unchecks the checkbox is value is false');
  $field
    ->setValue(true);
  $this
    ->assertEquals('foo', $field
    ->getValue(), '->setValue() checks the checkbox is value is true');
  try {
    $field
      ->setValue('bar');
    $this
      ->fail('->setValue() throws an \\InvalidArgumentException if the value is not one from the value attribute');
  } catch (\InvalidArgumentException $e) {
    $this
      ->assertTrue(true, '->setValue() throws an \\InvalidArgumentException if the value is not one from the value attribute');
  }
}