Completed
Push — master ( 72bc65...6342c7 )
by Dave
01:22 queued 01:19
created

testConversion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 24
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 34
rs 9.536
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\File\InvalidContentTypeException;
9
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResults;
10
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\SarbJsonResultsParser\SarbRelativeFileJsonResultsParser;
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
class SarbRelativeFileJsonResultsParserTest extends TestCase
18
{
19
    use AssertFileContentsSameTrait;
20
    use AssertResultMatch;
21
    use ExpectParseExceptionWithResultTrait;
22
    use ResourceLoaderTrait;
23
24
    /**
25
     * @var AnalysisResults
26
     */
27
    private $analysisResults;
28
29
    /**
30
     * @var SarbRelativeFileJsonResultsParser
31
     */
32
    private $sarbRelativeFileJsonResultsParser;
33
    /**
34
     * @var ProjectRoot
35
     */
36
    private $projectRoot;
37
38
    protected function setUp(): void
39
    {
40
        $this->projectRoot = ProjectRoot::fromProjectRoot('/vagrant/static-analysis-baseliner', '/home');
41
42
        $this->sarbRelativeFileJsonResultsParser = new SarbRelativeFileJsonResultsParser();
43
    }
44
45
    public function testConversion(): void
46
    {
47
        $fileContents = $this->getResource('sarb-relative/sarb.json');
48
        $this->analysisResults = $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
49
        $this->assertCount(3, $this->analysisResults->getAnalysisResults());
50
51
        $result1 = $this->analysisResults->getAnalysisResults()[0];
52
        $result2 = $this->analysisResults->getAnalysisResults()[1];
53
        $result3 = $this->analysisResults->getAnalysisResults()[2];
54
55
        $this->assertMatch($result1,
56
            'src/Domain/ResultsParser/AnalysisResults.php',
57
            67,
58
            'MismatchingDocblockParamType'
59
        );
60
        $this->assertSame(
61
            "Parameter \$array has wrong type 'array<mixed, mixed>', should be 'int'",
62
            $result1->getMessage()
63
        );
64
        $this->assertSame(
65
            '/vagrant/static-analysis-baseliner/src/Domain/ResultsParser/AnalysisResults.php',
66
            $result1->getLocation()->getAbsoluteFileName()->getFileName()
67
        );
68
69
        $this->assertMatch($result2,
70
            'src/Domain/Utils/JsonUtils.php',
71
            29,
72
            'MixedAssignment'
73
        );
74
75
        $this->assertMatch($result3,
76
            'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php',
77
            90,
78
            'MixedAssignment'
79
        );
80
    }
81
82
    public function testConversionWithRelativePath(): void
83
    {
84
        $fileContents = $this->getResource('sarb-relative/sarb.json');
85
        $projectRoot = $this->projectRoot->withRelativePath('code');
86
        $this->analysisResults = $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $projectRoot);
87
        $this->assertCount(3, $this->analysisResults->getAnalysisResults());
88
89
        $result1 = $this->analysisResults->getAnalysisResults()[0];
90
        $result2 = $this->analysisResults->getAnalysisResults()[1];
91
        $result3 = $this->analysisResults->getAnalysisResults()[2];
92
93
        $this->assertMatch($result1,
94
            'src/Domain/ResultsParser/AnalysisResults.php',
95
            67,
96
            'MismatchingDocblockParamType'
97
        );
98
        $this->assertSame(
99
            "Parameter \$array has wrong type 'array<mixed, mixed>', should be 'int'",
100
            $result1->getMessage()
101
        );
102
        $this->assertSame(
103
            '/vagrant/static-analysis-baseliner/code/src/Domain/ResultsParser/AnalysisResults.php',
104
            $result1->getLocation()->getAbsoluteFileName()->getFileName()
105
        );
106
107
        $this->assertMatch($result2,
108
            'src/Domain/Utils/JsonUtils.php',
109
            29,
110
            'MixedAssignment'
111
        );
112
113
        $this->assertMatch($result3,
114
            'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php',
115
            90,
116
            'MixedAssignment'
117
        );
118
    }
119
120
    public function testTypeGuesser(): void
121
    {
122
        $this->assertFalse($this->sarbRelativeFileJsonResultsParser->showTypeGuessingWarning());
123
    }
124
125
    /**
126
     * @psalm-return array<int,array{string, int}>
127
     */
128
    public function invalidFileProvider(): array
129
    {
130
        return [
131
            ['sarb-relative/sarb-invalid-missing-description.json', 1],
132
            ['sarb-relative/sarb-invalid-missing-file.json', 2],
133
            ['sarb-relative/sarb-invalid-missing-line.json', 2],
134
            ['sarb-relative/sarb-invalid-missing-type.json', 3],
135
        ];
136
    }
137
138
    /**
139
     * @dataProvider invalidFileProvider
140
     */
141
    public function testInvalidFileFormat(string $fileName, int $resultWithIssue): void
142
    {
143
        $fileContents = $this->getResource($fileName);
144
        $this->expectParseAtLocationExceptionForResult($resultWithIssue);
145
        $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
146
    }
147
148
    public function testInvalidJsonInput(): void
149
    {
150
        $fileContents = $this->getResource('invalid-json.json');
151
        $this->expectException(InvalidContentTypeException::class);
152
        $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
153
    }
154
}
155