Issue #1477218 by Berdir, Rob Loach: Convert Tracker tests to PSR-0, register PSR-0 test classes.

8.0.x
catch 2012-04-23 12:01:03 +09:00
parent 85a421eb9b
commit 042de88417
5 changed files with 57 additions and 3 deletions

View File

@ -159,6 +159,7 @@ function simpletest_run_tests($test_list, $reporter = 'drupal') {
* Batch operation callback.
*/
function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
simpletest_classloader_register();
// Get working values.
if (!isset($context['sandbox']['max'])) {
// First iteration: initialize working values.
@ -291,6 +292,9 @@ function simpletest_log_read($test_id, $prefix, $test_class, $during_test = FALS
* a static variable. In order to list tests provided by disabled modules
* hook_registry_files_alter() is used to forcefully add them to the registry.
*
* PSR-0 classes are found by searching the designated directory for each module
* for files matching the PSR-0 standard.
*
* @return
* An array of tests keyed with the groups specified in each of the tests
* getInfo() method and then keyed by the test class. An example of the array
@ -311,6 +315,10 @@ function simpletest_test_get_all() {
$groups = &drupal_static(__FUNCTION__);
if (!$groups) {
// Make sure that namespaces for disabled modules are registered so that the
// checks below will find them.
simpletest_classloader_register();
// Load test information from cache if available, otherwise retrieve the
// information from each tests getInfo() method.
if ($cache = cache()->get('simpletest')) {
@ -318,8 +326,33 @@ function simpletest_test_get_all() {
}
else {
// Select all clases in files ending with .test.
// @todo: Remove this once all tests have been ported to PSR-0.
$classes = db_query("SELECT name FROM {registry} WHERE type = :type AND filename LIKE :name", array(':type' => 'class', ':name' => '%.test'))->fetchCol();
// Select all PSR-0 classes in the Tests namespace of all modules.
$system_list = db_query("SELECT name, filename FROM {system}")->fetchAllKeyed();
foreach ($system_list as $name => $filename) {
// Build directory in which the test files would reside.
$tests_dir = DRUPAL_ROOT . '/' . dirname($filename) . '/lib/Drupal/' . $name . '/Tests';
// Scan it for test files if it exists.
if (is_dir($tests_dir)) {
$files = file_scan_directory($tests_dir, '/.*\.php/');
if (!empty($files)) {
$basedir = DRUPAL_ROOT . '/' . dirname($filename) . '/lib/';
foreach ($files as $file) {
// Convert the file name into the namespaced class name.
$replacements = array(
'/' => '\\',
$basedir => '',
'.php' => '',
);
$classes[] = strtr($file->uri, $replacements);
}
}
}
}
// Check that each class has a getInfo() method and store the information
// in an array keyed with the group specified in the test information.
$groups = array();
@ -355,6 +388,19 @@ function simpletest_test_get_all() {
return $groups;
}
/**
* Registers namespaces for disabled modules.
*/
function simpletest_classloader_register() {
// Get the cached test modules list and register a test namespace for each.
$disabled_modules = db_query("SELECT name, filename FROM {system} WHERE status = 0")->fetchAllKeyed();
if ($disabled_modules) {
foreach ($disabled_modules as $name => $filename) {
drupal_classloader_register($name, dirname($filename));
}
}
}
/**
* Implements hook_registry_files_alter().
*

View File

@ -183,6 +183,7 @@ function theme_simpletest_test_table($variables) {
function simpletest_test_form_submit($form, &$form_state) {
// Get list of tests.
$tests_list = array();
simpletest_classloader_register();
foreach ($form_state['values'] as $class_name => $value) {
// Since class_exists() will likely trigger an autoload lookup,
// we do the fast check first.
@ -233,6 +234,8 @@ function simpletest_result_form($form, &$form_state, $test_id) {
'#debug' => 0,
);
simpletest_classloader_register();
// Cycle through each test group.
$header = array(t('Message'), t('Group'), t('Filename'), t('Line'), t('Function'), array('colspan' => 2, 'data' => t('Status')));
$form['result']['results'] = array();

View File

@ -2,9 +2,13 @@
/**
* @file
* Tests for tracker.module.
* Definition of Drupal\tracker\Tests\TrackerTest.
*/
namespace Drupal\tracker\Tests;
use DrupalWebTestCase;
/**
* Defines a base class for testing tracker.module.
*/

View File

@ -4,4 +4,3 @@ dependencies[] = comment
package = Core
version = VERSION
core = 8.x
files[] = tracker.test

View File

@ -363,6 +363,8 @@ function simpletest_script_run_one_test($test_id, $test_class) {
// Bootstrap Drupal.
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
simpletest_classloader_register();
$test = new $test_class($test_id);
$test->run();
$info = $test->getInfo();
@ -396,7 +398,7 @@ function simpletest_script_command($test_id, $test_class) {
if ($args['color']) {
$command .= ' --color';
}
$command .= " --php " . escapeshellarg($php) . " --test-id $test_id --execute-test $test_class";
$command .= " --php " . escapeshellarg($php) . " --test-id $test_id --execute-test " . escapeshellarg($test_class);
return $command;
}