Passed
Pull Request — master (#91)
by Dave
02:52
created

PhanJsonResultsParserTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testConversionFromString() 0 24 1
A setUp() 0 4 1
A invalidFileProvider() 0 7 1
A testInvalidFileFormat() 0 5 1
A testWithRelativePath() 0 26 1
A testInvalidJsonInput() 0 5 1
A testTypeGuesser() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\PhanJsonResultsParser;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot;
8
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\File\InvalidContentTypeException;
9
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhanJsonResultsParser\PhanJsonResultsParser;
10
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertFileContentsSameTrait;
11
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertResultMatch;
12
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\ResourceLoaderTrait;
13
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\ExpectParseExceptionWithResultTrait;
14
use PHPUnit\Framework\TestCase;
15
16
class PhanJsonResultsParserTest extends TestCase
17
{
18
    use AssertFileContentsSameTrait;
19
    use AssertResultMatch;
20
    use ExpectParseExceptionWithResultTrait;
21
    use ResourceLoaderTrait;
22
23
    /**
24
     * @var ProjectRoot
25
     */
26
    private $projectRoot;
27
28
    /**
29
     * @var PhanJsonResultsParser
30
     */
31
    private $phanJsonResultsParser;
32
33
    protected function setUp(): void
34
    {
35
        $this->projectRoot = ProjectRoot::fromProjectRoot('/vagrant/static-analysis-baseliner', '/home');
36
        $this->phanJsonResultsParser = new PhanJsonResultsParser();
37
    }
38
39
    public function testConversionFromString(): void
40
    {
41
        $fileContents = $this->getResource('phan/phan.json');
42
        $analysisResults = $this->phanJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
43
44
        $this->assertCount(2, $analysisResults->getAnalysisResults());
45
46
        $result1 = $analysisResults->getAnalysisResults()[0];
47
        $result2 = $analysisResults->getAnalysisResults()[1];
48
49
        $this->assertMatch($result1,
50
            'src/Domain/Analyser/BaseLineResultsRemover.php',
51
            16,
52
            'PhanUnreferencedUseNormal'
53
        );
54
        $this->assertSame(
55
            'NOOPError PhanUnreferencedUseNormal Possibly zero references to use statement for classlike/namespace BaseLine (\DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\BaseLine)',
56
            $result1->getMessage()
57
        );
58
59
        $this->assertMatch($result2,
60
            'src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php',
61
            107,
62
            'PhanPossiblyNullTypeArgument'
63
        );
64
    }
65
66
    public function testWithRelativePath(): void
67
    {
68
        $projectRoot = $this->projectRoot->withRelativePath('code');
69
70
        $fileContents = $this->getResource('phan/phan.json');
71
        $analysisResults = $this->phanJsonResultsParser->convertFromString($fileContents, $projectRoot);
72
73
        $this->assertCount(2, $analysisResults->getAnalysisResults());
74
75
        $result1 = $analysisResults->getAnalysisResults()[0];
76
        $result2 = $analysisResults->getAnalysisResults()[1];
77
78
        $this->assertMatch($result1,
79
            'code/src/Domain/Analyser/BaseLineResultsRemover.php',
80
            16,
81
            'PhanUnreferencedUseNormal'
82
        );
83
        $this->assertSame(
84
            'NOOPError PhanUnreferencedUseNormal Possibly zero references to use statement for classlike/namespace BaseLine (\DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\BaseLine)',
85
            $result1->getMessage()
86
        );
87
88
        $this->assertMatch($result2,
89
            'code/src/Plugins/PsalmJsonResultsParser/PsalmJsonResultsParser.php',
90
            107,
91
            'PhanPossiblyNullTypeArgument'
92
        );
93
    }
94
95
    public function testTypeGuesser(): void
96
    {
97
        $this->assertFalse($this->phanJsonResultsParser->showTypeGuessingWarning());
98
    }
99
100
    /**
101
     * @psalm-return array<int,array{string, int}>
102
     */
103
    public function invalidFileProvider(): array
104
    {
105
        return [
106
            ['phan/phan-invalid-missing-check_name.json', 1],
107
            ['phan/phan-invalid-missing-description.json', 1],
108
            ['phan/phan-invalid-missing-file.json', 1],
109
            ['phan/phan-invalid-missing-line.json', 1],
110
        ];
111
    }
112
113
    /**
114
     * @dataProvider invalidFileProvider
115
     */
116
    public function testInvalidFileFormat(string $fileName, int $resultWithIssue): void
117
    {
118
        $fileContents = $this->getResource($fileName);
119
        $this->expectParseAtLocationExceptionForResult($resultWithIssue);
120
        $this->phanJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
121
    }
122
123
    public function testInvalidJsonInput(): void
124
    {
125
        $fileContents = $this->getResource('invalid-json.json');
126
        $this->expectException(InvalidContentTypeException::class);
127
        $this->phanJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
128
    }
129
}
130