Skip to content

Commit cec4fdd

Browse files
authored
Merge pull request #175 from mglaman/prevent-crash-if-no-phpunit
Check getInterfaceNames over implementsInterface
2 parents e6f5cab + fab76ba commit cec4fdd

File tree

5 files changed

+89
-12
lines changed

5 files changed

+89
-12
lines changed

.github/workflows/php.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,47 @@ jobs:
114114
cd ~/drupal
115115
COMPOSER_MEMORY_LIMIT=-1 composer require drupal/token drupal/blazy
116116
./vendor/bin/phpstan analyze web/modules/contrib/blazy --no-progress || if (($? == 255)); then false; else true; fi
117+
build_integration_no_phpunit:
118+
continue-on-error: ${{ matrix.experimental }}
119+
runs-on: "ubuntu-latest"
120+
name: "Build Integration (No PHPUnit) | PHP ${{ matrix.php-version }} | Drupal ${{ matrix.drupal }}"
121+
strategy:
122+
matrix:
123+
experimental: [false]
124+
php-version:
125+
- "7.4"
126+
drupal:
127+
- "^8.9"
128+
- "^9.0"
129+
steps:
130+
- name: "Checkout"
131+
uses: "actions/checkout@v2"
132+
- name: "Install PHP"
133+
uses: "shivammathur/setup-php@v2"
134+
with:
135+
coverage: "none"
136+
php-version: "${{ matrix.php-version }}"
137+
tools: composer:v2
138+
extensions: dom, curl, libxml, mbstring, zip, pdo, mysql, pdo_mysql, bcmath, gd, exif, iconv
139+
- name: Setup Drupal
140+
uses: bluehorndigital/[email protected]
141+
with:
142+
version: ${{ matrix.drupal }}
143+
path: ~/drupal
144+
- name: "Remove PHPUnit"
145+
run: |
146+
cd ~/drupal
147+
composer --dev remove phpspec/prophecy-phpunit drupal/core-dev
148+
- name: "require phpstan-drupal"
149+
run: |
150+
cd ~/drupal
151+
COMPOSER_MEMORY_LIMIT=-1 composer require mglaman/phpstan-drupal *@dev
152+
cp $GITHUB_WORKSPACE/tests/fixtures/config/drupal-phpstan.neon phpstan.neon
153+
- name: "Test core/install.php"
154+
run: |
155+
cd ~/drupal
156+
./vendor/bin/phpstan analyze web/core/install.php --debug
157+
- name: "Test no crash"
158+
run: |
159+
cd ~/drupal
160+
./vendor/bin/phpstan analyze web/core/modules/dynamic_page_cache --debug || if (($? == 255)); then false; else true; fi

src/Drupal/DrupalAutoloader.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,18 @@ public function register(Container $container): void
100100
$this->addThemeNamespaces();
101101
$this->registerPs4Namespaces($this->namespaces);
102102
$this->loadLegacyIncludes();
103-
require_once $this->drupalRoot . '/core/tests/bootstrap.php';
104-
105-
// class_alias is not supported by OptimizedDirectorySourceLocator or AutoloadSourceLocator,
106-
// so we manually load this PHPUnit compatibility trait that exists in Drupal 8.
107-
$phpunitCompatTraitFilepath = $this->drupalRoot . '/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php';
108-
if (file_exists($phpunitCompatTraitFilepath)) {
109-
require_once $phpunitCompatTraitFilepath;
110-
$this->autoloader->addClassMap(['Drupal\\Tests\\PhpunitCompatibilityTrait' => $phpunitCompatTraitFilepath]);
103+
104+
// @todo stop requiring the bootstrap.php and just copy what is needed.
105+
if (interface_exists(\PHPUnit\Framework\Test::class)) {
106+
require_once $this->drupalRoot . '/core/tests/bootstrap.php';
107+
108+
// class_alias is not supported by OptimizedDirectorySourceLocator or AutoloadSourceLocator,
109+
// so we manually load this PHPUnit compatibility trait that exists in Drupal 8.
110+
$phpunitCompatTraitFilepath = $this->drupalRoot . '/core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php';
111+
if (file_exists($phpunitCompatTraitFilepath)) {
112+
require_once $phpunitCompatTraitFilepath;
113+
$this->autoloader->addClassMap(['Drupal\\Tests\\PhpunitCompatibilityTrait' => $phpunitCompatTraitFilepath]);
114+
}
111115
}
112116

113117
foreach ($this->moduleData as $extension) {

src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public function processNode(Node $node, Scope $scope): array
3232
throw new ShouldNotHappenException();
3333
}
3434

35-
$whitelist = [
35+
$classReflection = $scopeClassReflection->getNativeReflection();
36+
$allowed_list = [
3637
// Ignore tests.
3738
'PHPUnit\Framework\Test',
3839
// Typed data objects cannot use dependency injection.
@@ -45,9 +46,10 @@ public function processNode(Node $node, Scope $scope): array
4546
// @see https://www.drupal.org/project/drupal/issues/2913224
4647
'Drupal\Core\Entity\EntityInterface',
4748
];
48-
$classReflection = $scopeClassReflection->getNativeReflection();
49-
foreach ($whitelist as $item) {
50-
if ($classReflection->implementsInterface($item)) {
49+
$implemented_interfaces = $classReflection->getInterfaceNames();
50+
51+
foreach ($allowed_list as $item) {
52+
if (in_array($item, $implemented_interfaces, true)) {
5153
return [];
5254
}
5355
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Drupal\Tests\module_with_tests\Kernel;
4+
5+
use Drupal;
6+
use Drupal\KernelTests\KernelTestBase;
7+
use Drupal\Tests\module_with_tests\Traits\ModuleWithTestsTrait;
8+
9+
/**
10+
* This is a dummy test class.
11+
*
12+
* @group module_with_tests
13+
*/
14+
class DrupalStaticCallTest extends KernelTestBase {
15+
16+
use ModuleWithTestsTrait;
17+
18+
/**
19+
* A dummy test.
20+
*/
21+
public function testModule() {
22+
$request = Drupal::hasRequest();
23+
$this->assertFalse($request);
24+
}
25+
26+
}

tests/src/DrupalIntegrationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function testExtensionTestSuiteAutoloading(): void
6060
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/Unit/ModuleWithTestsTest.php',
6161
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/Traits/ModuleWithTestsTrait.php',
6262
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/TestSite/ModuleWithTestsTestSite.php',
63+
__DIR__ . '/../fixtures/drupal/modules/module_with_tests/tests/src/Kernel/DrupalStaticCallTest.php',
6364
];
6465
foreach ($paths as $path) {
6566
$errors = $this->runAnalyze($path);

0 commit comments

Comments
 (0)