Passed
Pull Request — master (#96)
by Dave
02:39
created

SarbRelativeFileJsonResultsParserTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 71
c 1
b 0
f 0
dl 0
loc 136
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testInvalidFileFormat() 0 5 1
A invalidFileProvider() 0 7 1
A testTypeGuesser() 0 3 1
A testInvalidJsonInput() 0 5 1
A testConversion() 0 34 1
A testConversionWithRelativePath() 0 35 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\File\InvalidContentTypeException;
9
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResults;
10
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\SarbJsonResultsParser\SarbJsonResultsParser;
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
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 SarbJsonResultsParser
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new DaveLiddament\Static...FileJsonResultsParser() of type DaveLiddament\StaticAnal...veFileJsonResultsParser is incompatible with the declared type DaveLiddament\StaticAnal...r\SarbJsonResultsParser of property $sarbRelativeFileJsonResultsParser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
        );
61
        $this->assertSame(
62
            "Parameter \$array has wrong type 'array<mixed, mixed>', should be 'int'",
63
            $result1->getMessage()
64
        );
65
        $this->assertSame(
66
            '/vagrant/static-analysis-baseliner/src/Domain/ResultsParser/AnalysisResults.php',
67
            $result1->getLocation()->getAbsoluteFileName()->getFileName()
68
        );
69
70
        $this->assertMatch($result2,
71
            'src/Domain/Utils/JsonUtils.php',
72
            29,
73
            'MixedAssignment'
74
        );
75
76
        $this->assertMatch($result3,
77
            'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php',
78
            90,
79
            'MixedAssignment'
80
        );
81
    }
82
83
    public function testConversionWithRelativePath(): void
84
    {
85
        $fileContents = $this->getResource('sarb-relative/sarb.json');
86
        $projectRoot = $this->projectRoot->withRelativePath('code');
87
        $this->analysisResults = $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $projectRoot);
88
        $this->assertCount(3, $this->analysisResults->getAnalysisResults());
89
90
        $result1 = $this->analysisResults->getAnalysisResults()[0];
91
        $result2 = $this->analysisResults->getAnalysisResults()[1];
92
        $result3 = $this->analysisResults->getAnalysisResults()[2];
93
94
        $this->assertMatch($result1,
95
            'src/Domain/ResultsParser/AnalysisResults.php',
96
            67,
97
            'MismatchingDocblockParamType'
98
        );
99
        $this->assertSame(
100
            "Parameter \$array has wrong type 'array<mixed, mixed>', should be 'int'",
101
            $result1->getMessage()
102
        );
103
        $this->assertSame(
104
            '/vagrant/static-analysis-baseliner/code/src/Domain/ResultsParser/AnalysisResults.php',
105
            $result1->getLocation()->getAbsoluteFileName()->getFileName()
106
        );
107
108
        $this->assertMatch($result2,
109
            'src/Domain/Utils/JsonUtils.php',
110
            29,
111
            'MixedAssignment'
112
        );
113
114
        $this->assertMatch($result3,
115
            'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php',
116
            90,
117
            'MixedAssignment'
118
        );
119
    }
120
121
    public function testTypeGuesser(): void
122
    {
123
        $this->assertFalse($this->sarbRelativeFileJsonResultsParser->showTypeGuessingWarning());
124
    }
125
126
    /**
127
     * @psalm-return array<int,array{string, int}>
128
     */
129
    public function invalidFileProvider(): array
130
    {
131
        return [
132
            ['sarb-relative/sarb-invalid-missing-description.json', 1],
133
            ['sarb-relative/sarb-invalid-missing-file.json', 2],
134
            ['sarb-relative/sarb-invalid-missing-line.json', 2],
135
            ['sarb-relative/sarb-invalid-missing-type.json', 3],
136
        ];
137
    }
138
139
    /**
140
     * @dataProvider invalidFileProvider
141
     */
142
    public function testInvalidFileFormat(string $fileName, int $resultWithIssue): void
143
    {
144
        $fileContents = $this->getResource($fileName);
145
        $this->expectParseAtLocationExceptionForResult($resultWithIssue);
146
        $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
147
    }
148
149
    public function testInvalidJsonInput(): void
150
    {
151
        $fileContents = $this->getResource('invalid-json.json');
152
        $this->expectException(InvalidContentTypeException::class);
153
        $this->sarbRelativeFileJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
154
    }
155
}
156