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