Skip to content

Commit 8fd4bd7

Browse files
committed
Refactor tests onto PHPStan test base classes
All the tests are broken
1 parent 8221c5f commit 8fd4bd7

9 files changed

+95
-81
lines changed

src/Drupal/DrupalAutoloader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class DrupalAutoloader
5858
public function register(Container $container): void
5959
{
6060
$drupalParams = $container->getParameter('drupal');
61-
$drupalRoot = $drupalParams['drupal_root'];
61+
$drupalRoot = realpath($drupalParams['drupal_root']);
6262
$finder = new DrupalFinder();
6363
$finder->locateRoot($drupalRoot);
6464

@@ -284,7 +284,7 @@ protected function loadAndCatchErrors(string $path): void
284284
require_once $path;
285285
} catch (ContainerNotInitializedException $e) {
286286
$path = str_replace(dirname($this->drupalRoot) . '/', '', $path);
287-
// This can happen when drupal_get_path or drupal_get_filename are used outside of the scope of a function.
287+
// This can happen when drupal_get_path or drupal_get_filename are used outside the scope of a function.
288288
@trigger_error("$path invoked the Drupal container outside of the scope of a function or class method. It was not loaded.", E_USER_WARNING);
289289
} catch (\Throwable $e) {
290290
$path = str_replace(dirname($this->drupalRoot) . '/', '', $path);

src/Rules/Drupal/Tests/BrowserTestBaseDefaultThemeRule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Rules\Rule;
8+
use PHPStan\Rules\RuleErrorBuilder;
89
use PHPStan\Type\ObjectType;
910

1011
final class BrowserTestBaseDefaultThemeRule implements Rule
@@ -39,7 +40,8 @@ public function processNode(Node $node, Scope $scope): array
3940

4041
if ($defaultTheme === null || $defaultTheme === '') {
4142
return [
42-
'Drupal\Tests\BrowserTestBase::$defaultTheme is required. See https://www.drupal.org/node/3083055, which includes recommendations on which theme to use.',
43+
RuleErrorBuilder::message('Drupal\Tests\BrowserTestBase::$defaultTheme is required. See https://www.drupal.org/node/3083055, which includes recommendations on which theme to use.')
44+
->line($node->getLine())->build(),
4345
];
4446
}
4547
return [];
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
parameters:
2-
reportUnmatchedIgnoredErrors: false
3-
level: 2
2+
drupal:
3+
drupal_root: tests/fixtures/drupal
44
includes:
55
- ../../../extension.neon
66
- ../../../vendor/phpstan/phpstan-deprecation-rules/rules.neon

tests/src/BootstrapTest.php

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22

33
namespace mglaman\PHPStanDrupal\Tests;
44

5-
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
6-
use PHPStan\DependencyInjection\ContainerFactory;
7-
use PHPStan\File\FileHelper;
8-
use PHPUnit\Framework\TestCase;
5+
use PHPStan\Testing\PHPStanTestCase;
96

10-
final class BootstrapTest extends TestCase
7+
final class BootstrapTest extends PHPStanTestCase
118
{
129
private $previousErrorHandler;
1310
private $gatheredWarnings = [];
1411

15-
public function testContainerNotInitializedExceptionCatch() {
12+
public function testContainerNotInitializedExceptionCatch(): void
13+
{
1614
$this->previousErrorHandler = set_error_handler([$this, 'handleError']);
17-
$this->doDrupalBootstrap();
15+
self::getContainer();
1816
restore_error_handler();
1917

20-
$this->assertNotEmpty($this->gatheredWarnings);
18+
self::assertNotEmpty($this->gatheredWarnings);
2119
$expectedWarnings = [
2220
'drupal/modules/contained_not_initialized/contained_not_initialized.install invoked the Drupal container outside of the scope of a function or class method. It was not loaded.',
2321
'drupal/modules/contained_not_initialized/contained_not_initialized.post_update.php invoked the Drupal container outside of the scope of a function or class method. It was not loaded.',
@@ -27,47 +25,27 @@ public function testContainerNotInitializedExceptionCatch() {
2725
'drupal/modules/contained_not_initialized/contained_not_initialized.search_api.inc invoked the Drupal container outside of the scope of a function or class method. It was not loaded.',
2826
'drupal/modules/contained_not_initialized/contained_not_initialized.pathauto.inc invoked the Drupal container outside of the scope of a function or class method. It was not loaded.',
2927
];
30-
$this->assertEquals($expectedWarnings, $this->gatheredWarnings);
28+
self::assertEquals($expectedWarnings, $this->gatheredWarnings);
3129
}
3230

33-
public function handleError($type, $msg, $file, $line, $context = array()): void
31+
public function handleError(int $type, string $msg, string $file, int $line, array $context = []): bool
3432
{
3533
if (E_USER_WARNING !== $type) {
3634
$h = $this->previousErrorHandler;
3735
if (\is_callable($h)) {
38-
$h($type, $msg, $file, $line, $context);
36+
return $h($type, $msg, $file, $line, $context);
3937
}
40-
} else {
41-
$this->gatheredWarnings[] = $msg;
38+
return true;
4239
}
40+
$this->gatheredWarnings[] = $msg;
41+
return false;
4342
}
4443

45-
private function doDrupalBootstrap()
44+
public static function getAdditionalConfigFiles(): array
4645
{
47-
$rootDir = __DIR__ . '/../fixtures/drupal';
48-
$tmpDir = sys_get_temp_dir() . '/' . time() . 'phpstan';
49-
$containerFactory = new ContainerFactory($rootDir);
50-
$additionalConfigFiles = [
51-
\sprintf('%s/config.level%s.neon', $containerFactory->getConfigDirectory(), 4),
46+
return array_merge(parent::getAdditionalConfigFiles(), [
5247
__DIR__ . '/../fixtures/config/phpunit-drupal-phpstan.neon',
53-
];
54-
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, []);
55-
$fileHelper = $container->getByType(FileHelper::class);
56-
assert($fileHelper !== null);
57-
58-
$autoloadFiles = $container->getParameter('bootstrapFiles');
59-
$this->assertContains(dirname(__DIR__, 2) . '/drupal-autoloader.php', $autoloadFiles);
60-
if ($autoloadFiles !== null) {
61-
foreach ($autoloadFiles as $autoloadFile) {
62-
$autoloadFile = $fileHelper->normalizePath($autoloadFile);
63-
if (!is_file($autoloadFile)) {
64-
$this->fail('Autoload file not found');
65-
}
66-
(static function (string $file) use ($container): void {
67-
require_once $file;
68-
})($autoloadFile);
69-
}
70-
}
48+
]);
7149
}
7250

7351
}

tests/src/DrupalIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testTestSuiteAutoloading() {
2424
$suiteName = basename($path, '.php');
2525
$errors = $this->runAnalyze($path);
2626
self::assertCount(1, $errors->getErrors(), $path);
27-
self::assertEquals("Method Drupal\Tests\TestSuites\{$suiteName}::suite() should return static(Drupal\Tests\TestSuites\{$suiteName}) but return statement is missing.", $errors->getErrors()[0]->getMessage());
27+
self::assertEquals("Method Drupal\Tests\TestSuites\\$suiteName::suite() should return static(Drupal\Tests\TestSuites\\$suiteName) but return statement is missing.", $errors->getErrors()[0]->getMessage());
2828
self::assertCount(0, $errors->getInternalErrors(), print_r($errors->getInternalErrors(), true));
2929
}
3030

tests/src/DrupalRuleTestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Tests;
4+
5+
use PHPStan\Testing\RuleTestCase;
6+
7+
abstract class DrupalRuleTestCase extends RuleTestCase {
8+
9+
public static function getAdditionalConfigFiles(): array
10+
{
11+
return array_merge(parent::getAdditionalConfigFiles(), [
12+
__DIR__ . '/../fixtures/config/phpunit-drupal-phpstan.neon',
13+
]);
14+
}
15+
16+
}

tests/src/Rules/BrowserTestBaseDefaultThemeRuleTest.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,49 @@
22

33
namespace mglaman\PHPStanDrupal\Tests\Rules;
44

5-
use mglaman\PHPStanDrupal\Tests\AnalyzerTestBase;
5+
use mglaman\PHPStanDrupal\Rules\Drupal\Tests\BrowserTestBaseDefaultThemeRule;
6+
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
67

7-
final class BrowserTestBaseDefaultThemeRuleTest extends AnalyzerTestBase {
8+
/**
9+
* @extends \mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase<\mglaman\PHPStanDrupal\Rules\Drupal\Tests\BrowserTestBaseDefaultThemeRule>
10+
*/
11+
final class BrowserTestBaseDefaultThemeRuleTest extends DrupalRuleTestCase {
12+
13+
protected function getRule(): \PHPStan\Rules\Rule
14+
{
15+
return new BrowserTestBaseDefaultThemeRule();
16+
}
817

918
/**
1019
* @dataProvider fileData
1120
*/
12-
public function testRule(string $path, int $count, array $errorMessages): void
21+
public function testRule(string $path, array $errorMessages): void
1322
{
14-
$errors = $this->runAnalyze($path);
15-
self::assertCount($count, $errors->getErrors(), var_export($errors, true));
16-
foreach ($errors->getErrors() as $key => $error) {
17-
self::assertEquals($errorMessages[$key], $error->getMessage());
18-
}
23+
$this->analyse(
24+
[$path],
25+
$errorMessages
26+
);
1927
}
2028

2129
public function fileData(): \Generator
2230
{
2331
yield [
2432
__DIR__ . '/../../fixtures/drupal/modules/module_with_tests/tests/src/Functional/MissingDefaultThemeTest.php',
25-
1,
2633
[
27-
'Drupal\Tests\BrowserTestBase::$defaultTheme is required. See https://www.drupal.org/node/3083055, which includes recommendations on which theme to use.',
34+
[
35+
'Drupal\Tests\BrowserTestBase::$defaultTheme is required. See https://www.drupal.org/node/3083055, which includes recommendations on which theme to use.',
36+
12
37+
],
2838
]
2939
];
3040
yield [
3141
__DIR__ . '/../../fixtures/drupal/modules/module_with_tests/tests/src/Functional/DefaultThemeTest.php',
32-
0,
3342
[]
3443
];
3544
yield [
3645
__DIR__ . '/../../fixtures/drupal/modules/module_with_tests/tests/src/Functional/ExtendsDefaultThemeTest.php',
37-
0,
3846
[]
3947
];
4048
}
4149

42-
4350
}

tests/src/Rules/ConfigEntityConfigExportRuleTest.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@
22

33
namespace mglaman\PHPStanDrupal\Tests\Rules;
44

5-
use mglaman\PHPStanDrupal\Tests\AnalyzerTestBase;
5+
use mglaman\PHPStanDrupal\Rules\Deprecations\ConfigEntityConfigExportRule;
6+
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
67

7-
final class ConfigEntityConfigExportRuleTest extends AnalyzerTestBase {
8+
final class ConfigEntityConfigExportRuleTest extends DrupalRuleTestCase {
9+
10+
protected function getRule(): \PHPStan\Rules\Rule
11+
{
12+
return new ConfigEntityConfigExportRule(
13+
$this->createReflectionProvider()
14+
);
15+
}
816

917
/**
1018
* @dataProvider pluginData
1119
*/
12-
public function testConfigExportRuleCheck(string $path, int $count, array $errorMessages): void
20+
public function testConfigExportRuleCheck(string $path, array $errorMessages): void
1321
{
14-
$errors = $this->runAnalyze($path);
15-
self::assertCount($count, $errors->getErrors(), var_export($errors, true));
16-
foreach ($errors->getErrors() as $key => $error) {
17-
self::assertEquals($errorMessages[$key], $error->getMessage());
18-
}
22+
$this->analyse([$path], $errorMessages);
1923
}
2024

2125
public function pluginData(): \Generator
2226
{
2327
yield [
2428
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Entity/ConfigWithoutExport.php',
25-
1,
2629
[
27-
'Configuration entity must define a `config_export` key. See https://www.drupal.org/node/2481909',
30+
[
31+
'Configuration entity must define a `config_export` key. See https://www.drupal.org/node/2481909',
32+
15
33+
],
2834
]
2935
];
3036
yield [
3137
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Entity/ConfigWithExport.php',
32-
0,
3338
[]
3439
];
3540
}

tests/src/Rules/PluginAnnotationContextDefinitionsRuleTest.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,52 @@
22

33
namespace mglaman\PHPStanDrupal\Tests\Rules;
44

5-
use mglaman\PHPStanDrupal\Tests\AnalyzerTestBase;
5+
use mglaman\PHPStanDrupal\Rules\Deprecations\PluginAnnotationContextDefinitionsRule;
6+
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
67

7-
final class PluginAnnotationContextDefinitionsRuleTest extends AnalyzerTestBase {
8+
final class PluginAnnotationContextDefinitionsRuleTest extends DrupalRuleTestCase {
9+
10+
protected function getRule(): \PHPStan\Rules\Rule
11+
{
12+
return new PluginAnnotationContextDefinitionsRule(
13+
$this->createReflectionProvider()
14+
);
15+
}
816

917
/**
1018
* @dataProvider pluginData
1119
*/
12-
public function testContextAnnotationRuleCheck(string $path, int $count, array $errorMessages): void
20+
public function testContextAnnotationRuleCheck(string $path, array $errorMessages): void
1321
{
14-
$errors = $this->runAnalyze($path);
15-
self::assertCount($count, $errors->getErrors(), var_export($errors, true));
16-
foreach ($errors->getErrors() as $key => $error) {
17-
self::assertEquals($errorMessages[$key], $error->getMessage());
18-
}
22+
$this->analyse([$path] , $errorMessages);
1923
}
2024

2125
public function pluginData(): \Generator
2226
{
2327
yield [
2428
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Plugin/Condition/ConditionWithContext.php',
25-
1,
2629
[
27-
'Providing context definitions via the "context" key is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use the "context_definitions" key instead.',
30+
[
31+
'Providing context definitions via the "context" key is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use the "context_definitions" key instead.',
32+
17
33+
],
2834
]
2935
];
3036
yield [
3137
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Plugin/Condition/ConditionWithContextDefinitions.php',
32-
0,
3338
[]
3439
];
3540
yield [
3641
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Plugin/Action/ActionSample.php',
37-
0,
3842
[]
3943
];
4044
yield [
4145
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Plugin/Block/BlockWithContext.php',
42-
1,
4346
[
44-
'Providing context definitions via the "context" key is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use the "context_definitions" key instead.',
47+
[
48+
'Providing context definitions via the "context" key is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use the "context_definitions" key instead.',
49+
20
50+
],
4551
]
4652
];
4753
}

0 commit comments

Comments
 (0)