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:
commit
1ae2507f0e
|
@ -3,8 +3,11 @@
|
||||||
|
|
||||||
namespace Icinga\Module\Test\Clicommands;
|
namespace Icinga\Module\Test\Clicommands;
|
||||||
|
|
||||||
|
use DOMDocument;
|
||||||
|
use DOMXPath;
|
||||||
use Icinga\Application\Icinga;
|
use Icinga\Application\Icinga;
|
||||||
use Icinga\Cli\Command;
|
use Icinga\Cli\Command;
|
||||||
|
use Icinga\File\Storage\TemporaryLocalFileStorage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PHP unit- & style-tests
|
* PHP unit- & style-tests
|
||||||
|
@ -63,10 +66,18 @@ class PhpCommand extends Command
|
||||||
$options[] = $include;
|
$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()
|
$command = $this->getEnvironmentVariables()
|
||||||
. $phpUnit
|
. $phpUnit
|
||||||
. ' -c modules/test/phpunit.xml'
|
. " -c {$temp->resolvePath('phpunit.xml')}"
|
||||||
. ' ' . join(' ', array_merge($options, $this->params->getAllStandalone()));
|
. ' ' . join(' ', array_merge($options, $this->params->getAllStandalone()));
|
||||||
|
|
||||||
if ($this->isVerbose) {
|
if ($this->isVerbose) {
|
||||||
|
@ -175,9 +186,15 @@ class PhpCommand extends Command
|
||||||
*/
|
*/
|
||||||
protected function getEnvironmentVariables()
|
protected function getEnvironmentVariables()
|
||||||
{
|
{
|
||||||
|
$modulePaths = [];
|
||||||
|
foreach (Icinga::app()->getModuleManager()->getModuleInfo() as $module) {
|
||||||
|
$modulePaths[] = $module->path;
|
||||||
|
}
|
||||||
|
|
||||||
$vars = array();
|
$vars = array();
|
||||||
$vars[] = sprintf('ICINGAWEB_BASEDIR=%s', $this->app->getBaseDir());
|
$vars[] = sprintf('ICINGAWEB_BASEDIR=%s', $this->app->getBaseDir());
|
||||||
$vars[] = sprintf('ICINGAWEB_ICINGA_LIB=%s', $this->app->getLibraryDir('Icinga'));
|
$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
|
// Disabled as the bootstrap.php for PHPUnit and class BaseTestCase can't handle multiple paths yet
|
||||||
/*$vars[] = sprintf(
|
/*$vars[] = sprintf(
|
||||||
|
@ -187,4 +204,66 @@ class PhpCommand extends Command
|
||||||
|
|
||||||
return join(' ', $vars) . ' ';
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,13 @@
|
||||||
<!--
|
<!--
|
||||||
Unit testing
|
Unit testing
|
||||||
-->
|
-->
|
||||||
<testsuite name="unit">
|
<testsuite name="unit-framework">
|
||||||
<directory>../../test/php/application/</directory>
|
<directory>../../test/php/application/</directory>
|
||||||
<directory>../../test/php/library/</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>
|
<directory>../*/test/php</directory>
|
||||||
<exclude>../*/test/php/regression</exclude>
|
<exclude>../*/test/php/regression</exclude>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
@ -16,8 +18,12 @@
|
||||||
<!--
|
<!--
|
||||||
Regression testing
|
Regression testing
|
||||||
-->
|
-->
|
||||||
<testsuite name="regression">
|
<testsuite name="regression-framework">
|
||||||
<directory>../../test/php/regression/</directory>
|
<directory>../../test/php/regression/</directory>
|
||||||
|
</testsuite>
|
||||||
|
|
||||||
|
<!-- Module tests are independent from core tests -->
|
||||||
|
<testsuite name="regression-modules">
|
||||||
<directory>../*/test/php/regression</directory>
|
<directory>../*/test/php/regression</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
|
|
|
@ -35,25 +35,40 @@ $loader->registerNamespace('Tests', $testLibraryPath);
|
||||||
$loader->registerNamespace('Icinga', $icingaLibPath);
|
$loader->registerNamespace('Icinga', $icingaLibPath);
|
||||||
$loader->registerNamespace('Icinga\\Forms', $applicationPath . '/forms');
|
$loader->registerNamespace('Icinga\\Forms', $applicationPath . '/forms');
|
||||||
|
|
||||||
$modules = scandir($modulePath);
|
$modulePaths = getenv('ICINGAWEB_MODULE_DIRS');
|
||||||
foreach ($modules as $module) {
|
|
||||||
if ($module === '.' || $module === '..') {
|
if ($modulePaths) {
|
||||||
continue;
|
$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);
|
$moduleNamespace = 'Icinga\\Module\\' . ucfirst($module);
|
||||||
$moduleLibraryPath = $modulePath . '/' . $module . '/library/' . ucfirst($module);
|
$moduleLibraryPath = "$path/library/" . ucfirst($module);
|
||||||
|
|
||||||
if (is_dir($moduleLibraryPath)) {
|
if (is_dir($moduleLibraryPath)) {
|
||||||
$loader->registerNamespace($moduleNamespace, $moduleLibraryPath);
|
$loader->registerNamespace($moduleNamespace, $moduleLibraryPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
$moduleTestPath = $modulePath . '/' . $module . '/test/php';
|
$moduleTestPath = "$path/test/php";
|
||||||
if (is_dir($moduleTestPath)) {
|
if (is_dir($moduleTestPath)) {
|
||||||
$loader->registerNamespace('Tests\\' . $moduleNamespace, $moduleTestPath);
|
$loader->registerNamespace('Tests\\' . $moduleNamespace, $moduleTestPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
$moduleFormPath = $modulePath . '/' . $module . '/application/forms';
|
$moduleFormPath = "$path/application/forms";
|
||||||
if (is_dir($moduleFormPath)) {
|
if (is_dir($moduleFormPath)) {
|
||||||
$loader->registerNamespace($moduleNamespace . '\\Forms', $moduleFormPath);
|
$loader->registerNamespace($moduleNamespace . '\\Forms', $moduleFormPath);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue