There seems to be some confusion, so I try to be more specific and
provide an example. The following is quite similar to one of the
actual cases where I have problems with testing inheritance.
/**
* Creates HTML <select> elements
*/
class HtmlSelect {
private $name;
private $options = array();
function setName($name) {
$this->name = $name;
}
function setOptions($options) {
$this->options = $options;
}
/**
* Here we create an array-structure that represents
* the HTML we would like to create (later we can transform
* it to real HTML string, but this kind of array structure
* is easier to work with, especially if we want to apply
* some modifications to it in subclasses).
*/
function toHtmlArray() {
$html_options = array();
foreach ($this->options as $label => $value) {
$html_options[] = array(
"value" => $value,
"_contents" => $label,
);
}
return array(
"_tag" => "select",
"name" => $this->name,
"_contents" => $html_options,
);
}
}
/**
* Creates HTML <select> elements where the first option
* is always empty. Additionally that first <option> element
* has class="empty", so that we can style it differently with CSS.
*/
class NullableHtmlSelect extends HtmlSelect {
function setOptions($options) {
$options = array("" => "") + $options;
parent::setOptions($options);
}
function toHtmlArray() {
$html_select = parent::toHtmlArray();
$html_select["_contents"][""]["class"] = "empty";
return $html_select;
}
}
The new bahavior of subclass is different enough from parent, so that
when I would try to run all tests of parent class on child class, some
of them would fail.
The subclass strongly depends on the bahavior of superclass, adjusting
it only the slightest way, so if I would test it as if the superclass
would be invisible
(as metao suggests),
I would need to duplicate a lot of tests I have already written for
the superclass. This would result in some heavy duplication.
@metao:
I don't understand why my base class shouldn't do anything fancy. What
do you mean by fancy anyway? If my superclass wouldn't do anything
that would be worth testing, then that I would be better off without
that kind of useless base class at all.