PsalmJsonResultsParserTest::testTypeGuesser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\PsalmJsonResultsParser;
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\PsalmJsonResultsParser\PsalmJsonResultsParser;
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 PsalmJsonResultsParserTest 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 PsalmJsonResultsParser
32
     */
33
    private $psalmResultsParser;
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->psalmResultsParser = new PsalmJsonResultsParser();
44
45
        // Convert both ways
46
    }
47
48
    public function testConversion(): void
49
    {
50
        $original = $this->getResource('psalm/psalm.json');
51
        $this->analysisResults = $this->psalmResultsParser->convertFromString($original, $this->projectRoot);
52
        $this->assertCount(3, $this->analysisResults->getAnalysisResults());
53
54
        $result1 = $this->analysisResults->getAnalysisResults()[0];
55
        $result2 = $this->analysisResults->getAnalysisResults()[1];
56
        $result3 = $this->analysisResults->getAnalysisResults()[2];
57
58
        $this->assertMatch($result1,
59
            'src/Domain/ResultsParser/AnalysisResults.php',
60
            67,
61
            'MismatchingDocblockParamType',
62
            Severity::error(),
63
        );
64
        $this->assertSame(
65
            "Parameter \$array has wrong type 'array<mixed, mixed>', should be 'int'",
66
            $result1->getMessage(),
67
        );
68
69
        $this->assertMatch($result2,
70
            'src/Domain/Utils/JsonUtils.php',
71
            29,
72
            'MixedAssignment',
73
            Severity::error(),
74
        );
75
76
        $this->assertMatch($result3,
77
            'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php',
78
            90,
79
            'MixedAssignment',
80
            Severity::error(),
81
        );
82
    }
83
84
    public function testTypeGuesser(): void
85
    {
86
        $this->assertFalse($this->psalmResultsParser->showTypeGuessingWarning());
87
    }
88
89
    public function testInvalidJsonInput(): void
90
    {
91
        $fileContents = $this->getResource('invalid-json.json');
92
        $this->expectException(InvalidContentTypeException::class);
93
        $this->psalmResultsParser->convertFromString($fileContents, $this->projectRoot);
94
    }
95
96
    /**
97
     * @psalm-return array<int,array{string, int}>
98
     */
99
    public function invalidFileProvider(): array
100
    {
101
        return [
102
            ['psalm/psalm-invalid-missing-type.json', 4],
103
            ['psalm/psalm-invalid-missing-description.json', 1],
104
            ['psalm/psalm-invalid-missing-file.json', 2],
105
            ['psalm/psalm-invalid-missing-line.json', 3],
106
        ];
107
    }
108
109
    /**
110
     * @dataProvider invalidFileProvider
111
     */
112
    public function testInvalidFileFormat(string $fileName, int $resultWithIssue): void
113
    {
114
        $fileContents = $this->getResource($fileName);
115
        $this->expectParseAtLocationExceptionForResult($resultWithIssue);
116
        $this->psalmResultsParser->convertFromString($fileContents, $this->projectRoot);
117
    }
118
}
119