Skip to content

Commit 18f4368

Browse files
committed
Prepare to test entity integration
1 parent 683aa30 commit 18f4368

12 files changed

+183
-116
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: php
22
php:
3-
- 7.1
4-
- 7.2
5-
- 7.3
3+
- "7.1"
4+
- "7.2"
5+
- "7.3"
66

77
before_install:
88
- composer global require "hirak/prestissimo:^0.3"

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
}
3838
},
3939
"autoload-dev": {
40-
"classmap": ["tests/"]
40+
"classmap": ["tests/src"]
4141
},
4242
"extra": {
4343
"installer-paths": {

extension.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,3 @@ services:
3535
-
3636
class: PHPStan\Type\ServiceDynamicReturnTypeExtension
3737
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
38-
-
39-
class: PHPStan\Reflection\EntityFieldsViaMagicReflectionExtension
40-
tags: [phpstan.broker.propertiesClassReflectionExtension]

src/Drupal/Bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ protected function addCoreNamespaces(): void
164164
$this->namespaces['Drupal\\KernelTests'] = $core_tests_dir . '/KernelTests';
165165
$this->namespaces['Drupal\\FunctionalTests'] = $core_tests_dir . '/FunctionalTests';
166166
$this->namespaces['Drupal\\FunctionalJavascriptTests'] = $core_tests_dir . '/FunctionalJavascriptTests';
167+
$this->namespaces['Drupal\\Tests\\TestSuites'] = $this->drupalRoot . '/core/tests/TestSuites';
167168
}
168169
protected function addModuleNamespaces(): void
169170
{

tests/DrupalIntegrationTest.php

Lines changed: 0 additions & 109 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Drupal\phpstan_fixtures;
4+
5+
use Drupal\entity_test\Entity\EntityTest;
6+
7+
class EntityFieldFixture {
8+
public function testMagicalFanciness() {
9+
10+
/** @var EntityTest $testEntity */
11+
$testEntity = EntityTest::create([
12+
'name' => 'Llama',
13+
'type' => 'entity_test',
14+
]);
15+
16+
// 🤦‍♂
17+
$label1 = $testEntity->label();
18+
$label2 = $testEntity->get('name')->first()->value;
19+
$label3 = $testEntity->name->first()->value;
20+
$label4 = $testEntity->name->value;
21+
22+
}
23+
}

tests/src/AnalyzerTestBase.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPStan\Drupal;
4+
5+
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
6+
use PHPStan\Analyser\Analyser;
7+
use PHPStan\DependencyInjection\ContainerFactory;
8+
use PHPStan\File\FileHelper;
9+
use PHPUnit\Framework\TestCase;
10+
11+
abstract class AnalyzerTestBase extends TestCase {
12+
13+
protected function runAnalyze(string $path) {
14+
$rootDir = __DIR__ . '/../fixtures/drupal';
15+
$tmpDir = sys_get_temp_dir() . '/' . time() . 'phpstan';
16+
$containerFactory = new ContainerFactory($rootDir);
17+
$container = $containerFactory->create(
18+
$tmpDir,
19+
[__DIR__ . '/../fixtures/config/phpunit-drupal-phpstan.neon'],
20+
[]
21+
);
22+
$fileHelper = $container->getByType(FileHelper::class);
23+
assert($fileHelper !== null);
24+
25+
$bootstrapFile = $container->parameters['bootstrap'];
26+
$this->assertEquals(dirname(__DIR__, 2) . '/phpstan-bootstrap.php', $bootstrapFile);
27+
// Mock the autoloader.
28+
$GLOBALS['drupalVendorDir'] = dirname(__DIR__, 2) . '/vendor';
29+
if ($bootstrapFile !== null) {
30+
$bootstrapFile = $fileHelper->normalizePath($bootstrapFile);
31+
if (!is_file($bootstrapFile)) {
32+
$this->fail('Bootstrap file not found');
33+
}
34+
try {
35+
(static function (string $file): void {
36+
require_once $file;
37+
})($bootstrapFile);
38+
} catch (ContainerNotInitializedException $e) {
39+
$trace = $e->getTrace();
40+
$offending_file = $trace[1];
41+
$this->fail(sprintf('%s called the Drupal container from unscoped code.', $offending_file['file']));
42+
}
43+
catch (\Throwable $e) {
44+
$this->fail('Could not load the bootstrap file');
45+
}
46+
}
47+
48+
$analyser = $container->getByType(Analyser::class);
49+
assert($analyser !== null);
50+
51+
$file = $fileHelper->normalizePath($path);
52+
$errors = $analyser->analyse(
53+
[$file],
54+
false,
55+
null,
56+
null,
57+
true
58+
);
59+
foreach ($errors as $error) {
60+
$this->assertSame($fileHelper->normalizePath($file), $error->getFile());
61+
}
62+
return $errors;
63+
}
64+
65+
66+
}

tests/src/DeprecationRulesTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPStan\Drupal;
4+
5+
class DeprecationRulesTest extends AnalyzerTestBase
6+
{
7+
8+
/**
9+
* @dataProvider dataDeprecatedSamples
10+
*/
11+
public function testDeprecationRules(string $path, int $count, array $errorMessages)
12+
{
13+
$errors = $this->runAnalyze($path);
14+
$this->assertCount($count, $errors);
15+
foreach ($errors as $key => $error) {
16+
$this->assertEquals($errorMessages[$key], $error->getMessage());
17+
}
18+
}
19+
20+
public function dataDeprecatedSamples(): \Generator
21+
{
22+
yield [
23+
__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/UsesDeprecatedUrlFunction.php',
24+
2,
25+
[
26+
'\Drupal calls should be avoided in classes, use dependency injection instead',
27+
'Call to deprecated method url() of class Drupal.'
28+
]
29+
];
30+
yield [
31+
__DIR__ . '/../fixtures/drupal/core/lib/Drupal/Core/Entity/EntityManager.php',
32+
1,
33+
[
34+
'Class Drupal\Core\Entity\EntityManager implements deprecated interface Drupal\Core\Entity\EntityManagerInterface.'
35+
]
36+
];
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPStan\Drupal;
4+
5+
final class DrupalIntegrationTest extends AnalyzerTestBase {
6+
7+
public function testInstallPhp() {
8+
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/core/install.php');
9+
$this->assertCount(0, $errors);
10+
}
11+
12+
public function testTestSuiteAutoloading() {
13+
$paths = [
14+
__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/FunctionalJavascriptTestSuite.php',
15+
__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/FunctionalTestSuite.php',
16+
__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/KernelTestSuite.php',
17+
__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/TestSuiteBase.php',
18+
__DIR__ . '/../fixtures/drupal/core/tests/TestSuites/UnitTestSuite.php',
19+
];
20+
foreach ($paths as $path) {
21+
$errors = $this->runAnalyze($path);
22+
$this->assertCount(0, $errors, print_r($errors, true));
23+
}
24+
}
25+
26+
public function testDrupalTestInChildSiteContant() {
27+
$errors = $this->runAnalyze(__DIR__ . '/../fixtures/drupal/modules/phpstan_fixtures/src/DrupalTestInChildSiteContant.php');
28+
$this->assertCount(0, $errors);
29+
}
30+
31+
}

tests/src/DrushIntegrationTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPStan\Drupal;
4+
5+
final class DrushIntegrationTest extends AnalyzerTestBase
6+
{
7+
/**
8+
* @dataProvider dataPaths
9+
*/
10+
public function testPaths($path) {
11+
$errors = $this->runAnalyze($path);
12+
$this->assertCount(0, $errors, print_r($errors, true));
13+
}
14+
15+
public function dataPaths(): \Generator
16+
{
17+
yield [__DIR__ . '/../fixtures/drupal/modules/drush_command/src/Commands/TestDrushCommands.php'];
18+
}
19+
20+
}

0 commit comments

Comments
 (0)