diff --git a/lib/phpunit/classes/util.php b/lib/phpunit/classes/util.php index 92d8c9dfa19..f08f490cf64 100644 --- a/lib/phpunit/classes/util.php +++ b/lib/phpunit/classes/util.php @@ -1026,17 +1026,8 @@ class phpunit_util { $ftemplate = file_get_contents("$CFG->dirroot/phpunit.xml.dist"); $ftemplate = preg_replace('|', $ftemplate); - // Get all the components - $components = self::get_all_plugins_with_tests() + self::get_all_subsystems_with_tests(); - - // Get all the directories having tests - $directories = self::get_all_directories_with_tests(); - - // Find any directory not covered by proper components - $remaining = array_diff($directories, $components); - - // Add them to the list of components - $components += $remaining; + // Gets all the components with tests + $components = tests_finder::get_components_with_tests('phpunit'); // Create the corresponding phpunit.xml file for each component foreach ($components as $cname => $cpath) { @@ -1065,112 +1056,6 @@ class phpunit_util { } } - /** - * Returns all the plugins having PHPUnit tests - * - * @return array all the plugins having PHPUnit tests - * - */ - private static function get_all_plugins_with_tests() { - $pluginswithtests = array(); - - $plugintypes = get_plugin_types(); - ksort($plugintypes); - foreach ($plugintypes as $type => $unused) { - $plugs = get_plugin_list($type); - ksort($plugs); - foreach ($plugs as $plug => $fullplug) { - // Look for tests recursively - if (self::directory_has_tests($fullplug)) { - $pluginswithtests[$type . '_' . $plug] = $fullplug; - } - } - } - return $pluginswithtests; - } - - /** - * Returns all the subsystems having PHPUnit tests - * - * Note we are hacking here the list of subsystems - * to cover some well-known subsystems that are not properly - * returned by the {@link get_core_subsystems()} function. - * - * @return array all the subsystems having PHPUnit tests - */ - private static function get_all_subsystems_with_tests() { - global $CFG; - - $subsystemswithtests = array(); - - $subsystems = get_core_subsystems(); - - // Hack the list a bit to cover some well-known ones - $subsystems['backup'] = 'backup'; - $subsystems['db-dml'] = 'lib/dml'; - $subsystems['db-ddl'] = 'lib/ddl'; - - ksort($subsystems); - foreach ($subsystems as $subsys => $relsubsys) { - if ($relsubsys === null) { - continue; - } - $fullsubsys = $CFG->dirroot . '/' . $relsubsys; - if (!is_dir($fullsubsys)) { - continue; - } - // Look for tests recursively - if (self::directory_has_tests($fullsubsys)) { - $subsystemswithtests['core_' . $subsys] = $fullsubsys; - } - } - return $subsystemswithtests; - } - - /** - * Returns all the directories having tests - * - * @return array all directories having tests - */ - private static function get_all_directories_with_tests() { - global $CFG; - - $dirs = array(); - $dirite = new RecursiveDirectoryIterator($CFG->dirroot); - $iteite = new RecursiveIteratorIterator($dirite); - $sep = preg_quote(DIRECTORY_SEPARATOR, '|'); - $regite = new RegexIterator($iteite, '|'.$sep.'tests'.$sep.'.*_test\.php$|'); - foreach ($regite as $path => $element) { - $key = dirname(dirname($path)); - $value = trim(str_replace('/', '_', str_replace($CFG->dirroot, '', $key)), '_'); - $dirs[$key] = $value; - } - ksort($dirs); - return array_flip($dirs); - } - - /** - * Returns if a given directory has tests (recursively) - * - * @param $dir string full path to the directory to look for phpunit tests - * @return bool if a given directory has tests (true) or no (false) - */ - private static function directory_has_tests($dir) { - if (!is_dir($dir)) { - return false; - } - - $dirite = new RecursiveDirectoryIterator($dir); - $iteite = new RecursiveIteratorIterator($dirite); - $sep = preg_quote(DIRECTORY_SEPARATOR, '|'); - $regite = new RegexIterator($iteite, '|'.$sep.'tests'.$sep.'.*_test\.php$|'); - $regite->rewind(); - if ($regite->valid()) { - return true; - } - return false; - } - /** * To be called from debugging() only. * @param string $message diff --git a/lib/phpunit/lib.php b/lib/phpunit/lib.php index 64d72f9e0fd..ac30f623e94 100644 --- a/lib/phpunit/lib.php +++ b/lib/phpunit/lib.php @@ -37,3 +37,4 @@ require_once(__DIR__.'/classes/advanced_testcase.php'); require_once(__DIR__.'/classes/unittestcase.php'); require_once(__DIR__.'/classes/hint_resultprinter.php'); // Loaded here because phpunit.xml does not support relative links for printerFile require_once(__DIR__.'/../testing/classes/test_lock.php'); +require_once(__DIR__.'/../testing/classes/tests_finder.php'); diff --git a/lib/testing/classes/tests_finder.php b/lib/testing/classes/tests_finder.php new file mode 100644 index 00000000000..0a89cfa586c --- /dev/null +++ b/lib/testing/classes/tests_finder.php @@ -0,0 +1,190 @@ +. + +/** + * Tests finder + * + * @package core + * @category test + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Finds components and plugins with tests + * + * @package core + * @category test + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tests_finder { + + /** + * Returns all the components with tests of the specified type + * @param string $testtype The kind of test we are looking for + * @return array + */ + public static function get_components_with_tests($testtype) { + + // Get all the components + $components = self::get_all_plugins_with_tests($testtype) + self::get_all_subsystems_with_tests($testtype); + + // Get all the directories having tests + $directories = self::get_all_directories_with_tests($testtype); + + // Find any directory not covered by proper components + $remaining = array_diff($directories, $components); + + // Add them to the list of components + $components += $remaining; + + return $components; + } + + /** + * Returns all the plugins having tests + * @param string $testtype The kind of test we are looking for + * @return array all the plugins having tests + */ + private static function get_all_plugins_with_tests($testtype) { + $pluginswithtests = array(); + + $plugintypes = get_plugin_types(); + ksort($plugintypes); + foreach ($plugintypes as $type => $unused) { + $plugs = get_plugin_list($type); + ksort($plugs); + foreach ($plugs as $plug => $fullplug) { + // Look for tests recursively + if (self::directory_has_tests($fullplug, $testtype)) { + $pluginswithtests[$type . '_' . $plug] = $fullplug; + } + } + } + return $pluginswithtests; + } + + /** + * Returns all the subsystems having tests + * + * Note we are hacking here the list of subsystems + * to cover some well-known subsystems that are not properly + * returned by the {@link get_core_subsystems()} function. + * + * @param string $testtype The kind of test we are looking for + * @return array all the subsystems having tests + */ + private static function get_all_subsystems_with_tests($testtype) { + global $CFG; + + $subsystemswithtests = array(); + + $subsystems = get_core_subsystems(); + + // Hack the list a bit to cover some well-known ones + $subsystems['backup'] = 'backup'; + $subsystems['db-dml'] = 'lib/dml'; + $subsystems['db-ddl'] = 'lib/ddl'; + + ksort($subsystems); + foreach ($subsystems as $subsys => $relsubsys) { + if ($relsubsys === null) { + continue; + } + $fullsubsys = $CFG->dirroot . '/' . $relsubsys; + if (!is_dir($fullsubsys)) { + continue; + } + // Look for tests recursively + if (self::directory_has_tests($fullsubsys, $testtype)) { + $subsystemswithtests['core_' . $subsys] = $fullsubsys; + } + } + return $subsystemswithtests; + } + + /** + * Returns all the directories having tests + * + * @param string $testtype The kind of test we are looking for + * @return array all directories having tests + */ + private static function get_all_directories_with_tests($testtype) { + global $CFG; + + $dirs = array(); + $dirite = new RecursiveDirectoryIterator($CFG->dirroot); + $iteite = new RecursiveIteratorIterator($dirite); + $regexp = self::get_regexp($testtype); + $regite = new RegexIterator($iteite, $regexp); + foreach ($regite as $path => $element) { + $key = dirname(dirname($path)); + $value = trim(str_replace('/', '_', str_replace($CFG->dirroot, '', $key)), '_'); + $dirs[$key] = $value; + } + ksort($dirs); + return array_flip($dirs); + } + + /** + * Returns if a given directory has tests (recursively) + * + * @param string $dir full path to the directory to look for phpunit tests + * @param string $testtype phpunit|behat + * @return bool if a given directory has tests (true) or no (false) + */ + private static function directory_has_tests($dir, $testtype) { + if (!is_dir($dir)) { + return false; + } + + $dirite = new RecursiveDirectoryIterator($dir); + $iteite = new RecursiveIteratorIterator($dirite); + $regexp = self::get_regexp($testtype); + $regite = new RegexIterator($iteite, $regexp); + $regite->rewind(); + if ($regite->valid()) { + return true; + } + return false; + } + + + /** + * Returns the regular expression to match by the test files + * @param string $testtype + * @return string + */ + private static function get_regexp($testtype) { + + $sep = preg_quote(DIRECTORY_SEPARATOR, '|'); + + switch ($testtype) { + case 'phpunit': + $regexp = '|'.$sep.'tests'.$sep.'.*_test\.php$|'; + break; + case 'features': + $regexp = '|'.$sep.'tests'.$sep.'behat'.$sep.'.*\.feature$|'; + break; + case 'stepsdefinitions': + $regexp = '|'.$sep.'tests'.$sep.'behat'.$sep.'.*\.php$|'; + break; + } + + return $regexp; + } +}