Skip to content

Commit 165429a

Browse files
authored
Fix incompatibility without drupal/core-dev (#246)
* Remove exit code 255 check from no_phpunit job * drop including bootstrap.php for explicit test namespace registration * Ignore test paths on no core-dev jobs * BrowserTestBaseDefaultThemeRule triggers autoloading error when no PHPUnit * Use fix from #216
1 parent 6001de5 commit 165429a

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ jobs:
156156
run: |
157157
cd ~/drupal
158158
COMPOSER_MEMORY_LIMIT=-1 composer require mglaman/phpstan-drupal *@dev
159-
cp $GITHUB_WORKSPACE/tests/fixtures/config/drupal-phpstan.neon phpstan.neon
159+
cp $GITHUB_WORKSPACE/tests/fixtures/config/drupal-no-dev-phpstan.neon phpstan.neon
160160
- name: "Test core/install.php"
161161
run: |
162162
cd ~/drupal
163163
./vendor/bin/phpstan analyze web/core/install.php --debug
164164
- name: "Test no crash"
165165
run: |
166166
cd ~/drupal
167-
./vendor/bin/phpstan analyze web/core/modules/dynamic_page_cache --debug || if (($? == 255)); then false; else true; fi
167+
./vendor/bin/phpstan analyze web/core/modules/dynamic_page_cache --debug

src/Drupal/DrupalAutoloader.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,12 @@ public function register(Container $container): void
8989
return strpos($a->getName(), '_test') !== false ? 10 : 0;
9090
});
9191
$this->themeData = $extensionDiscovery->scan('theme');
92-
$this->addTestNamespaces();
92+
$this->addCoreTestNamespaces();
9393
$this->addModuleNamespaces();
9494
$this->addThemeNamespaces();
9595
$this->registerPs4Namespaces($this->namespaces);
9696
$this->loadLegacyIncludes();
9797

98-
// @todo stop requiring the bootstrap.php and just copy what is needed.
99-
if (interface_exists(\PHPUnit\Framework\Test::class)) {
100-
require $this->drupalRoot . '/core/tests/bootstrap.php';
101-
}
102-
10398
foreach ($this->moduleData as $extension) {
10499
$this->loadExtension($extension);
105100

@@ -196,6 +191,11 @@ class: Drupal\jsonapi\Routing\JsonApiParamEnhancer
196191

197192
$service_map = $container->getByType(ServiceMap::class);
198193
$service_map->setDrupalServices($this->serviceMap);
194+
195+
if (interface_exists(\PHPUnit\Framework\Test::class)
196+
&& class_exists('Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter')) {
197+
\Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter::mutateTestBase($this->autoloader);
198+
}
199199
}
200200

201201
protected function loadLegacyIncludes(): void
@@ -206,7 +206,7 @@ protected function loadLegacyIncludes(): void
206206
}
207207
}
208208

209-
protected function addTestNamespaces(): void
209+
protected function addCoreTestNamespaces(): void
210210
{
211211
// Add core test namespaces.
212212
$core_tests_dir = $this->drupalRoot . '/core/tests/Drupal';
@@ -246,13 +246,38 @@ protected function addModuleNamespaces(): void
246246
$this->serviceClassProviders[$module_name] = $class;
247247
$serviceId = "service_provider.$module_name.service_provider";
248248
$this->serviceMap[$serviceId] = ['class' => $class];
249+
250+
$this->registerExtensionTestNamespace($module);
249251
}
250252
}
251253
protected function addThemeNamespaces(): void
252254
{
253255
foreach ($this->themeData as $theme_name => $theme) {
254256
$theme_dir = $this->drupalRoot . '/' . $theme->getPath();
255257
$this->namespaces["Drupal\\$theme_name"] = $theme_dir . '/src';
258+
$this->registerExtensionTestNamespace($theme);
259+
}
260+
}
261+
262+
protected function registerExtensionTestNamespace(Extension $extension): void
263+
{
264+
$suite_names = ['Unit', 'Kernel', 'Functional', 'Build', 'FunctionalJavascript'];
265+
$dir = $this->drupalRoot . '/' . $extension->getPath();
266+
$test_dir = $dir . '/tests/src';
267+
if (is_dir($test_dir)) {
268+
foreach ($suite_names as $suite_name) {
269+
$suite_dir = $test_dir . '/' . $suite_name;
270+
if (is_dir($suite_dir)) {
271+
// Register the PSR-4 directory for PHPUnit-based suites.
272+
$this->namespaces['Drupal\\Tests\\' . $extension->getName() . '\\' . $suite_name . '\\'][] = $suite_dir;
273+
}
274+
}
275+
// Extensions can have a \Drupal\Tests\extension\Traits namespace for
276+
// cross-suite trait code.
277+
$trait_dir = $test_dir . '/Traits';
278+
if (is_dir($trait_dir)) {
279+
$this->namespaces['Drupal\\Tests\\' . $extension->getName() . '\\Traits\\'][] = $trait_dir;
280+
}
256281
}
257282
}
258283

src/Rules/Drupal/Tests/BrowserTestBaseDefaultThemeRule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function getNodeType(): string
1919
public function processNode(Node $node, Scope $scope): array
2020
{
2121
if (!interface_exists(\PHPUnit\Framework\Test::class)) {
22-
// PHPUnit is unavailable in the class loader.
2322
return [];
2423
}
2524
assert($node instanceof Node\Stmt\Class_);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
parameters:
2+
reportUnmatchedIgnoredErrors: false
3+
level: 0
4+
ignoreErrors:
5+
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
6+
excludePaths:
7+
- */tests/src/*/*.php
8+
includes:
9+
- vendor/mglaman/phpstan-drupal/extension.neon

tests/fixtures/drupal/sites/simpletest/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)