Permalink
Browse files

ID#339: fixed form control finder issue with empty form groups.

  • Loading branch information...
Christian Achatz
Christian Achatz committed Oct 8, 2018
1 parent ec0da56 commit e8fa2eb79f3a8e263ce4f611cbaca38941304821
@@ -463,11 +463,10 @@ public function testGetLabel3() {
* Tests API complies for non existing tags.
*/
public function testGetFormElementsByTagName1() {
$this->expectException(FormException::class);
$form = new HtmlFormTag();
$doc = new Document();
$form->setParent($doc);
$form->getFormElementsByTagName('html:placeholder');
$this->assertEmpty($form->getFormElementsByTagName('html:placeholder'));
}
/**
@@ -542,6 +541,31 @@ public function testGetFormElementsByTagName3() {
}
/**
* ID#339: test handling or empty form groups.
*/
public function testGetFormElementsByTagName4() {
$form = new HtmlFormTag();
$doc = new Document();
$form->setParent($doc);
$form->setContent('<form:group name="empty"></form:group>
<form:button name="submit" value="submit" />');
$form->onParseTime();
$form->onAfterAppend();
$this->assertCount(2, $form->getChildren());
$actual = $form->getFormElementsByTagName('form:button');
$this->assertCount(1, $actual);
$this->assertInstanceOf(ButtonTag::class, $actual[0]);
$this->assertEquals('submit', $actual[0]->getAttribute('name'));
}
/**
* Tests API complies for no elements found.
*/
@@ -661,11 +685,38 @@ public function testGetFormElementsByName3() {
);
}
/**
* ID#339: test handling or empty form groups.
*/
public function testGetFormElementsByName4() {
$form = new HtmlFormTag();
$doc = new Document();
$form->setParent($doc);
$form->setContent('<form:group name="empty"></form:group>
<form:radio id="radio-1" name="bar" value="1" />
<form:radio id="radio-2" name="bar" value="1" />
<form:button name="submit" value="submit" />');
$form->onParseTime();
$form->onAfterAppend();
$this->assertCount(4, $form->getChildren());
$actual = $form->getFormElementsByName('bar');
$this->assertCount(2, $actual);
foreach ($actual as $field) {
$this->assertInstanceOf(RadioButtonTag::class, $field);
$this->assertEquals('bar', $field->getAttribute('name'));
}
}
/**
* Tests API complies for no elements found.
*/
public function testGetFormElementsByType1() {
$this->expectException(FormException::class);
$form = new HtmlFormTag();
$doc = new Document();
$form->setParent($doc);
@@ -720,6 +771,31 @@ public function testGetFormElementsByType2() {
}
/**
* ID#339: test handling or empty form groups.
*/
public function testGetFormElementsByType3() {
$form = new HtmlFormTag();
$doc = new Document();
$form->setParent($doc);
$form->setContent('<form:group name="empty"></form:group>
<form:button name="submit" value="submit" />');
$form->onParseTime();
$form->onAfterAppend();
$this->assertCount(2, $form->getChildren());
$actual = $form->getFormElementsByType(ButtonTag::class);
$this->assertCount(1, $actual);
$this->assertInstanceOf(ButtonTag::class, $actual[0]);
$this->assertEquals('submit', $actual[0]->getAttribute('name'));
}
/**
* Tests getMarker() delegates to getFormElementByName().
*/
@@ -47,44 +47,47 @@
public function getFormElementByID(string $id);
/**
* Returns a list of form controls with the given name.
* Returns a list of form controls with the given name. In case no tag is found, this
* method will return an empty list.
*
* @param string $name The name of the form elements to collect (e.g. for radio buttons).
*
* @return FormControl[] The list of form controls with the given name.
* @return FormControl[] The list of form controls with the given name or an empty list.
*
* @author Christian Achatz
* @version
* Version 0.1, 16.08.2010<br />
*/
public function getFormElementsByName(string $name);
public function getFormElementsByName(string $name): array;
/**
* Returns a list of form elements addressed by their tag name.
* Returns a list of form elements addressed by their tag name. In case no tag is found, this
* method will return an empty list.
*
* @param string $tagName The tag name of the desired form element (e.g. "form:text").
*
* @return FormControl[] A list of references on the form elements.
* @throws FormException In case the form element cannot be found or desired tag is not registered.
* @return FormControl[] A list of references on the form elements or an empty list.
*
* @author Christian Achatz
* @version
* Version 0.1, 14.06.2008 (API change: do use this function instead of getFormElementsByType()!)<br />
* Version 0.2, 12.12.2012 (Refactoring due to tag renaming)<br />
* Version 0.3, 08.10.2016 (ID#339: changes method signature to not throw exceptions to avoid issues finding nested elements)<br />
*/
public function getFormElementsByTagName(string $tagName);
public function getFormElementsByTagName(string $tagName): array;
/**
* Returns a list of form elements addressed by their implementation class name.
* Returns a list of form elements addressed by their implementation class name. In case no tag is
* found, this method will return an empty list.
*
* @param string $class Name of the implementation class of the form elements to return.
*
* @return FormControl[] A list of references on the form elements.
* @throws FormException In case the form element cannot be found or desired tag is not registered.
* @return FormControl[] A list of references on the form elements or an empty list.
*
* @author Christian Achatz
* @version
* Version 0.1, 03.03.2018<br />
* Version 0.2, 08.10.2016 (ID#339: changes method signature to not throw exceptions to avoid issues finding nested elements)<br />
*/
public function getFormElementsByType(string $class): array;
@@ -181,11 +181,14 @@ public function getLabel(string $name) {
/**
* @param string $name The name of the form elements to collect (e.g. for radio buttons).
*
* @return FormControl[] The list of form controls with the given name.
* @return FormControl[] The list of form controls with the given name or an empty list.
*/
public function getFormElementsByName(string $name) {
public function getFormElementsByName(string $name): array {
$elements = [];
if (count($this->children) > 0) {
foreach ($this->children as &$child) {
// when we directly find something - get it!
@@ -198,6 +201,7 @@ public function getFormElementsByName(string $name) {
$elements = array_merge($elements, $child->getFormElementsByName($name));
}
}
}
return $elements;
@@ -206,10 +210,9 @@ public function getFormElementsByName(string $name) {
/**
* @param string $tagName The tag name of the desired form element (e.g. "form:text").
*
* @return FormControl[] A list of references on the form elements.
* @throws FormException In case the form element cannot be found or desired tag is not registered.
* @return FormControl[] A list of references on the form elements or an empty list.
*/
public function getFormElementsByTagName(string $tagName) {
public function getFormElementsByTagName(string $tagName): array {
/* @var $form HtmlFormTag */
if ($this instanceof HtmlForm) {
@@ -220,9 +223,10 @@ public function getFormElementsByTagName(string $tagName) {
$tagClassName = $form->getTagClass($tagName);
$formElements = [];
if (count($this->children) > 0) {
$formElements = [];
foreach ($this->children as &$child) {
// when we directly find something - get it!
@@ -236,35 +240,22 @@ public function getFormElementsByTagName(string $tagName) {
}
}
return $formElements;
}
// display extended debug message in case no form elements were found
$parent = $form->getParent();
$docCon = get_class($parent->getDocumentController());
throw new FormException('[' . get_class($this) . '::getFormElementsByType()] No form elements of type "&lt;'
. $tagName . ' /&gt;" composed in ' . 'current form "' . $form->getAttribute('name') . '" in document controller "'
. $docCon . '"!', E_USER_ERROR);
return $formElements;
}
/**
* @param string $class Name of the implementation class of the form elements to return.
*
* @return FormControl[] A list of references on the form elements.
* @throws FormException In case the form element cannot be found or desired tag is not registered.
* @return FormControl[] A list of references on the form elements or an empty list.
*/
public function getFormElementsByType(string $class): array {
/* @var $form HtmlFormTag */
if ($this instanceof HtmlForm) {
$form = $this;
} else {
$form = $this->getForm();
}
$formElements = [];
if (count($this->children) > 0) {
$formElements = [];
foreach ($this->children as &$child) {
// when we directly find something - get it!
@@ -278,15 +269,9 @@ public function getFormElementsByType(string $class): array {
}
}
return $formElements;
}
// display extended debug message in case no form elements were found
$parent = $form->getParent();
$docCon = get_class($parent->getDocumentController());
throw new FormException('[' . get_class($this) . '::getFormElementsByType()] No form elements of type "'
. $class . '" composed in ' . 'current form "' . $form->getAttribute('name') . '" in document controller "'
. $docCon . '"!', E_USER_ERROR);
return $formElements;
}
}

0 comments on commit e8fa2eb

Please sign in to comment.