|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\PhpstanJsonResultsParser; |
|
6
|
|
|
|
|
7
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot; |
|
8
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Severity; |
|
9
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\File\InvalidContentTypeException; |
|
10
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ErrorReportedByStaticAnalysisTool; |
|
11
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\FqcnRemover; |
|
12
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\ParseAtLocationException; |
|
13
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhpstanJsonResultsParser\PhpstanJsonResultsParser; |
|
14
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertFileContentsSameTrait; |
|
15
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertResultMatch; |
|
16
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\ResourceLoaderTrait; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
|
|
19
|
|
|
class PhpstanJsonResultsParserTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
use AssertFileContentsSameTrait; |
|
22
|
|
|
use AssertResultMatch; |
|
23
|
|
|
use ResourceLoaderTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ProjectRoot |
|
27
|
|
|
*/ |
|
28
|
|
|
private $projectRoot; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var PhpstanJsonResultsParser |
|
32
|
|
|
*/ |
|
33
|
|
|
private $phpstanJsonResultsParser; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $fileContents; |
|
39
|
|
|
|
|
40
|
|
|
protected function setUp(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->projectRoot = ProjectRoot::fromProjectRoot('/vagrant/static-analysis-baseliner', '/home'); |
|
43
|
|
|
$this->phpstanJsonResultsParser = new PhpstanJsonResultsParser(new FqcnRemover()); |
|
44
|
|
|
$this->fileContents = $this->getResource('phpstan/phpstan.json'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testConversionFromString(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$analysisResults = $this->phpstanJsonResultsParser->convertFromString($this->fileContents, $this->projectRoot); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertCount(3, $analysisResults->getAnalysisResults()); |
|
52
|
|
|
|
|
53
|
|
|
$result1 = $analysisResults->getAnalysisResults()[0]; |
|
54
|
|
|
$result2 = $analysisResults->getAnalysisResults()[1]; |
|
55
|
|
|
$result3 = $analysisResults->getAnalysisResults()[2]; |
|
56
|
|
|
|
|
57
|
|
|
$this->assertMatch($result1, |
|
58
|
|
|
'src/Domain/BaseLiner/BaseLineImporter.php', |
|
59
|
|
|
89, |
|
60
|
|
|
'Parameter #1 $array of static method expects int, array given.', |
|
61
|
|
|
Severity::error() |
|
62
|
|
|
); |
|
63
|
|
|
$this->assertSame( |
|
64
|
|
|
'Parameter #1 $array of static method DaveLiddament\\StaticAnalysisResultsBaseliner\\Domain\\ResultsParser\\AnalysisResults::fromArray() expects int, array given.', |
|
65
|
|
|
$result1->getMessage() |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertMatch($result2, |
|
69
|
|
|
'src/Domain/ResultsParser/AnalysisResults.php', |
|
70
|
|
|
0, |
|
71
|
|
|
'Argument of an invalid type int supplied for foreach, only iterables are supported.', |
|
72
|
|
|
Severity::error() |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertMatch($result3, |
|
76
|
|
|
'src/Domain/ResultsParser/AnalysisResults.php', |
|
77
|
|
|
73, |
|
78
|
|
|
'PHPDoc tag @param for parameter $array with type array is incompatible with native type int', |
|
79
|
|
|
Severity::error() |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testTypeGuesser(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->assertTrue($this->phpstanJsonResultsParser->showTypeGuessingWarning()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testInvalidJsonInput(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$fileContents = $this->getResource('invalid-json.json'); |
|
91
|
|
|
$this->expectException(InvalidContentTypeException::class); |
|
92
|
|
|
$this->phpstanJsonResultsParser->convertFromString($fileContents, $this->projectRoot); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @psalm-return array<int,array{string}> |
|
97
|
|
|
*/ |
|
98
|
|
|
public function invalidFileProvider(): array |
|
99
|
|
|
{ |
|
100
|
|
|
return [ |
|
101
|
|
|
['phpstan/phpstan-invalid-missing-description.json'], |
|
102
|
|
|
['phpstan/phpstan-invalid-missing-file.json'], |
|
103
|
|
|
['phpstan/phpstan-invalid-missing-files.json'], |
|
104
|
|
|
['phpstan/phpstan-invalid-missing-line.json'], |
|
105
|
|
|
['phpstan/phpstan-invalid-missing-errors.json'], |
|
106
|
|
|
['phpstan/phpstan-invalid-errors-not-strings.json'], |
|
107
|
|
|
]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @dataProvider invalidFileProvider |
|
112
|
|
|
*/ |
|
113
|
|
|
public function testInvalidFileFormat(string $fileName): void |
|
114
|
|
|
{ |
|
115
|
|
|
$fileContents = $this->getResource($fileName); |
|
116
|
|
|
$this->expectException(ParseAtLocationException::class); |
|
117
|
|
|
$this->phpstanJsonResultsParser->convertFromString($fileContents, $this->projectRoot); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testPhpstanReportsErrors(): void |
|
121
|
|
|
{ |
|
122
|
|
|
$fileContents = $this->getResource('phpstan/phpstan-with-errors.json'); |
|
123
|
|
|
$this->expectException(ErrorReportedByStaticAnalysisTool::class); |
|
124
|
|
|
$this->expectExceptionMessage('PHPStan failed with errors:'.\PHP_EOL.'Error 1'.\PHP_EOL.'Error 2'); |
|
125
|
|
|
$this->phpstanJsonResultsParser->convertFromString($fileContents, $this->projectRoot); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|