Skip to content

Commit 7fc51d2

Browse files
committed
Fixes extension autoloader to behave like Drupal core
1 parent 8b91cfa commit 7fc51d2

File tree

6 files changed

+69
-16
lines changed

6 files changed

+69
-16
lines changed

src/Drupal/DrupalAutoloader.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,12 @@ protected function addModuleNamespaces(): void
234234
$module_dir = $this->drupalRoot . '/' . $module->getPath();
235235
$this->namespaces["Drupal\\$module_name"] = $module_dir . '/src';
236236

237-
// @see drupal_phpunit_get_extension_namespaces
237+
// Extensions can have a \Drupal\Tests\extension namespace for test cases, traits, and other classes such
238+
// as those that extend \Drupal\TestSite\TestSetupInterface.
239+
// @see drupal_phpunit_get_extension_namespaces()
238240
$module_test_dir = $module_dir . '/tests/src';
239241
if (is_dir($module_test_dir)) {
240-
$suite_names = ['Unit', 'Kernel', 'Functional', 'FunctionalJavascript', 'Build'];
241-
foreach ($suite_names as $suite_name) {
242-
$suite_dir = $module_test_dir . '/' . $suite_name;
243-
if (is_dir($suite_dir)) {
244-
// Register the PSR-4 directory for PHPUnit-based suites.
245-
$this->namespaces["Drupal\\Tests\\$module_name\\$suite_name"] = $suite_dir;
246-
}
247-
248-
// Extensions can have a \Drupal\extension\Traits namespace for
249-
// cross-suite trait code.
250-
$trait_dir = $module_test_dir . '/Traits';
251-
if (is_dir($trait_dir)) {
252-
$this->namespaces["Drupal\\Tests\\$module_name\\Traits"] = $trait_dir;
253-
}
254-
}
242+
$this->namespaces["Drupal\\Tests\\$module_name"] = $module_test_dir;
255243
}
256244

257245
$servicesFileName = $module_dir . '/' . $module_name . '.services.yml';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type: module
2+
name: Module with tests
3+
core: 8.x
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Drupal\Tests\module_with_tests\TestSite;
4+
5+
use Drupal\TestSite\TestSetupInterface;
6+
7+
class ModuleWithTestsTestSite implements TestSetupInterface {
8+
9+
protected $modules = [];
10+
11+
public function setup() {
12+
$this->modules = ['module_with_tests', 'views'];
13+
}
14+
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Drupal\Tests\module_with_tests\Traits;
4+
5+
trait ModuleWithTestsTrait {
6+
7+
protected function dummyTrait() {
8+
return TRUE;
9+
}
10+
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Drupal\Tests\module_with_tests\Unit;
4+
5+
use Drupal\Tests\module_with_tests\Traits\ModuleWithTestsTrait;
6+
use Drupal\Tests\UnitTestCase;
7+
8+
/**
9+
* This is a dummy test class.
10+
*
11+
* @group module_with_tests
12+
*/
13+
class ModuleWithTestsTest extends UnitTestCase {
14+
15+
use ModuleWithTestsTrait;
16+
17+
/**
18+
* A dummy test.
19+
*/
20+
public function testModule() {
21+
$this->assertTrue($this->dummyTrait());
22+
}
23+
24+
}

tests/src/DrupalIntegrationTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public function testExtensionReportsError() {
3737
$this->assertEquals('Function phpstan_fixtures_MissingReturnRule() should return string but return statement is missing.', $error->getMessage());
3838
}
3939

40+
public function testExtensionTestSuiteAutoloading() {
41+
$paths = [
42+
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/Unit/ModuleWithTestsTest.php',
43+
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/Traits/ModuleWithTestsTrait.php',
44+
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/TestSite/ModuleWithTestsTestSite.php',
45+
];
46+
foreach ($paths as $path) {
47+
$errors = $this->runAnalyze($path);
48+
$this->assertCount(0, $errors, print_r($errors, true));
49+
}
50+
}
51+
4052
public function testServiceMapping() {
4153
$errorMessages = [
4254
'\Drupal calls should be avoided in classes, use dependency injection instead',

0 commit comments

Comments
 (0)