From fd239fa40c118c5d4f39d148da4ec8154ffe0c15 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 28 Jun 2018 14:26:37 +0200 Subject: [PATCH 1/3] Separate framework and modules tests logically refs #3494 --- modules/test/phpunit.xml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/test/phpunit.xml b/modules/test/phpunit.xml index f2d8c0644..da5d10674 100644 --- a/modules/test/phpunit.xml +++ b/modules/test/phpunit.xml @@ -4,11 +4,13 @@ - + ../../test/php/application/ ../../test/php/library/ + - + + ../*/test/php ../*/test/php/regression @@ -16,8 +18,12 @@ - + ../../test/php/regression/ + + + + ../*/test/php/regression From 6da5d4173e12c4c9767057f77604b5cbb2f1add2 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 28 Jun 2018 16:05:18 +0200 Subject: [PATCH 2/3] icingacli test php unit: include the tests of all installed modules refs #3494 --- .../application/clicommands/PhpCommand.php | 77 ++++++++++++++++++- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/modules/test/application/clicommands/PhpCommand.php b/modules/test/application/clicommands/PhpCommand.php index 8a62deffd..a2553943c 100644 --- a/modules/test/application/clicommands/PhpCommand.php +++ b/modules/test/application/clicommands/PhpCommand.php @@ -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) { @@ -187,4 +198,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); + } + } } From 0c364f7866b8521b08c14ffe4133d4ba767dd88b Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Tue, 10 Jul 2018 15:22:47 +0200 Subject: [PATCH 3/3] icingacli test php unit: include the classes of all installed modules refs #3494 --- .../application/clicommands/PhpCommand.php | 6 ++++ test/php/bootstrap.php | 29 ++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/modules/test/application/clicommands/PhpCommand.php b/modules/test/application/clicommands/PhpCommand.php index a2553943c..926ece7dc 100644 --- a/modules/test/application/clicommands/PhpCommand.php +++ b/modules/test/application/clicommands/PhpCommand.php @@ -186,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( diff --git a/test/php/bootstrap.php b/test/php/bootstrap.php index 2c805e8b5..6e3345c14 100644 --- a/test/php/bootstrap.php +++ b/test/php/bootstrap.php @@ -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); }