TestSuiteBase.php 2.5 KB

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