sebastianbergmann/phpunit — Issue #385: TestSuite inheritance
Author: dmitri--
Created: 2011-10-26 23:36:09+00:00
State: open

my case is very simple:
- I have one class which has all necessary tests for v1 of DB (1st instance)
- I need a class with tests for v2 of DB (another instance) which of course would contain tests for v2 working as v1 (it's backward compatible)
It looks quite natural that 2nd class should be derived from the 1st one -- it will inherit all the tests and code in setup/teardown methods. Everything so goog so far, but it does not work in phpUnit.
It appears that it includes 2nd class first and since the module contains include() statement for the module with the 1st class, both are loaded at once and only 2nd class is recognized by phpUnit.
To demonstrate the issue I created two simple files. The content of the tests is trivial for the sake of this demonstration being simiple:

StackTest.php:

assertEquals(0, 0);
}
}
?>

Stack2Test.php:

assertEquals(0, 0);
}
}
?>

Expected results:
-3 methods are tested: StackTest::testPushAndPop, Stack2Test::testPushAndPop, Stack2Test::testPushAndPop2

Actual result:
-2 methods are tested: Stack2Test::testPushAndPop, Stack2Test::testPushAndPop2

It looks like the following code

$filename = PHPUnit_Util_Fileloader::checkAndLoad($filename, $syntaxCheck);
$newClasses = PHPUnit_Util_Class::collectEnd();
$baseName = str_replace('.php', '', basename($filename));

foreach ($newClasses as $className) {
if (substr($className, 0 - strlen($baseName)) == $baseName) {
$class = new ReflectionClass($className);

if ($class->getFileName() == $filename) {
$newClasses = array($className);
break;
}
}
}

does not take into account that one include() can include more than one file and more than one class would be loaded from them.