SarbRelativeFileJsonResultsParserTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 78
c 2
b 0
f 0
dl 0
loc 143
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testTypeGuesser() 0 3 1
A testConversion() 0 37 1
A testConversionWithRelativePath() 0 38 1
A testInvalidFileFormat() 0 5 1
A invalidFileProvider() 0 8 1
A testInvalidJsonInput() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\SarbJsonResultsParser;
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\AnalysisResults;
11
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\SarbJsonResultsParser\SarbRelativeFileJsonResultsParser;
12
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertFileContentsSameTrait;
13
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertResultMatch;
14
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\ResourceLoaderTrait;
15
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\ExpectParseExceptionWithResultTrait;
16
use PHPUnit\Framework\TestCase;
17
18
final class SarbRelativeFileJsonResultsParserTest extends TestCase
19
{
20
    use AssertFileContentsSameTrait;
21
    use AssertResultMatch;
22
    use ExpectParseExceptionWithResultTrait;
23
    use ResourceLoaderTrait;
24
25
    /**
26
     * @var AnalysisResults
27
     */
28
    private $analysisResults;
29
30
    /**
31
     * @var SarbRelativeFileJsonResultsParser
32
     */
33
    private $sarbRelativeFileJsonResultsParser;
34
    /**
35
     * @var ProjectRoot
36
     */
37
    private $projectRoot;
38
39
    protected function setUp(): void
40
    {
41
        $this->projectRoot = ProjectRoot::fromProjectRoot('/vagrant/static-analysis-baseliner', '/home');
42
43
        $this->sarbRelativeFileJsonResultsParser = new SarbRelativeFileJsonResultsParser();
44
    }
45
46
    public function testConversion(): void
47
    {
48
        $fileContents = $this->getResource('sarb-relative/sarb.json');
49
        $this->analysisResults = $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
50
        $this->assertCount(3, $this->analysisResults->getAnalysisResults());
51
52
        $result1 = $this->analysisResults->getAnalysisResults()[0];
53
        $result2 = $this->analysisResults->getAnalysisResults()[1];
54
        $result3 = $this->analysisResults->getAnalysisResults()[2];
55
56
        $this->assertMatch($result1,
57
            'src/Domain/ResultsParser/AnalysisResults.php',
58
            67,
59
            'MismatchingDocblockParamType',
60
            Severity::error(),
61
        );
62
        $this->assertSame(
63
            "Parameter \$array has wrong type 'array<mixed, mixed>', should be 'int'",
64
            $result1->getMessage(),
65
        );
66
        $this->assertSame(
67
            '/vagrant/static-analysis-baseliner/src/Domain/ResultsParser/AnalysisResults.php',
68
            $result1->getLocation()->getAbsoluteFileName()->getFileName(),
69
        );
70
71
        $this->assertMatch($result2,
72
            'src/Domain/Utils/JsonUtils.php',
73
            29,
74
            'MixedAssignment',
75
            Severity::error(),
76
        );
77
78
        $this->assertMatch($result3,
79
            'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php',
80
            90,
81
            'MixedAssignment',
82
            Severity::warning(),
83
        );
84
    }
85
86
    public function testConversionWithRelativePath(): void
87
    {
88
        $fileContents = $this->getResource('sarb-relative/sarb.json');
89
        $projectRoot = $this->projectRoot->withRelativePath('code');
90
        $this->analysisResults = $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $projectRoot);
91
        $this->assertCount(3, $this->analysisResults->getAnalysisResults());
92
93
        $result1 = $this->analysisResults->getAnalysisResults()[0];
94
        $result2 = $this->analysisResults->getAnalysisResults()[1];
95
        $result3 = $this->analysisResults->getAnalysisResults()[2];
96
97
        $this->assertMatch($result1,
98
            'src/Domain/ResultsParser/AnalysisResults.php',
99
            67,
100
            'MismatchingDocblockParamType',
101
            Severity::error(),
102
        );
103
        $this->assertSame(
104
            "Parameter \$array has wrong type 'array<mixed, mixed>', should be 'int'",
105
            $result1->getMessage(),
106
        );
107
        $this->assertSame(
108
            '/vagrant/static-analysis-baseliner/code/src/Domain/ResultsParser/AnalysisResults.php',
109
            $result1->getLocation()->getAbsoluteFileName()->getFileName(),
110
        );
111
112
        $this->assertMatch($result2,
113
            'src/Domain/Utils/JsonUtils.php',
114
            29,
115
            'MixedAssignment',
116
            Severity::error(),
117
        );
118
119
        $this->assertMatch($result3,
120
            'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php',
121
            90,
122
            'MixedAssignment',
123
            Severity::warning(),
124
        );
125
    }
126
127
    public function testTypeGuesser(): void
128
    {
129
        $this->assertFalse($this->sarbRelativeFileJsonResultsParser->showTypeGuessingWarning());
130
    }
131
132
    /**
133
     * @return array<int,array{string, int}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string, int}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
134
     */
135
    public function invalidFileProvider(): array
136
    {
137
        return [
138
            ['sarb-relative/sarb-invalid-missing-description.json', 1],
139
            ['sarb-relative/sarb-invalid-missing-file.json', 2],
140
            ['sarb-relative/sarb-invalid-missing-line.json', 2],
141
            ['sarb-relative/sarb-invalid-missing-type.json', 3],
142
            ['sarb-relative/sarb-invalid-severity.json', 2],
143
        ];
144
    }
145
146
    /**
147
     * @dataProvider invalidFileProvider
148
     */
149
    public function testInvalidFileFormat(string $fileName, int $resultWithIssue): void
150
    {
151
        $fileContents = $this->getResource($fileName);
152
        $this->expectParseAtLocationExceptionForResult($resultWithIssue);
153
        $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
154
    }
155
156
    public function testInvalidJsonInput(): void
157
    {
158
        $fileContents = $this->getResource('invalid-json.json');
159
        $this->expectException(InvalidContentTypeException::class);
160
        $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
161
    }
162
}
163