Skip to content

Commit e71d285

Browse files
committed
Make PhpstanDrupalDrushCommandsTest verify written config
1 parent f285c02 commit e71d285

File tree

3 files changed

+94
-20
lines changed

3 files changed

+94
-20
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
includes:
2+
- ../../../extension.neon
3+
- ../../../rules.neon
4+
- ../../../vendor/phpstan/phpstan-deprecation-rules/rules.neon

tests/fixtures/config/phpunit-drupal-phpstan.neon

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ parameters:
1414
content_entity_using_default_storage:
1515
class: Drupal\phpstan_fixtures\Entity\ContentEntityUsingDefaultStorage
1616
includes:
17-
- ../../../extension.neon
18-
- ../../../rules.neon
19-
- ../../../vendor/phpstan/phpstan-deprecation-rules/rules.neon
17+
- phpstan-drupal-includes.neon
2018
# @todo remove after https://github.com/mglaman/phpstan-drupal/issues/399.
2119
conditionalTags:
2220
PhpParser\NodeVisitor\NodeConnectingVisitor:

tests/src/Drush/Commands/PhpstanDrupalDrushCommandsTest.php

Lines changed: 89 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,33 @@
88
use Drupal\node\Entity\Node;
99
use Drupal\node\NodeStorage;
1010
use mglaman\PHPStanDrupal\Drush\Commands\PhpstanDrupalDrushCommands;
11-
use PHPUnit\Framework\TestCase;
11+
use PHPStan\Testing\PHPStanTestCase;
1212
use Symfony\Component\Console\Input\ArrayInput;
1313
use Symfony\Component\Console\Output\BufferedOutput;
1414

15-
final class PhpstanDrupalDrushCommandsTest extends TestCase
15+
final class PhpstanDrupalDrushCommandsTest extends PHPStanTestCase
1616
{
17+
private static string $tmpDir = '';
1718

18-
public function testCreateNeon(): void
19+
protected function setUp(): void
1920
{
20-
$container = new ContainerBuilder();
21-
22-
$etmMock = $this->createStub(EntityTypeManagerInterface::class);
21+
parent::setUp();
22+
$tmpDir = \sys_get_temp_dir() . '/phpstan-drupal-tests';
23+
if (!@\mkdir($tmpDir, 0777) && !\is_dir($tmpDir)) {
24+
self::fail(\sprintf('Cannot create temp directory %s', $tmpDir));
25+
}
26+
self::$tmpDir = $tmpDir;
27+
}
2328

24-
$etdMock = $this->createStub(EntityTypeInterface::class);
25-
$etdMock->method('id')->willReturn('node');
26-
/** @phpstan-ignore-next-line */
27-
$etdMock->method('getClass')->willReturn(Node::class);
28-
/** @phpstan-ignore-next-line */
29-
$etdMock->method('getStorageClass')->willReturn(NodeStorage::class);
29+
protected function tearDown(): void
30+
{
31+
parent::tearDown();
32+
@unlink(self::$tmpDir . '/phpstan-drupal.neon');
33+
}
3034

31-
$etmMock->method('getDefinitions')->willReturn([
32-
'node' => $etdMock
33-
]);
34-
$container->set('entity_type.manager', $etmMock);
35-
\Drupal::setContainer($container);
35+
public function testCreateNeon(): void
36+
{
37+
$this->createDrupalContainer();
3638

3739
$command = new PhpstanDrupalDrushCommands();
3840
$command->setInput(new ArrayInput([]));
@@ -55,4 +57,74 @@ class: Drupal\node\Entity\Node
5557
', $output->fetch());
5658
}
5759

60+
public function testWriteNeon(): void
61+
{
62+
$configFile = self::$tmpDir . '/phpstan-drupal.neon';
63+
64+
$this->createDrupalContainer();
65+
66+
$command = new PhpstanDrupalDrushCommands();
67+
$command->setInput(new ArrayInput([]));
68+
$output = new BufferedOutput();
69+
$command->setOutput($output);
70+
71+
$command->setup([
72+
'drupal_root' => '',
73+
'file' => $configFile
74+
]);
75+
76+
self::assertFileExists($configFile);
77+
78+
// Build the container, which validates all parameters.
79+
try {
80+
self::getContainer();
81+
} catch (\Throwable $e) {
82+
if (strpos(get_class($e), 'Nette\Schema\ValidationException') !== FALSE) {
83+
self::fail('PHPStan config validation error: ' . $e->getMessage());
84+
} else {
85+
throw $e;
86+
}
87+
}
88+
89+
// Verify config after validation.
90+
self::assertEquals('parameters:
91+
level: 2
92+
paths:
93+
- tests/fixtures/drupal/modules/custom
94+
- tests/fixtures/drupal/themes/custom
95+
- tests/fixtures/drupal/profiles/custom
96+
drupal:
97+
drupal_root: tests/fixtures/drupal
98+
entityMapping:
99+
node:
100+
class: Drupal\node\Entity\Node
101+
storage: Drupal\node\NodeStorage', file_get_contents($configFile));
102+
}
103+
104+
public static function getAdditionalConfigFiles(): array
105+
{
106+
return array_merge(parent::getAdditionalConfigFiles(), [
107+
__DIR__ . '/../../../fixtures/config/phpstan-drupal-includes.neon',
108+
self::$tmpDir . '/phpstan-drupal.neon',
109+
]);
110+
}
111+
112+
private function createDrupalContainer(): void
113+
{
114+
$container = new ContainerBuilder();
115+
116+
$etdMock = $this->createStub(EntityTypeInterface::class);
117+
$etdMock->method('id')->willReturn('node');
118+
/** @phpstan-ignore-next-line */
119+
$etdMock->method('getClass')->willReturn(Node::class);
120+
/** @phpstan-ignore-next-line */
121+
$etdMock->method('getStorageClass')->willReturn(NodeStorage::class);
122+
123+
$etmMock = $this->createStub(EntityTypeManagerInterface::class);
124+
$etmMock->method('getDefinitions')->willReturn([
125+
'node' => $etdMock
126+
]);
127+
$container->set('entity_type.manager', $etmMock);
128+
\Drupal::setContainer($container);
129+
}
58130
}

0 commit comments

Comments
 (0)