/** * Adds an option to the select field. * * @param string $displayName The display text of the option. * @param string $value The option's value. * @param boolean $preSelected True in case, the option should be selected, false otherwise. * * @author Christian Achatz * @version * Version 0.1, 15.02.2010<br /> */ public function addOption($displayName, $value, $preSelected = false) { $option = new SelectBoxOptionTag(); $option->setContent($displayName); $option->setAttribute('value', $value); if ($preSelected == true) { $option->setAttribute('selected', 'selected'); } $this->addOptionTag($option); }
/** * Test whether other options are unselected during selection of one * specific option in simple select boxes. */ public function testRemoveSelectedOptions() { $tag = new SelectBoxTag(); $selectedOption = new SelectBoxOptionTag(); $selectedOption->setAttribute('selected', 'selected'); $unselectedOption = new SelectBoxOptionTag(); $firstId = XmlParser::generateUniqID(); $secondId = XmlParser::generateUniqID(); $thirdId = XmlParser::generateUniqID(); // inject child structure as basis for un-selection $property = new ReflectionProperty(SelectBoxTag::class, 'children'); $property->setAccessible(true); $property->setValue($tag, [$firstId => clone $selectedOption->setObjectId($firstId), $secondId => clone $unselectedOption->setObjectId($secondId), $thirdId => clone $selectedOption->setObjectId($thirdId)]); $method = new ReflectionMethod(SelectBoxTag::class, 'removeSelectedOptions'); $method->setAccessible(true); $method->invokeArgs($tag, [$firstId]); /* @var $children DomNode[] */ $children = $property->getValue($tag); $this->assertEquals('selected', $children[$firstId]->getAttribute('selected')); $this->assertNull($children[$secondId]->getAttribute('selected')); $this->assertNull($children[$thirdId]->getAttribute('selected')); }
/** * Tests transformation of a complex multi-select box. */ public function testTransformation() { $select = $this->getSelectBox(); $group = new SelectBoxGroupTag(); $group->setAttribute('label', 'foo'); $option1 = new SelectBoxOptionTag(); $option1->setAttribute('value', '1'); $option1->setContent('1'); $option1->setAttribute('selected', 'selected'); $option2 = new SelectBoxOptionTag(); $option2->setAttribute('value', '2'); $option2->setContent('2'); $option3 = new SelectBoxOptionTag(); $option3->setAttribute('value', '3'); $option3->setContent('3'); $option3->setAttribute('selected', 'selected'); $option4 = new SelectBoxOptionTag(); $option4->setAttribute('value', '4'); $option4->setContent('4'); $option4->setAttribute('selected', 'selected'); $group->addOptionTag($option1); $group->addOptionTag($option2); $group->addOptionTag($option3); $select->addGroupTag($group); $select->addOptionTag($option4); $actual = $select->transform(); // general structure $this->assertContains('<select multiple="multiple" name="' . self::SELECT_BOX_NAME . '[]"', $actual); $this->assertContains('</select>', $actual); $this->assertContains('<optgroup label="foo">', $actual); $this->assertContains('</optgroup>', $actual); // selected items $this->assertContains('<option value="1" selected="selected">1</option>', $actual); $this->assertContains('<option value="2"', $actual); $this->assertContains('<option value="3" selected="selected">3</option>', $actual); $this->assertContains('<option value="4" selected="selected">4</option>', $actual); }