Completed
Push — master ( 41ae82...437c90 )
by Dave
13s queued 11s
created

PhanJsonResultsParserTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
c 0
b 0
f 0
dl 0
loc 82
rs 10
wmc 6

6 Methods

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