Skip to content

Commit c72367d

Browse files
committed
Fix changes to analyzer and error handling
1 parent f3319ab commit c72367d

File tree

9 files changed

+46
-28
lines changed

9 files changed

+46
-28
lines changed

src/Drupal/DrupalAutoloader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ 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';
103104

104105
foreach ($this->moduleData as $extension) {
105106
$this->loadExtension($extension);
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
parameters:
22
reportUnmatchedIgnoredErrors: false
33
level: 2
4-
# Ignore functions for the PHPUnit bootstrap process.
5-
ignoreErrors:
6-
- '#Function drupal_phpunit_[a-zA-Z0-9\\_]+ not found#'
7-
- '#Unsafe usage of new static().#'
84
includes:
95
- ../../../extension.neon
106
- ../../../vendor/phpstan/phpstan-deprecation-rules/rules.neon

tests/fixtures/drupal/sites/.gitkeep

Whitespace-only changes.

tests/src/AnalyzerTestBase.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
66
use PHPStan\Analyser\Analyser;
7+
use PHPStan\Analyser\AnalyserResult;
78
use PHPStan\DependencyInjection\ContainerFactory;
89
use PHPStan\File\FileHelper;
910
use PHPUnit\Framework\TestCase;
@@ -43,12 +44,13 @@ protected function runAnalyze(string $path) {
4344
$file = $fileHelper->normalizePath($path);
4445
$errors = $analyser->analyse(
4546
[$file],
46-
false,
4747
null,
4848
null,
49-
true
49+
false,
50+
null
5051
);
51-
foreach ($errors as $error) {
52+
assert($errors instanceof AnalyserResult);
53+
foreach ($errors->getErrors() as $error) {
5254
$this->assertSame($fileHelper->normalizePath($file), $error->getFile());
5355
}
5456
return $errors;

tests/src/DeprecationRulesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class DeprecationRulesTest extends AnalyzerTestBase
1111
public function testDeprecationRules(string $path, int $count, array $errorMessages)
1212
{
1313
$errors = $this->runAnalyze($path);
14-
$this->assertCount($count, $errors, var_export($errors, true));
15-
foreach ($errors as $key => $error) {
14+
$this->assertCount($count, $errors->getErrors(), var_export($errors, true));
15+
foreach ($errors->getErrors() as $key => $error) {
1616
$this->assertEquals($errorMessages[$key], $error->getMessage());
1717
}
1818
}
@@ -23,11 +23,11 @@ public function dataDeprecatedSamples(): \Generator
2323
__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/UsesDeprecatedUrlFunction.php',
2424
2,
2525
[
26-
'\Drupal calls should be avoided in classes, use dependency injection instead',
2726
'Call to deprecated method url() of class Drupal:
2827
in drupal:8.0.0 and is removed from drupal:9.0.0.
2928
Instead create a \Drupal\Core\Url object directly, for example using
30-
Url::fromRoute().'
29+
Url::fromRoute().',
30+
'\Drupal calls should be avoided in classes, use dependency injection instead',
3131
]
3232
];
3333
yield [

tests/src/DrupalIntegrationTest.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,42 @@ final class DrupalIntegrationTest extends AnalyzerTestBase {
66

77
public function testInstallPhp() {
88
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/core/install.php');
9-
$this->assertCount(0, $errors);
9+
$this->assertCount(0, $errors->getErrors());
10+
$this->assertCount(0, $errors->getInternalErrors());
1011
}
1112

1213
public function testTestSuiteAutoloading() {
1314
$paths = [
1415
__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/FunctionalJavascriptTestSuite.php',
1516
__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/FunctionalTestSuite.php',
1617
__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/KernelTestSuite.php',
17-
__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/TestSuiteBase.php',
1818
__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/UnitTestSuite.php',
1919
];
2020
foreach ($paths as $path) {
2121
$errors = $this->runAnalyze($path);
22-
$this->assertCount(0, $errors, print_r($errors, true));
22+
$this->assertCount(1, $errors->getErrors(), $path);
23+
$this->assertEquals('Unsafe usage of new static().', $errors->getErrors()[0]->getMessage());
24+
$this->assertCount(0, $errors->getInternalErrors(), print_r($errors->getInternalErrors(), true));
2325
}
26+
27+
// Abstract doesn't warn on static constructor.
28+
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/TestSuiteBase.php');
29+
$this->assertCount(0, $errors->getErrors(), $path);
30+
$this->assertCount(0, $errors->getInternalErrors(), print_r($errors->getInternalErrors(), true));
2431
}
2532

2633
public function testDrupalTestInChildSiteContant() {
2734
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/DrupalTestInChildSiteContant.php');
28-
$this->assertCount(0, $errors);
35+
$this->assertCount(0, $errors->getErrors());
36+
$this->assertCount(0, $errors->getInternalErrors());
2937
}
3038

3139
public function testExtensionReportsError() {
3240
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/phpstan_fixtures.module');
33-
$this->assertCount(2, $errors, var_export($errors, true));
41+
$this->assertCount(2, $errors->getErrors(), var_export($errors, true));
42+
$this->assertCount(0, $errors->getInternalErrors(), var_export($errors, true));
43+
44+
$errors = $errors->getErrors();
3445
$error = array_shift($errors);
3546
$this->assertEquals('If condition is always false.', $error->getMessage());
3647
$error = array_shift($errors);
@@ -45,7 +56,8 @@ public function testExtensionTestSuiteAutoloading() {
4556
];
4657
foreach ($paths as $path) {
4758
$errors = $this->runAnalyze($path);
48-
$this->assertCount(0, $errors, print_r($errors, true));
59+
$this->assertCount(0, $errors->getErrors(), print_r($errors, true));
60+
$this->assertCount(0, $errors->getInternalErrors(), print_r($errors, true));
4961
}
5062
}
5163

@@ -55,20 +67,22 @@ public function testServiceMapping() {
5567
'Call to an undefined method Drupal\Core\Entity\EntityManager::thisMethodDoesNotExist().',
5668
];
5769
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/TestServicesMappingExtension.php');
58-
$this->assertCount(2, $errors);
59-
foreach ($errors as $key => $error) {
70+
$this->assertCount(2, $errors->getErrors());
71+
$this->assertCount(0, $errors->getInternalErrors());
72+
foreach ($errors->getErrors() as $key => $error) {
6073
$this->assertEquals($errorMessages[$key], $error->getMessage());
6174
}
6275
}
6376

6477
public function testAppRootPseudoService() {
65-
$errorMessages = [];
6678
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/AppRootParameter.php');
67-
$this->assertCount(0, $errors, var_export($errors, TRUE));
79+
$this->assertCount(0, $errors->getErrors(), var_export($errors, TRUE));
80+
$this->assertCount(0, $errors->getInternalErrors(), var_export($errors, TRUE));
6881
}
6982

7083
public function testThemeSettingsFile() {
7184
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/core/modules/system/tests/themes/test_theme_settings/theme-settings.php');
72-
$this->assertCount(0, $errors, var_export($errors, TRUE));
85+
$this->assertCount(0, $errors->getErrors(), var_export($errors, TRUE));
86+
$this->assertCount(0, $errors->getInternalErrors(), var_export($errors, TRUE));
7387
}
7488
}

tests/src/DrushIntegrationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ final class DrushIntegrationTest extends AnalyzerTestBase
99
*/
1010
public function testPaths($path) {
1111
$errors = $this->runAnalyze($path);
12-
$this->assertCount(0, $errors, print_r($errors, true));
12+
$this->assertCount(0, $errors->getErrors(), var_export($errors, TRUE));
13+
$this->assertCount(0, $errors->getInternalErrors(), var_export($errors, TRUE));
1314
}
1415

1516
public function dataPaths(): \Generator

tests/src/EntityTestClass.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ final class EntityTestClass extends AnalyzerTestBase
1010
public function testEntityFields(string $path, int $count, array $errorMessages): void
1111
{
1212
$errors = $this->runAnalyze($path);
13-
$this->assertCount($count, $errors, print_r($errors, true));
14-
foreach ($errors as $key => $error) {
13+
$this->assertCount($count, $errors->getErrors(), var_export($errors, TRUE));
14+
$this->assertCount(0, $errors->getInternalErrors(), var_export($errors, TRUE));
15+
foreach ($errors->getErrors() as $key => $error) {
1516
$this->assertEquals($errorMessages[$key], $error->getMessage());
1617
}
1718
}
1819

1920
public function testEntityReferenceTargetIdPropertyReflection(): void
2021
{
2122
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/Entity/ReflectionEntityTest.php');
22-
$this->assertCount(2, $errors, print_r($errors, true));
23+
$this->assertCount(2, $errors->getErrors(), var_export($errors, TRUE));
24+
$this->assertCount(0, $errors->getInternalErrors(), var_export($errors, TRUE));
25+
$errors = $errors->getErrors();
2326
$error = array_shift($errors);
2427
$this->assertEquals(
2528
'Method Drupal\phpstan_fixtures\Entity\ReflectionEntityTest::getOwnerId() should return int but returns string.',

tests/src/ServiceProviderAutoloadingTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ final class ServiceProviderAutoloadingTest extends AnalyzerTestBase
99
*/
1010
public function testLoadingServiceProvider(string $path, int $count, array $errorMessages) {
1111
$errors = $this->runAnalyze($path);
12-
$this->assertCount($count, $errors, print_r($errors, true));
13-
foreach ($errors as $key => $error) {
12+
$this->assertCount($count, $errors->getErrors(), print_r($errors, true));
13+
$this->assertCount(0, $errors->getInternalErrors(), print_r($errors, true));
14+
foreach ($errors->getErrors() as $key => $error) {
1415
$this->assertEquals($errorMessages[$key], $error->getMessage());
1516
}
1617
}

0 commit comments

Comments
 (0)