|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\PhanJsonResultsParser; |
|
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\Plugins\ResultsParsers\PhanJsonResultsParser\PhanJsonResultsParser; |
|
11
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertFileContentsSameTrait; |
|
12
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertResultMatch; |
|
13
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\ResourceLoaderTrait; |
|
14
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\ExpectParseExceptionWithResultTrait; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
final class PhanJsonResultsParserTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
use AssertFileContentsSameTrait; |
|
20
|
|
|
use AssertResultMatch; |
|
21
|
|
|
use ExpectParseExceptionWithResultTrait; |
|
22
|
|
|
use ResourceLoaderTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ProjectRoot |
|
26
|
|
|
*/ |
|
27
|
|
|
private $projectRoot; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var PhanJsonResultsParser |
|
31
|
|
|
*/ |
|
32
|
|
|
private $phanJsonResultsParser; |
|
33
|
|
|
|
|
34
|
|
|
protected function setUp(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->projectRoot = ProjectRoot::fromProjectRoot('/vagrant/static-analysis-baseliner', '/home'); |
|
37
|
|
|
$this->phanJsonResultsParser = new PhanJsonResultsParser(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testConversionFromString(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$fileContents = $this->getResource('phan/phan.json'); |
|
43
|
|
|
$analysisResults = $this->phanJsonResultsParser->convertFromString($fileContents, $this->projectRoot); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertCount(2, $analysisResults->getAnalysisResults()); |
|
46
|
|
|
|
|
47
|
|
|
$result1 = $analysisResults->getAnalysisResults()[0]; |
|
48
|
|
|
$result2 = $analysisResults->getAnalysisResults()[1]; |
|
49
|
|
|
|
|
50
|
|
|
$this->assertMatch($result1, |
|
51
|
|
|
'src/Domain/Analyser/BaseLineResultsRemover.php', |
|
52
|
|
|
16, |
|
53
|
|
|
'PhanUnreferencedUseNormal', |
|
54
|
|
|
Severity::error()); |
|
55
|
|
|
$this->assertSame( |
|
56
|
|
|
'NOOPError PhanUnreferencedUseNormal Possibly zero references to use statement for classlike/namespace BaseLine (\DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\BaseLine)', |
|
57
|
|
|
$result1->getMessage(), |
|
58
|
|
|
); |
|
59
|
|
|
$this->assertSame( |
|
60
|
|
|
'/vagrant/static-analysis-baseliner/src/Domain/Analyser/BaseLineResultsRemover.php', |
|
61
|
|
|
$result1->getLocation()->getAbsoluteFileName()->getFileName(), |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertMatch($result2, |
|
65
|
|
|
'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php', |
|
66
|
|
|
107, |
|
67
|
|
|
'PhanPossiblyNullTypeArgument', |
|
68
|
|
|
Severity::error()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testWithRelativePath(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$projectRoot = $this->projectRoot->withRelativePath('code'); |
|
74
|
|
|
|
|
75
|
|
|
$fileContents = $this->getResource('phan/phan.json'); |
|
76
|
|
|
$analysisResults = $this->phanJsonResultsParser->convertFromString($fileContents, $projectRoot); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertCount(2, $analysisResults->getAnalysisResults()); |
|
79
|
|
|
|
|
80
|
|
|
$result1 = $analysisResults->getAnalysisResults()[0]; |
|
81
|
|
|
$result2 = $analysisResults->getAnalysisResults()[1]; |
|
82
|
|
|
|
|
83
|
|
|
$this->assertMatch($result1, |
|
84
|
|
|
'src/Domain/Analyser/BaseLineResultsRemover.php', |
|
85
|
|
|
16, |
|
86
|
|
|
'PhanUnreferencedUseNormal', |
|
87
|
|
|
Severity::error()); |
|
88
|
|
|
$this->assertSame( |
|
89
|
|
|
'NOOPError PhanUnreferencedUseNormal Possibly zero references to use statement for classlike/namespace BaseLine (\DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\BaseLine)', |
|
90
|
|
|
$result1->getMessage(), |
|
91
|
|
|
); |
|
92
|
|
|
$this->assertSame( |
|
93
|
|
|
'/vagrant/static-analysis-baseliner/code/src/Domain/Analyser/BaseLineResultsRemover.php', |
|
94
|
|
|
$result1->getLocation()->getAbsoluteFileName()->getFileName(), |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertMatch($result2, |
|
98
|
|
|
'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php', |
|
99
|
|
|
107, |
|
100
|
|
|
'PhanPossiblyNullTypeArgument', |
|
101
|
|
|
Severity::error()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testTypeGuesser(): void |
|
105
|
|
|
{ |
|
106
|
|
|
$this->assertFalse($this->phanJsonResultsParser->showTypeGuessingWarning()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return array<int,array{string, int}> |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
|
|
public function invalidFileProvider(): array |
|
113
|
|
|
{ |
|
114
|
|
|
return [ |
|
115
|
|
|
['phan/phan-invalid-missing-check_name.json', 1], |
|
116
|
|
|
['phan/phan-invalid-missing-description.json', 1], |
|
117
|
|
|
['phan/phan-invalid-missing-file.json', 1], |
|
118
|
|
|
['phan/phan-invalid-missing-line.json', 1], |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @dataProvider invalidFileProvider |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testInvalidFileFormat(string $fileName, int $resultWithIssue): void |
|
126
|
|
|
{ |
|
127
|
|
|
$fileContents = $this->getResource($fileName); |
|
128
|
|
|
$this->expectParseAtLocationExceptionForResult($resultWithIssue); |
|
129
|
|
|
$this->phanJsonResultsParser->convertFromString($fileContents, $this->projectRoot); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testInvalidJsonInput(): void |
|
133
|
|
|
{ |
|
134
|
|
|
$fileContents = $this->getResource('invalid-json.json'); |
|
135
|
|
|
$this->expectException(InvalidContentTypeException::class); |
|
136
|
|
|
$this->phanJsonResultsParser->convertFromString($fileContents, $this->projectRoot); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|