diff --git a/core/modules/simpletest/src/TestDiscovery.php b/core/modules/simpletest/src/TestDiscovery.php index ae5dc525880d35316573b51e063454ecda237377..02bf7071fbc6e46eef01109c3a105369d5011cc9 100644 --- a/core/modules/simpletest/src/TestDiscovery.php +++ b/core/modules/simpletest/src/TestDiscovery.php @@ -119,6 +119,10 @@ public function registerTestNamespaces() { $this->testNamespaces["Drupal\\Tests\\$name\\Kernel\\"][] = "$base_path/tests/src/Kernel"; $this->testNamespaces["Drupal\\Tests\\$name\\Functional\\"][] = "$base_path/tests/src/Functional"; $this->testNamespaces["Drupal\\Tests\\$name\\FunctionalJavascript\\"][] = "$base_path/tests/src/FunctionalJavascript"; + + // Add discovery for traits which are shared between different test + // suites. + $this->testNamespaces["Drupal\\Tests\\$name\\Traits\\"][] = "$base_path/tests/src/Traits"; } foreach ($this->testNamespaces as $prefix => $paths) { diff --git a/core/modules/simpletest/tests/src/Traits/TestTrait.php b/core/modules/simpletest/tests/src/Traits/TestTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..eeac4963ecc55b7a403f33ea694ab85ba161ae73 --- /dev/null +++ b/core/modules/simpletest/tests/src/Traits/TestTrait.php @@ -0,0 +1,32 @@ +<?php + +namespace Drupal\Tests\simpletest\Traits; + +/** + * A nothing trait, but declared in the Drupal\Tests namespace. + * + * We use this trait to test autoloading of traits outside of the normal test + * suite namespaces. + * + * @see \Drupal\Tests\simpletest\Unit\TraitAccessTest + */ +trait TestTrait { + + /** + * Random string for a not very interesting trait. + * + * @var string + */ + protected $stuff = 'stuff'; + + /** + * Return a test string to a trait user. + * + * @return string + * Just a random sort of string. + */ + protected function getStuff() { + return $this->stuff; + } + +} diff --git a/core/modules/simpletest/tests/src/Unit/TraitAccessTest.php b/core/modules/simpletest/tests/src/Unit/TraitAccessTest.php new file mode 100644 index 0000000000000000000000000000000000000000..dbaffbf9ab0101dbf0d2ede864d601296b69eb01 --- /dev/null +++ b/core/modules/simpletest/tests/src/Unit/TraitAccessTest.php @@ -0,0 +1,25 @@ +<?php + +namespace Drupal\Tests\simpletest\Unit; + +use Drupal\Tests\UnitTestCase; +use Drupal\Tests\simpletest\Traits\TestTrait; + +/** + * Test whether traits are autoloaded during PHPUnit discovery time. + * + * @group simpletest + */ +class TraitAccessTest extends UnitTestCase { + + use TestTrait; + + /** + * @coversNothing + */ + public function testSimpleStuff() { + $stuff = $this->getStuff(); + $this->assertSame($stuff, 'stuff', "Same old stuff"); + } + +} diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php index 9aeb1d79f047880ada1201f94f9f538cc4efde4c..a2b4921a4525e96178748602266cec78ea5981b7 100644 --- a/core/tests/bootstrap.php +++ b/core/tests/bootstrap.php @@ -77,15 +77,28 @@ function drupal_phpunit_contrib_extension_directory_roots($root = NULL) { * An associative array of extension directories, keyed by their namespace. */ function drupal_phpunit_get_extension_namespaces($dirs) { + $suite_names = ['Unit', 'Kernel', 'Functional', 'FunctionalJavascript']; $namespaces = array(); foreach ($dirs as $extension => $dir) { if (is_dir($dir . '/src')) { // Register the PSR-4 directory for module-provided classes. $namespaces['Drupal\\' . $extension . '\\'][] = $dir . '/src'; } - if (is_dir($dir . '/tests/src')) { - // Register the PSR-4 directory for PHPUnit test classes. - $namespaces['Drupal\\Tests\\' . $extension . '\\'][] = $dir . '/tests/src'; + $test_dir = $dir . '/tests/src'; + if (is_dir($test_dir)) { + foreach ($suite_names as $suite_name) { + $suite_dir = $test_dir . '/' . $suite_name; + if (is_dir($suite_dir)) { + // Register the PSR-4 directory for PHPUnit-based suites. + $namespaces['Drupal\\Tests\\' . $extension . '\\' . $suite_name . '\\'][] = $suite_dir; + } + } + // Extensions can have a \Drupal\extension\Traits namespace for + // cross-suite trait code. + $trait_dir = $test_dir . '/Traits'; + if (is_dir($trait_dir)) { + $namespaces['Drupal\\Tests\\' . $extension . '\\Traits\\'][] = $trait_dir; + } } } return $namespaces;