SarbJsonResultsParserTest::testInvalidFileFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
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\SarbJsonResultsParser;
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 SarbJsonResultsParserTest 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 SarbJsonResultsParser
32
     */
33
    private $sarbJsonResultsParser;
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->sarbJsonResultsParser = new SarbJsonResultsParser();
44
    }
45
46
    public function testConversion(): void
47
    {
48
        $fileContents = $this->getResource('sarb/sarb.json');
49
        $this->analysisResults = $this->sarbJsonResultsParser->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
67
        $this->assertMatch($result2,
68
            'src/Domain/Utils/JsonUtils.php',
69
            29,
70
            'MixedAssignment',
71
            Severity::error(),
72
        );
73
74
        $this->assertMatch($result3,
75
            'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php',
76
            90,
77
            'MixedAssignment',
78
            Severity::warning(),
79
        );
80
    }
81
82
    public function testTypeGuesser(): void
83
    {
84
        $this->assertFalse($this->sarbJsonResultsParser->showTypeGuessingWarning());
85
    }
86
87
    /**
88
     * @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...
89
     */
90
    public function invalidFileProvider(): array
91
    {
92
        return [
93
            ['sarb/sarb-invalid-missing-description.json', 1],
94
            ['sarb/sarb-invalid-missing-file.json', 2],
95
            ['sarb/sarb-invalid-missing-line.json', 2],
96
            ['sarb/sarb-invalid-missing-type.json', 3],
97
            ['sarb/sarb-invalid-severity.json', 3],
98
        ];
99
    }
100
101
    /**
102
     * @dataProvider invalidFileProvider
103
     */
104
    public function testInvalidFileFormat(string $fileName, int $resultWithIssue): void
105
    {
106
        $fileContents = $this->getResource($fileName);
107
        $this->expectParseAtLocationExceptionForResult($resultWithIssue);
108
        $this->sarbJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
109
    }
110
111
    public function testInvalidJsonInput(): void
112
    {
113
        $fileContents = $this->getResource('invalid-json.json');
114
        $this->expectException(InvalidContentTypeException::class);
115
        $this->sarbJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
116
    }
117
}
118