Skip to content

Commit 80612b1

Browse files
committed
GetDeprecatedServiceRule and GlobalDrupalDependencyInjectionRule tests
1 parent 8fd4bd7 commit 80612b1

File tree

4 files changed

+156
-27
lines changed

4 files changed

+156
-27
lines changed

tests/src/DeprecationRulesTest.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
namespace mglaman\PHPStanDrupal\Tests;
44

5-
class DeprecationRulesTest extends AnalyzerTestBase
5+
use PHPStan\Testing\PHPStanTestCase;
6+
7+
class DeprecationRulesTest extends PHPStanTestCase
68
{
79

810
/**
911
* @dataProvider dataDeprecatedSamples
1012
*/
11-
public function testDeprecationRules(string $path, int $count, array $errorMessages): void
13+
public function testDeprecationRules(string $path, array $errorMessages): void
1214
{
1315
if (version_compare('9.0.0', \Drupal::VERSION) !== 1) {
1416
$this->markTestSkipped('Only tested on Drupal 8.x.x');
1517
}
1618
$errors = $this->runAnalyze($path);
17-
self::assertCount($count, $errors->getErrors(), var_export($errors, true));
1819
foreach ($errors->getErrors() as $key => $error) {
1920
self::assertEquals($errorMessages[$key], $error->getMessage());
2021
}
@@ -24,49 +25,41 @@ public function dataDeprecatedSamples(): \Generator
2425
{
2526
yield [
2627
__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/UsesDeprecatedUrlFunction.php',
27-
2,
2828
[
29-
'Call to deprecated method url() of class Drupal:
29+
['Call to deprecated method url() of class Drupal:
3030
in drupal:8.0.0 and is removed from drupal:9.0.0.
3131
Instead create a \Drupal\Core\Url object directly, for example using
32-
Url::fromRoute().',
33-
'\Drupal calls should be avoided in classes, use dependency injection instead',
32+
Url::fromRoute().', 2],
3433
]
3534
];
3635
yield [
3736
__DIR__ . '/../fixtures/drupal/core/lib/Drupal/Core/Entity/EntityManager.php',
38-
2,
3937
[
40-
'Class Drupal\Core\Entity\EntityManager implements deprecated interface Drupal\Core\Entity\EntityManagerInterface:
41-
in drupal:8.0.0 and is removed from drupal:9.0.0.',
42-
'Method Drupal\\Core\\Entity\\EntityManager::setFieldMap() should return $this(Drupal\\Core\\Entity\\EntityManager) but returns Drupal\\Core\\Entity\\EntityFieldManager.',
38+
['Class Drupal\Core\Entity\EntityManager implements deprecated interface Drupal\Core\Entity\EntityManagerInterface:
39+
in drupal:8.0.0 and is removed from drupal:9.0.0.', 2],
40+
['Method Drupal\\Core\\Entity\\EntityManager::setFieldMap() should return $this(Drupal\\Core\\Entity\\EntityManager) but returns Drupal\\Core\\Entity\\EntityFieldManager.', 2],
4341
]
4442
];
4543
yield [
4644
__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/DeprecatedGlobalConstants.php',
47-
2,
4845
[
49-
'Call to deprecated constant DATETIME_STORAGE_TIMEZONE: Deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::STORAGE_TIMEZONE instead.',
50-
'Call to deprecated constant DATETIME_DATE_STORAGE_FORMAT: Deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATE_STORAGE_FORMAT instead.',
46+
['Call to deprecated constant DATETIME_STORAGE_TIMEZONE: Deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::STORAGE_TIMEZONE instead.', 2],
47+
['Call to deprecated constant DATETIME_DATE_STORAGE_FORMAT: Deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATE_STORAGE_FORMAT instead.', 2],
5148
]
5249
];
5350
yield 'entity.manager ContainerInjectionInterface test' => [
5451
__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/Controller/EntityManagerInjectedController.php',
55-
2,
5652
[
57-
'Parameter $entity_manager of method Drupal\\phpstan_fixtures\\Controller\\EntityManagerInjectedController::__construct() has typehint with deprecated interface Drupal\\Core\\Entity\\EntityManagerInterface:
58-
in drupal:8.0.0 and is removed from drupal:9.0.0.',
59-
'The "entity.manager" service is deprecated. You should use the \'entity_type.manager\' service instead.'
53+
['Parameter $entity_manager of method Drupal\\phpstan_fixtures\\Controller\\EntityManagerInjectedController::__construct() has typehint with deprecated interface Drupal\\Core\\Entity\\EntityManagerInterface:
54+
in drupal:8.0.0 and is removed from drupal:9.0.0.', 2],
6055
]
6156
];
6257
yield 'entity.manager ContainerFactoryPluginInterface test' => [
6358
__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/Plugin/Block/EntityManagerInjectedBlock.php',
64-
3,
6559
[
66-
'Parameter $entity_manager of method Drupal\\phpstan_fixtures\\Plugin\\Block\\EntityManagerInjectedBlock::__construct() has typehint with deprecated interface Drupal\\Core\\Entity\\EntityManagerInterface:
67-
in drupal:8.0.0 and is removed from drupal:9.0.0.',
68-
'Unsafe usage of new static().',
69-
'The "entity.manager" service is deprecated. You should use the \'entity_type.manager\' service instead.'
60+
['Parameter $entity_manager of method Drupal\\phpstan_fixtures\\Plugin\\Block\\EntityManagerInjectedBlock::__construct() has typehint with deprecated interface Drupal\\Core\\Entity\\EntityManagerInterface:
61+
in drupal:8.0.0 and is removed from drupal:9.0.0.', 2],
62+
['Unsafe usage of new static().', 2],
7063
]
7164
];
7265
}

tests/src/DrupalIntegrationTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public function testExtensionReportsError(): void
5454
$error = array_shift($errors);
5555
self::assertEquals('Function phpstan_fixtures_MissingReturnRule() should return string but return statement is missing.', $error->getMessage());
5656
if ($is_d9) {
57-
$error = array_shift($errors);
58-
self::assertEquals('The "app.root" service is deprecated in drupal:9.0.0 and is removed from drupal:10.0.0. Use the app.root parameter instead. See https://www.drupal.org/node/3080612', $error->getMessage());
57+
array_shift($errors);
5958
$error = array_shift($errors);
6059
self::assertEquals('Binary operation "." between SplString and \'/core/includes…\' results in an error.', $error->getMessage());
6160
}
@@ -87,14 +86,12 @@ public function testServiceMapping8()
8786
}
8887
$errorMessages = [
8988
'The "entity.manager" service is deprecated. You should use the \'entity_type.manager\' service instead.',
90-
'\Drupal calls should be avoided in classes, use dependency injection instead',
9189
'Call to an undefined method Drupal\Core\Entity\EntityManager::thisMethodDoesNotExist().',
9290
'Call to deprecated method getDefinitions() of class Drupal\\Core\\Entity\\EntityManager:
9391
in drupal:8.0.0 and is removed from drupal:9.0.0.
9492
Use \\Drupal\\Core\\Entity\\EntityTypeManagerInterface::getDefinitions()
9593
instead.',
9694
'The "path.alias_manager" service is deprecated. Use "path_alias.manager" instead. See https://drupal.org/node/3092086',
97-
'\Drupal calls should be avoided in classes, use dependency injection instead',
9895
];
9996
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/TestServicesMappingExtension.php');
10097
self::assertCount(count($errorMessages), $errors->getErrors());
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Tests\Rules;
4+
5+
use mglaman\PHPStanDrupal\Drupal\ServiceMap;
6+
use mglaman\PHPStanDrupal\Rules\Deprecations\GetDeprecatedServiceRule;
7+
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
8+
9+
final class GetDeprecatedServiceRuleTest extends DrupalRuleTestCase {
10+
11+
protected function getRule(): \PHPStan\Rules\Rule
12+
{
13+
return new GetDeprecatedServiceRule(
14+
self::getContainer()->getByType(ServiceMap::class)
15+
);
16+
}
17+
18+
/**
19+
* @dataProvider drupal8Data
20+
*/
21+
public function testRuleDrupal8(string $path, array $errorMessages): void
22+
{
23+
if (version_compare('9.0.0', \Drupal::VERSION) !== 1) {
24+
self::markTestSkipped('Only tested on Drupal 8.x.x');
25+
}
26+
$this->analyse([$path], $errorMessages);
27+
}
28+
29+
/**
30+
* @dataProvider drupal9Data
31+
*/
32+
public function testRuleDrupal9(string $path, array $errorMessages): void
33+
{
34+
if (version_compare(\Drupal::VERSION, '9.0.0', 'gt') === 1) {
35+
self::markTestSkipped('Only tested on Drupal 9.x.x');
36+
}
37+
$this->analyse([$path], $errorMessages);
38+
}
39+
40+
public function drupal8Data(): \Generator
41+
{
42+
yield [
43+
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Plugin/Block/EntityManagerInjectedBlock.php',
44+
[
45+
[
46+
'The "entity.manager" service is deprecated. You should use the \'entity_type.manager\' service instead.',
47+
15
48+
],
49+
]
50+
];
51+
yield [
52+
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Controller/EntityManagerInjectedController.php',
53+
[
54+
[
55+
'The "entity.manager" service is deprecated. You should use the \'entity_type.manager\' service instead.',
56+
15
57+
],
58+
]
59+
];
60+
}
61+
62+
public function drupal9Data(): \Generator
63+
{
64+
yield [
65+
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/phpstan_fixtures.module',
66+
[
67+
[
68+
'The "app.root" service is deprecated in drupal:9.0.0 and is removed from drupal:10.0.0. Use the app.root parameter instead. See https://www.drupal.org/node/3080612',
69+
18
70+
],
71+
]
72+
];
73+
yield [
74+
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/AppRootParameter.php',
75+
[
76+
[
77+
'The "app.root" service is deprecated in drupal:9.0.0 and is removed from drupal:10.0.0. Use the app.root parameter instead. See https://www.drupal.org/node/3080612',
78+
17
79+
],
80+
]
81+
];
82+
}
83+
84+
85+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Tests\Rules;
4+
5+
use mglaman\PHPStanDrupal\Rules\Drupal\GlobalDrupalDependencyInjectionRule;
6+
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase;
7+
8+
final class GlobalDrupalDependencyInjectionRuleTest extends DrupalRuleTestCase {
9+
10+
protected function getRule(): \PHPStan\Rules\Rule
11+
{
12+
return new GlobalDrupalDependencyInjectionRule();
13+
}
14+
15+
/**
16+
* @dataProvider resultData
17+
*/
18+
public function testRule(string $path, array $errorMessages): void
19+
{
20+
$this->analyse([$path], $errorMessages);
21+
}
22+
23+
public function resultData(): \Generator
24+
{
25+
yield [
26+
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/UsesDeprecatedUrlFunction.php',
27+
[
28+
[
29+
'\Drupal calls should be avoided in classes, use dependency injection instead',
30+
07
31+
],
32+
]
33+
];
34+
yield [
35+
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/TestServicesMappingExtension.php',
36+
[
37+
[
38+
'\Drupal calls should be avoided in classes, use dependency injection instead',
39+
07
40+
],
41+
[
42+
'\Drupal calls should be avoided in classes, use dependency injection instead',
43+
13
44+
],
45+
]
46+
];
47+
yield [
48+
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Entity/ReflectionEntityTest.php',
49+
[],
50+
];
51+
}
52+
53+
54+
}

0 commit comments

Comments
 (0)