Merge pull request #3495 from Icinga/bugfix/tests-don-t-respect-module-paths-3494

icingacli test php unit: include the tests of all installed modules
This commit is contained in:
Eric Lippmann 2018-07-18 13:45:18 +02:00 committed by GitHub
commit 1ae2507f0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 112 additions and 12 deletions

View File

@ -3,8 +3,11 @@
namespace Icinga\Module\Test\Clicommands;
use DOMDocument;
use DOMXPath;
use Icinga\Application\Icinga;
use Icinga\Cli\Command;
use Icinga\File\Storage\TemporaryLocalFileStorage;
/**
* PHP unit- & style-tests
@ -63,10 +66,18 @@ class PhpCommand extends Command
$options[] = $include;
}
chdir(Icinga::app()->getBaseDir());
$baseDir = Icinga::app()->getBaseDir();
$phpunitXml = new DOMDocument();
$temp = new TemporaryLocalFileStorage();
$phpunitXml->loadXML(file_get_contents("$baseDir/modules/test/phpunit.xml"));
$this->adjustPhpunitDom($phpunitXml);
$temp->create('phpunit.xml', $phpunitXml->saveXML());
chdir($baseDir);
$command = $this->getEnvironmentVariables()
. $phpUnit
. ' -c modules/test/phpunit.xml'
. " -c {$temp->resolvePath('phpunit.xml')}"
. ' ' . join(' ', array_merge($options, $this->params->getAllStandalone()));
if ($this->isVerbose) {
@ -175,9 +186,15 @@ class PhpCommand extends Command
*/
protected function getEnvironmentVariables()
{
$modulePaths = [];
foreach (Icinga::app()->getModuleManager()->getModuleInfo() as $module) {
$modulePaths[] = $module->path;
}
$vars = array();
$vars[] = sprintf('ICINGAWEB_BASEDIR=%s', $this->app->getBaseDir());
$vars[] = sprintf('ICINGAWEB_ICINGA_LIB=%s', $this->app->getLibraryDir('Icinga'));
$vars[] = sprintf('ICINGAWEB_MODULE_DIRS=%s', implode(':', $modulePaths));
// Disabled as the bootstrap.php for PHPUnit and class BaseTestCase can't handle multiple paths yet
/*$vars[] = sprintf(
@ -187,4 +204,66 @@ class PhpCommand extends Command
return join(' ', $vars) . ' ';
}
/**
* Make all relative paths absolute and include all installed modules
*
* @param DOMDocument $phpunitXml
*/
protected function adjustPhpunitDom(DOMDocument $phpunitXml)
{
$app = Icinga::app();
$modulesTest = "{$app->getBaseDir()}/modules/test/";
$domPath = new DOMXPath($phpunitXml);
$phpunit = $domPath->query("//phpunit")->item(0);
$phpunit->setAttribute('bootstrap', $modulesTest . $phpunit->getAttribute('bootstrap'));
foreach ([
'//phpunit/testsuites/testsuite/directory',
'//phpunit/testsuites/testsuite/exclude',
'//phpunit/filter/whitelist/directory',
'//phpunit/filter/whitelist/exclude/directory',
'//phpunit/filter/whitelist/exclude/file'
] as $xPath) {
$nodes = $domPath->query($xPath);
for ($i = 0; $i < $nodes->length; ++$i) {
$element = $nodes->item($i);
$element->nodeValue = $modulesTest . $element->nodeValue;
}
}
$unitModules = $domPath->query("//phpunit/testsuites/testsuite[@name='unit-modules']")->item(0);
$regressionModules = $domPath->query("//phpunit/testsuites/testsuite[@name='regression-modules']")->item(0);
while ($unitModules->hasChildNodes()) {
$unitModules->removeChild($unitModules->childNodes->item(0));
}
while ($regressionModules->hasChildNodes()) {
$regressionModules->removeChild($regressionModules->childNodes->item(0));
}
foreach ($app->getModuleManager()->getModuleInfo() as $module) {
$testPhp = "$module->path/test/php";
if (file_exists($testPhp)) {
$unitModules->appendChild($phpunitXml->createElement('directory', $testPhp));
$testPhpRegression = "$testPhp/regression";
if (file_exists($testPhpRegression)) {
$regressionModules->appendChild($phpunitXml->createElement('directory', $testPhpRegression));
$unitModules->appendChild($phpunitXml->createElement('exclude', $testPhpRegression));
}
}
}
if (! $unitModules->hasChildNodes()) {
$unitModules->parentNode->removeChild($unitModules);
}
if (! $regressionModules->hasChildNodes()) {
$regressionModules->parentNode->removeChild($regressionModules);
}
}
}

View File

@ -4,11 +4,13 @@
<!--
Unit testing
-->
<testsuite name="unit">
<testsuite name="unit-framework">
<directory>../../test/php/application/</directory>
<directory>../../test/php/library/</directory>
</testsuite>
<!-- Module tests are independent from core tests -->
<!-- Module tests are independent from core tests -->
<testsuite name="unit-modules">
<directory>../*/test/php</directory>
<exclude>../*/test/php/regression</exclude>
</testsuite>
@ -16,8 +18,12 @@
<!--
Regression testing
-->
<testsuite name="regression">
<testsuite name="regression-framework">
<directory>../../test/php/regression/</directory>
</testsuite>
<!-- Module tests are independent from core tests -->
<testsuite name="regression-modules">
<directory>../*/test/php/regression</directory>
</testsuite>
</testsuites>

View File

@ -35,25 +35,40 @@ $loader->registerNamespace('Tests', $testLibraryPath);
$loader->registerNamespace('Icinga', $icingaLibPath);
$loader->registerNamespace('Icinga\\Forms', $applicationPath . '/forms');
$modules = scandir($modulePath);
foreach ($modules as $module) {
if ($module === '.' || $module === '..') {
continue;
$modulePaths = getenv('ICINGAWEB_MODULE_DIRS');
if ($modulePaths) {
$modulePaths = preg_split('/:/', $modulePaths, -1, PREG_SPLIT_NO_EMPTY);
}
if (! $modulePaths) {
$modulePaths = array_flip(scandir($modulePath));
unset($modulePaths['.']);
unset($modulePaths['..']);
$modulePaths = array_keys($modulePaths);
foreach ($modulePaths as &$path) {
$path = "$modulePath/$path";
}
unset($path);
}
foreach ($modulePaths as $path) {
$module = basename($path);
$moduleNamespace = 'Icinga\\Module\\' . ucfirst($module);
$moduleLibraryPath = $modulePath . '/' . $module . '/library/' . ucfirst($module);
$moduleLibraryPath = "$path/library/" . ucfirst($module);
if (is_dir($moduleLibraryPath)) {
$loader->registerNamespace($moduleNamespace, $moduleLibraryPath);
}
$moduleTestPath = $modulePath . '/' . $module . '/test/php';
$moduleTestPath = "$path/test/php";
if (is_dir($moduleTestPath)) {
$loader->registerNamespace('Tests\\' . $moduleNamespace, $moduleTestPath);
}
$moduleFormPath = $modulePath . '/' . $module . '/application/forms';
$moduleFormPath = "$path/application/forms";
if (is_dir($moduleFormPath)) {
$loader->registerNamespace($moduleNamespace . '\\Forms', $moduleFormPath);
}