invalidFileProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\PhpCodeSnifferJsonResultsParser;
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\Utils\ParseAtLocationException;
11
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhpCodeSnifferJsonResultsParser\PhpCodeSnifferJsonResultsParser;
12
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertFileContentsSameTrait;
13
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\AssertResultMatch;
14
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\ResourceLoaderTrait;
15
use PHPUnit\Framework\TestCase;
16
17
final class PhpCodeSnifferJsonResultsParserTest extends TestCase
18
{
19
    use AssertFileContentsSameTrait;
20
    use AssertResultMatch;
21
    use ResourceLoaderTrait;
22
23
    /**
24
     * @var ProjectRoot
25
     */
26
    private $projectRoot;
27
28
    /**
29
     * @var PhpCodeSnifferJsonResultsParser
30
     */
31
    private $phpCodeSnifferJsonResultsParser;
32
33
    /**
34
     * @var string
35
     */
36
    private $fileContents;
37
38
    protected function setUp(): void
39
    {
40
        $this->projectRoot = ProjectRoot::fromProjectRoot('/vagrant', '/home');
41
        $this->phpCodeSnifferJsonResultsParser = new PhpCodeSnifferJsonResultsParser();
42
        $this->fileContents = $this->getResource('phpCodeSniffer/full.json');
43
    }
44
45
    public function testConversionFromString(): void
46
    {
47
        $analysisResults = $this->phpCodeSnifferJsonResultsParser->convertFromString($this->fileContents,
48
            $this->projectRoot);
49
50
        $this->assertCount(6, $analysisResults->getAnalysisResults());
51
52
        $result1 = $analysisResults->getAnalysisResults()[0];
53
        $result2 = $analysisResults->getAnalysisResults()[1];
54
        $result3 = $analysisResults->getAnalysisResults()[2];
55
        $result4 = $analysisResults->getAnalysisResults()[3];
56
        $result5 = $analysisResults->getAnalysisResults()[4];
57
        $result6 = $analysisResults->getAnalysisResults()[5];
58
59
        $this->assertMatch($result1,
60
            'src/Domain/BaseLiner/BaseLineImporter.php',
61
            8,
62
            'Generic.Files.LineLength.TooLong',
63
            Severity::warning(),
64
        );
65
66
        $this->assertMatch($result2,
67
            'src/Domain/BaseLiner/BaseLineImporter.php',
68
            52,
69
            'Squiz.WhiteSpace.FunctionSpacing.Before',
70
            Severity::error(),
71
        );
72
73
        $this->assertMatch($result3,
74
            'src/Domain/Common/InvalidPathException.php',
75
            2,
76
            'Squiz.Commenting.FileComment.Missing',
77
            Severity::error(),
78
        );
79
        $this->assertSame(
80
            'Missing file doc comment',
81
            $result3->getMessage(),
82
        );
83
84
        $this->assertMatch($result4,
85
            'src/Domain/Common/InvalidPathException.php',
86
            7,
87
            'Squiz.Commenting.ClassComment.Missing',
88
            Severity::error(),
89
        );
90
91
        $this->assertMatch($result5,
92
            'src/Domain/Common/InvalidPathException.php',
93
            9,
94
            'Squiz.Commenting.FunctionComment.Missing',
95
            Severity::error(),
96
        );
97
98
        $this->assertMatch($result6,
99
            'src/Domain/Common/InvalidPathException.php',
100
            11,
101
            'Generic.Files.LineLength.TooLong',
102
            Severity::warning(),
103
        );
104
    }
105
106
    public function testTypeGuesser(): void
107
    {
108
        $this->assertFalse($this->phpCodeSnifferJsonResultsParser->showTypeGuessingWarning());
109
    }
110
111
    public function testInvalidJsonInput(): void
112
    {
113
        $fileContents = $this->getResource('invalid-json.json');
114
        $this->expectException(InvalidContentTypeException::class);
115
        $this->phpCodeSnifferJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
116
    }
117
118
    /**
119
     * @return array<int,array{string}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
120
     */
121
    public function invalidFileProvider(): array
122
    {
123
        return [
124
            ['phpCodeSniffer/invalid-filename.json'],
125
            ['phpCodeSniffer/invalid-missing-files.json'],
126
            ['phpCodeSniffer/invalid-missing-line.json'],
127
            ['phpCodeSniffer/invalid-missing-message.json'],
128
            ['phpCodeSniffer/invalid-missing-type.json'],
129
            ['phpCodeSniffer/invalid-missing-severity.json'],
130
            ['phpCodeSniffer/invalid-severity.json'],
131
        ];
132
    }
133
134
    /**
135
     * @dataProvider invalidFileProvider
136
     */
137
    public function testInvalidFileFormat(string $fileName): void
138
    {
139
        $fileContents = $this->getResource($fileName);
140
        $this->expectException(ParseAtLocationException::class);
141
        $this->phpCodeSnifferJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
142
    }
143
}
144