annotate core/tests/TestSuites/TestSuiteBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\TestSuites;
Chris@0 4
Chris@0 5 use Drupal\simpletest\TestDiscovery;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Base class for Drupal test suites.
Chris@0 9 */
Chris@0 10 abstract class TestSuiteBase extends \PHPUnit_Framework_TestSuite {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Finds extensions in a Drupal installation.
Chris@0 14 *
Chris@0 15 * An extension is defined as a directory with an *.info.yml file in it.
Chris@0 16 *
Chris@0 17 * @param string $root
Chris@0 18 * Path to the root of the Drupal installation.
Chris@0 19 *
Chris@0 20 * @return string[]
Chris@0 21 * Associative array of extension paths, with extension name as keys.
Chris@0 22 */
Chris@0 23 protected function findExtensionDirectories($root) {
Chris@0 24 $extension_roots = \drupal_phpunit_contrib_extension_directory_roots($root);
Chris@0 25
Chris@0 26 $extension_directories = array_map('drupal_phpunit_find_extension_directories', $extension_roots);
Chris@0 27 return array_reduce($extension_directories, 'array_merge', []);
Chris@0 28 }
Chris@0 29
Chris@0 30 /**
Chris@0 31 * Find and add tests to the suite for core and any extensions.
Chris@0 32 *
Chris@0 33 * @param string $root
Chris@0 34 * Path to the root of the Drupal installation.
Chris@0 35 * @param string $suite_namespace
Chris@0 36 * SubNamespace used to separate test suite. Examples: Unit, Functional.
Chris@0 37 */
Chris@0 38 protected function addTestsBySuiteNamespace($root, $suite_namespace) {
Chris@0 39 // Core's tests are in the namespace Drupal\${suite_namespace}Tests\ and are
Chris@0 40 // always inside of core/tests/Drupal/${suite_namespace}Tests. The exception
Chris@0 41 // to this is Unit tests for historical reasons.
Chris@0 42 if ($suite_namespace == 'Unit') {
Chris@0 43 $this->addTestFiles(TestDiscovery::scanDirectory("Drupal\\Tests\\", "$root/core/tests/Drupal/Tests"));
Chris@0 44 }
Chris@0 45 else {
Chris@0 46 $this->addTestFiles(TestDiscovery::scanDirectory("Drupal\\${suite_namespace}Tests\\", "$root/core/tests/Drupal/${suite_namespace}Tests"));
Chris@0 47 }
Chris@0 48
Chris@0 49 // Extensions' tests will always be in the namespace
Chris@0 50 // Drupal\Tests\$extension_name\$suite_namespace\ and be in the
Chris@0 51 // $extension_path/tests/src/$suite_namespace directory. Not all extensions
Chris@0 52 // will have all kinds of tests.
Chris@0 53 foreach ($this->findExtensionDirectories($root) as $extension_name => $dir) {
Chris@0 54 $test_path = "$dir/tests/src/$suite_namespace";
Chris@0 55 if (is_dir($test_path)) {
Chris@0 56 $this->addTestFiles(TestDiscovery::scanDirectory("Drupal\\Tests\\$extension_name\\$suite_namespace\\", $test_path));
Chris@0 57 }
Chris@0 58 }
Chris@0 59 }
Chris@0 60
Chris@0 61 }