TestSuiteBase.php 2.2 KB

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