Skip to content

Commit c0897b7

Browse files
Merge pull request #19697 from MauricioFauth/twig-lint-command-test-fix
Fix TwigLintCommandTest failing test
2 parents 80c5cf7 + 65268af commit c0897b7

6 files changed

Lines changed: 25 additions & 83 deletions

File tree

libraries/classes/Command/TwigLintCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ protected function configure(): void
7373
->addOption('show-deprecations', null, InputOption::VALUE_NONE, 'Show deprecations as errors');
7474
}
7575

76+
/** @return string[] */
7677
protected function findFiles(string $baseFolder): array
7778
{
7879
/* Open the handle */
@@ -141,7 +142,8 @@ static function (int $level, string $message, string $file, int $line) use (&$pr
141142
return $this->display($output, $io, $filesInfo);
142143
}
143144

144-
protected function getFilesInfo(string $templatesPath): array
145+
/** @return array{template: string, file: string, valid: bool, line?: int, exception?: Error}[] */
146+
public function getFilesInfo(string $templatesPath): array
145147
{
146148
$filesInfo = [];
147149
$filesFound = $this->findFiles($templatesPath);
@@ -160,6 +162,7 @@ protected function getTemplateContents(string $filePath): string
160162
return (string) file_get_contents($filePath);
161163
}
162164

165+
/** @return array{template: string, file: string, valid: bool, line?: int, exception?: Error} */
163166
private function validate(string $template, string $file): array
164167
{
165168
$twig = Template::getTwigEnvironment(null);

phpstan-baseline.neon

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -700,26 +700,11 @@ parameters:
700700
count: 1
701701
path: libraries/classes/Command/TwigLintCommand.php
702702

703-
-
704-
message: "#^Method PhpMyAdmin\\\\Command\\\\TwigLintCommand\\:\\:findFiles\\(\\) return type has no value type specified in iterable type array\\.$#"
705-
count: 1
706-
path: libraries/classes/Command/TwigLintCommand.php
707-
708703
-
709704
message: "#^Method PhpMyAdmin\\\\Command\\\\TwigLintCommand\\:\\:getContext\\(\\) return type has no value type specified in iterable type array\\.$#"
710705
count: 1
711706
path: libraries/classes/Command/TwigLintCommand.php
712707

713-
-
714-
message: "#^Method PhpMyAdmin\\\\Command\\\\TwigLintCommand\\:\\:getFilesInfo\\(\\) return type has no value type specified in iterable type array\\.$#"
715-
count: 1
716-
path: libraries/classes/Command/TwigLintCommand.php
717-
718-
-
719-
message: "#^Method PhpMyAdmin\\\\Command\\\\TwigLintCommand\\:\\:validate\\(\\) return type has no value type specified in iterable type array\\.$#"
720-
count: 1
721-
path: libraries/classes/Command/TwigLintCommand.php
722-
723708
-
724709
message: "#^Only booleans are allowed in a ternary operator condition, \\(callable\\)\\|null given\\.$#"
725710
count: 1
@@ -745,16 +730,6 @@ parameters:
745730
count: 1
746731
path: libraries/classes/Command/TwigLintCommand.php
747732

748-
-
749-
message: "#^Parameter \\#1 \\$filePath of method PhpMyAdmin\\\\Command\\\\TwigLintCommand\\:\\:getTemplateContents\\(\\) expects string, mixed given\\.$#"
750-
count: 1
751-
path: libraries/classes/Command/TwigLintCommand.php
752-
753-
-
754-
message: "#^Parameter \\#2 \\$file of method PhpMyAdmin\\\\Command\\\\TwigLintCommand\\:\\:validate\\(\\) expects string, mixed given\\.$#"
755-
count: 1
756-
path: libraries/classes/Command/TwigLintCommand.php
757-
758733
-
759734
message: "#^Parameter \\#2 \\$template of method PhpMyAdmin\\\\Command\\\\TwigLintCommand\\:\\:renderException\\(\\) expects string, mixed given\\.$#"
760735
count: 1
@@ -40975,11 +40950,6 @@ parameters:
4097540950
count: 1
4097640951
path: test/classes/Command/TwigLintCommandTest.php
4097740952

40978-
-
40979-
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\TestCase\\:\\:exactly\\(\\)\\.$#"
40980-
count: 2
40981-
path: test/classes/Command/TwigLintCommandTest.php
40982-
4098340953
-
4098440954
message: "#^Parameter \\#1 \\$array_arg of function sort expects TArray of array\\<T\\>, mixed given\\.$#"
4098540955
count: 2

psalm-baseline.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,8 @@
233233
</NonInvariantDocblockPropertyType>
234234
</file>
235235
<file src="libraries/classes/Command/TwigLintCommand.php">
236-
<MixedArgument occurrences="6">
236+
<MixedArgument occurrences="5">
237237
<code>$code</code>
238-
<code>$file</code>
239238
<code>$info['exception']</code>
240239
<code>$info['file']</code>
241240
<code>$info['file']</code>
@@ -251,9 +250,8 @@
251250
<code>$info['valid']</code>
252251
<code>$info['valid']</code>
253252
</MixedArrayAccess>
254-
<MixedAssignment occurrences="4">
253+
<MixedAssignment occurrences="3">
255254
<code>$code</code>
256-
<code>$file</code>
257255
<code>$info</code>
258256
<code>$showDeprecations</code>
259257
</MixedAssignment>
@@ -14719,13 +14717,15 @@
1471914717
<code>$filesFound</code>
1472014718
<code>$filesInfos</code>
1472114719
</MixedArgument>
14722-
<MixedAssignment occurrences="5">
14720+
<MixedAssignment occurrences="4">
1472314721
<code>$context</code>
1472414722
<code>$context</code>
1472514723
<code>$filesFound</code>
14726-
<code>$filesFound</code>
1472714724
<code>$filesInfos</code>
1472814725
</MixedAssignment>
14726+
<PossiblyUndefinedArrayOffset occurrences="1">
14727+
<code>$filesFound[0]['exception']</code>
14728+
</PossiblyUndefinedArrayOffset>
1472914729
</file>
1473014730
<file src="test/classes/CommonTest.php">
1473114731
<DocblockTypeContradiction occurrences="2">

test/classes/Command/TwigLintCommandTest.php

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpMyAdmin\Tests\AbstractTestCase;
99
use Symfony\Component\Console\Command\Command;
1010
use Twig\Error\SyntaxError;
11-
use Twig\Source;
1211

1312
use function class_exists;
1413
use function sort;
@@ -98,53 +97,21 @@ public function testGetFilesInfo(): void
9897
], $filesInfos);
9998
}
10099

101-
/**
102-
* @requires PHPUnit < 10
103-
*/
104100
public function testGetFilesInfoInvalidFile(): void
105101
{
106-
$command = $this->getMockBuilder(TwigLintCommand::class)
107-
->onlyMethods(['getTemplateContents', 'findFiles'])
108-
->getMock();
109-
110-
$command->expects($this->exactly(1))
111-
->method('findFiles')
112-
->willReturn(
113-
[
114-
'foo.twig',
115-
'foo-invalid.twig',
116-
]
117-
);
118-
119-
$command->expects($this->exactly(2))
120-
->method('getTemplateContents')
121-
->withConsecutive(
122-
['foo.twig'],
123-
['foo-invalid.twig']
124-
)
125-
->willReturnOnConsecutiveCalls('{{ file }}', '{{ file }');
126-
127-
$filesFound = $this->callFunction($command, TwigLintCommand::class, 'getFilesInfo', [
128-
TEST_PATH . 'test/classes/_data/file_listing',
129-
]);
130-
131-
self::assertEquals([
132-
[
133-
'template' => '{{ file }}',
134-
'file' => 'foo.twig',
135-
'valid' => true,
136-
],
137-
[
138-
'template' => '{{ file }',
139-
'file' => 'foo-invalid.twig',
140-
'valid' => false,
141-
'line' => 1,
142-
'exception' => new SyntaxError('Unexpected "}".', 1, new Source(
143-
'{{ file }',
144-
'foo-invalid.twig'
145-
)),
146-
],
147-
], $filesFound);
102+
$twigLintCommand = new TwigLintCommand();
103+
$path = __DIR__ . '/../_data/templates/lint_command';
104+
$filesFound = $twigLintCommand->getFilesInfo($path);
105+
106+
self::assertCount(2, $filesFound);
107+
self::assertSame('{{ file }' . "\n", $filesFound[0]['template']);
108+
self::assertSame($path . '/foo-invalid.twig', $filesFound[0]['file']);
109+
self::assertArrayHasKey('exception', $filesFound[0]);
110+
$exception = $filesFound[0]['exception'];
111+
self::assertInstanceOf(SyntaxError::class, $exception);
112+
self::assertSame('Unexpected "}" in "' . $path . '/foo-invalid.twig" at line 1.', $exception->getMessage());
113+
self::assertSame('{{ file }}' . "\n", $filesFound[1]['template']);
114+
self::assertSame($path . '/foo-valid.twig', $filesFound[1]['file']);
148115
}
149116

150117
public function testGetContext(): void
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ file }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ file }}

0 commit comments

Comments
 (0)