8
8
use Drupal \node \Entity \Node ;
9
9
use Drupal \node \NodeStorage ;
10
10
use mglaman \PHPStanDrupal \Drush \Commands \PhpstanDrupalDrushCommands ;
11
- use PHPUnit \ Framework \ TestCase ;
11
+ use PHPStan \ Testing \ PHPStanTestCase ;
12
12
use Symfony \Component \Console \Input \ArrayInput ;
13
13
use Symfony \Component \Console \Output \BufferedOutput ;
14
14
15
- final class PhpstanDrupalDrushCommandsTest extends TestCase
15
+ final class PhpstanDrupalDrushCommandsTest extends PHPStanTestCase
16
16
{
17
+ private static string $ tmpDir = '' ;
17
18
18
- public function testCreateNeon (): void
19
+ protected function setUp (): void
19
20
{
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
+ }
23
28
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
+ }
30
34
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 ();
36
38
37
39
$ command = new PhpstanDrupalDrushCommands ();
38
40
$ command ->setInput (new ArrayInput ([]));
@@ -55,4 +57,74 @@ class: Drupal\node\Entity\Node
55
57
' , $ output ->fetch ());
56
58
}
57
59
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
+ }
58
130
}
0 commit comments