Skip to content

Commit 0f27eea

Browse files
committed
Add BaselineIgnoredErrorsHelperTest, remove AnalyseCommandTest cases
1 parent e69bb2c commit 0f27eea

File tree

11 files changed

+133
-176
lines changed

11 files changed

+133
-176
lines changed

build/collision-detector.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
"../tests/PHPStan/Rules/Functions/data/define-bug-3349.php",
1616
"../tests/PHPStan/Levels/data/stubs/function.php",
1717
"../tests/PHPStan/Rules/Properties/data/abstract-final-property-hook-parse-error.php",
18-
"../tests/PHPStan/Rules/Properties/data/final-property-hooks.php",
19-
"../tests/PHPStan/Levels/data/stubs/function.php",
20-
"../tests/PHPStan/Command/data-ignore-new-errors/A.php",
21-
"../tests/PHPStan/Command/data-ignore-new-errors-compare/A.php"
22-
]
18+
"../tests/PHPStan/Rules/Properties/data/final-property-hooks.php"
19+
]
2320
}

build/phpstan.neon

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ parameters:
2424
checkMissingCallableSignature: true
2525
excludePaths:
2626
- ../tests/*/data/*
27-
- ../tests/*/data-*/*
2827
- ../tests/tmp/*
2928
- ../tests/PHPStan/Analyser/nsrt/*
3029
- ../tests/PHPStan/Analyser/traits/*

phpcs.xml

-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@
209209
<exclude-pattern>tests/*/Fixture/</exclude-pattern>
210210
<exclude-pattern>tests/*/cache/</exclude-pattern>
211211
<exclude-pattern>tests/*/data/</exclude-pattern>
212-
<exclude-pattern>tests/*/data-*/</exclude-pattern>
213212
<exclude-pattern>tests/*/traits/</exclude-pattern>
214213
<exclude-pattern>tests/PHPStan/Analyser/nsrt/</exclude-pattern>
215214
<exclude-pattern>tests/e2e/anon-class/</exclude-pattern>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\Ignore;
4+
5+
use PHPStan\Analyser\Error;
6+
use PHPStan\File\ParentDirectoryRelativePathHelper;
7+
use PHPStan\Testing\PHPStanTestCase;
8+
9+
class BaselineIgnoredErrorsHelperTest extends PHPStanTestCase
10+
{
11+
12+
public function testEmptyBaseline(): void
13+
{
14+
$result = $this->runRemoveUnusedIgnoredErrors(
15+
[],
16+
[
17+
new Error(
18+
'Foo',
19+
__DIR__ . '/foo.php',
20+
),
21+
],
22+
);
23+
24+
$this->assertCount(0, $result);
25+
}
26+
27+
public function testRemoveUnusedIgnoreError(): void
28+
{
29+
$result = $this->runRemoveUnusedIgnoredErrors(
30+
[
31+
[
32+
'message' => '#^Foo#',
33+
'count' => 1,
34+
'path' => 'foo.php',
35+
],
36+
],
37+
[],
38+
);
39+
40+
$this->assertCount(0, $result);
41+
}
42+
43+
public function testeReduceErrorCount(): void
44+
{
45+
$result = $this->runRemoveUnusedIgnoredErrors(
46+
[
47+
[
48+
'message' => '#^Foo#',
49+
'count' => 2,
50+
'path' => 'foo.php',
51+
],
52+
],
53+
[
54+
new Error(
55+
'Foo',
56+
__DIR__ . '/foo.php',
57+
),
58+
],
59+
);
60+
61+
$this->assertCount(1, $result);
62+
$this->assertSame('Foo', $result[0]->getMessage());
63+
$this->assertSame(__DIR__ . '/foo.php', $result[0]->getFilePath());
64+
}
65+
66+
public function testNewError(): void
67+
{
68+
$result = $this->runRemoveUnusedIgnoredErrors(
69+
[
70+
[
71+
'message' => '#^Foo#',
72+
'count' => 1,
73+
'path' => 'foo.php',
74+
],
75+
],
76+
[
77+
new Error(
78+
'Bar',
79+
__DIR__ . '/bar.php',
80+
),
81+
],
82+
);
83+
84+
$this->assertCount(0, $result);
85+
}
86+
87+
public function testIncreaseErrorCount(): void
88+
{
89+
$result = $this->runRemoveUnusedIgnoredErrors(
90+
[
91+
[
92+
'message' => '#^Foo#',
93+
'count' => 1,
94+
'path' => 'foo.php',
95+
],
96+
],
97+
[
98+
new Error(
99+
'Foo',
100+
__DIR__ . '/foo.php',
101+
),
102+
new Error(
103+
'Foo',
104+
__DIR__ . '/foo.php',
105+
),
106+
],
107+
);
108+
109+
$this->assertCount(1, $result);
110+
$this->assertSame('Foo', $result[0]->getMessage());
111+
$this->assertSame(__DIR__ . '/foo.php', $result[0]->getFilePath());
112+
}
113+
114+
/**
115+
* @param mixed[][] $baselinedErrors
116+
* @param list<Error> $currentAnalysisErrors
117+
* @return list<Error> errors
118+
*/
119+
private function runRemoveUnusedIgnoredErrors(array $baselinedErrors, array $currentAnalysisErrors): array
120+
{
121+
$baselineIgnoredErrorHelper = new BaselineIgnoredErrorHelper(self::getFileHelper());
122+
123+
$parentDirHelper = new ParentDirectoryRelativePathHelper(__DIR__);
124+
125+
return $baselineIgnoredErrorHelper->removeUnusedIgnoredErrors($baselinedErrors, $currentAnalysisErrors, $parentDirHelper);
126+
}
127+
128+
}

tests/PHPStan/Command/AnalyseCommandTest.php

+3-98
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
use PHPStan\Testing\PHPStanTestCase;
77
use Symfony\Component\Console\Tester\CommandTester;
88
use Throwable;
9-
use function array_merge;
109
use function chdir;
1110
use function getcwd;
1211
use function microtime;
1312
use function realpath;
14-
use function rename;
1513
use function sprintf;
16-
use function unlink;
1714
use const DIRECTORY_SEPARATOR;
1815
use const PHP_EOL;
1916

@@ -74,98 +71,6 @@ public function testValidAutoloadFile(): void
7471
}
7572
}
7673

77-
public function testGenerateBaselineIgnoreNewErrorsRemoveFile(): void
78-
{
79-
$baselineFile = __DIR__ . '/data-ignore-new-errors/baseline.neon';
80-
$this->runCommand(0, [
81-
'paths' => [__DIR__ . '/data-ignore-new-errors/A.php', __DIR__ . '/data-ignore-new-errors/B.php'],
82-
'--configuration' => __DIR__ . '/data-ignore-new-errors/empty.neon',
83-
'--level' => '9',
84-
'--generate-baseline' => $baselineFile,
85-
]);
86-
87-
$output = $this->runCommand(0, [
88-
'paths' => [__DIR__ . '/data-ignore-new-errors/B.php', __DIR__ . '/data-ignore-new-errors/C.php'],
89-
'--configuration' => $baselineFile,
90-
'--level' => '9',
91-
'--generate-baseline' => $baselineFile,
92-
'--only-remove-errors' => true,
93-
]);
94-
@unlink($baselineFile);
95-
96-
$this->assertStringContainsString('[OK] Baseline generated with 1 error', $output);
97-
}
98-
99-
public function testGenerateBaselineIgnoreNewErrorsReducedErrorCount(): void
100-
{
101-
$baselineFile = __DIR__ . '/data-ignore-new-errors-compare/baseline.neon';
102-
$baselineFileSecondRun = __DIR__ . '/data-ignore-new-errors/baseline.neon';
103-
$this->runCommand(0, [
104-
'paths' => [__DIR__ . '/data-ignore-new-errors-compare/A.php'],
105-
'--configuration' => __DIR__ . '/data-ignore-new-errors-compare/empty.neon',
106-
'--level' => '9',
107-
'--generate-baseline' => $baselineFile,
108-
]);
109-
110-
rename($baselineFile, $baselineFileSecondRun);
111-
$output = $this->runCommand(0, [
112-
'paths' => [__DIR__ . '/data-ignore-new-errors/A.php'],
113-
'--configuration' => $baselineFileSecondRun,
114-
'--level' => '9',
115-
'--generate-baseline' => $baselineFileSecondRun,
116-
'--only-remove-errors' => true,
117-
]);
118-
@unlink($baselineFileSecondRun);
119-
120-
$this->assertStringContainsString('[OK] Baseline generated with 2 errors', $output);
121-
}
122-
123-
public function testGenerateBaselineIgnoreNewErrorsIncreasedErrorCount(): void
124-
{
125-
$baselineFile = __DIR__ . '/data-ignore-new-errors/baseline.neon';
126-
$baselineFileSecondRun = __DIR__ . '/data-ignore-new-errors-compare/baseline.neon';
127-
$this->runCommand(0, [
128-
'paths' => [__DIR__ . '/data-ignore-new-errors/A.php'],
129-
'--configuration' => __DIR__ . '/data-ignore-new-errors/empty.neon',
130-
'--level' => '9',
131-
'--generate-baseline' => $baselineFile,
132-
]);
133-
134-
rename($baselineFile, $baselineFileSecondRun);
135-
$output = $this->runCommand(0, [
136-
'paths' => [__DIR__ . '/data-ignore-new-errors-compare/A.php'],
137-
'--configuration' => $baselineFileSecondRun,
138-
'--level' => '9',
139-
'--generate-baseline' => $baselineFileSecondRun,
140-
'--only-remove-errors' => true,
141-
]);
142-
@unlink($baselineFileSecondRun);
143-
144-
$this->assertStringContainsString('[OK] Baseline generated with 2 errors', $output);
145-
}
146-
147-
public function testGenerateBaselineIgnoreNewErrorsEmptyBaseline(): void
148-
{
149-
$baselineFile = __DIR__ . '/data-ignore-new-errors/baseline.neon';
150-
$this->runCommand(0, [
151-
'paths' => [__DIR__ . '/data-ignore-new-errors/A.php', __DIR__ . '/data-ignore-new-errors/B.php'],
152-
'--configuration' => __DIR__ . '/data-ignore-new-errors/empty.neon',
153-
'--level' => '9',
154-
'--generate-baseline' => $baselineFile,
155-
]);
156-
157-
$output = $this->runCommand(1, [
158-
'paths' => [__DIR__ . '/data-ignore-new-errors/C.php'],
159-
'--configuration' => $baselineFile,
160-
'--level' => '9',
161-
'--generate-baseline' => $baselineFile,
162-
'--only-remove-errors' => true,
163-
]);
164-
@unlink($baselineFile);
165-
166-
$this->assertStringContainsString('[ERROR] No errors were found during the analysis.', $output);
167-
}
168-
16974
/**
17075
* @return string[][]
17176
*/
@@ -212,16 +117,16 @@ public static function autoDiscoveryPathsProvider(): array
212117
}
213118

214119
/**
215-
* @param array<string, string|string[]|bool> $parameters
120+
* @param array<string, string> $parameters
216121
*/
217122
private function runCommand(int $expectedStatusCode, array $parameters = []): string
218123
{
219124
$commandTester = new CommandTester(new AnalyseCommand([], microtime(true)));
220125

221-
$commandTester->execute(array_merge([
126+
$commandTester->execute([
222127
'paths' => [__DIR__ . DIRECTORY_SEPARATOR . 'test'],
223128
'--debug' => true,
224-
], $parameters), ['debug' => true]);
129+
] + $parameters, ['debug' => true]);
225130

226131
$this->assertSame($expectedStatusCode, $commandTester->getStatusCode(), $commandTester->getDisplay());
227132

tests/PHPStan/Command/data-ignore-new-errors-compare/A.php

-16
This file was deleted.

tests/PHPStan/Command/data-ignore-new-errors-compare/empty.neon

Whitespace-only changes.

tests/PHPStan/Command/data-ignore-new-errors/A.php

-23
This file was deleted.

tests/PHPStan/Command/data-ignore-new-errors/B.php

-16
This file was deleted.

tests/PHPStan/Command/data-ignore-new-errors/C.php

-16
This file was deleted.

tests/PHPStan/Command/data-ignore-new-errors/empty.neon

Whitespace-only changes.

0 commit comments

Comments
 (0)