コード例 #1
0
 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');
 }
コード例 #2
0
 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->assertEquals(null, $field->getValue(), '->getValue() returns null if the checkbox is not checked');
     $this->assertFalse($field->isMultiple(), '->hasValue() returns false for checkboxes');
     try {
         $field->addChoice(new \DOMNode());
         $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('1', $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->assertEquals(null, $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');
     }
 }