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

testConversionFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 32
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 52
rs 9.408

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\File\InvalidContentTypeException;
9
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\ParseAtLocationException;
10
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\PhpCodeSnifferJsonResultsParser\PhpCodeSnifferJsonResultsParser;
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 PhpCodeSnifferJsonResultsParserTest 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 PhpCodeSnifferJsonResultsParser
29
     */
30
    private $phpCodeSnifferJsonResultsParser;
31
32
    /**
33
     * @var string
34
     */
35
    private $fileContents;
36
37
    protected function setUp(): void
38
    {
39
        $this->projectRoot = new ProjectRoot('/vagrant', '/home');
40
        $this->phpCodeSnifferJsonResultsParser = new PhpCodeSnifferJsonResultsParser();
41
        $this->fileContents = $this->getResource('phpCodeSniffer/full.json');
42
    }
43
44
    public function testConversionFromString(): void
45
    {
46
        $analysisResults = $this->phpCodeSnifferJsonResultsParser->convertFromString($this->fileContents,
47
            $this->projectRoot);
48
49
        $this->assertCount(6, $analysisResults->getAnalysisResults());
50
51
        $result1 = $analysisResults->getAnalysisResults()[0];
52
        $result2 = $analysisResults->getAnalysisResults()[1];
53
        $result3 = $analysisResults->getAnalysisResults()[2];
54
        $result4 = $analysisResults->getAnalysisResults()[3];
55
        $result5 = $analysisResults->getAnalysisResults()[4];
56
        $result6 = $analysisResults->getAnalysisResults()[5];
57
58
        $this->assertMatch($result1,
59
            'src/Domain/BaseLiner/BaseLineImporter.php',
60
            8,
61
            'Generic.Files.LineLength.TooLong'
62
        );
63
64
        $this->assertMatch($result2,
65
            'src/Domain/BaseLiner/BaseLineImporter.php',
66
            52,
67
            'Squiz.WhiteSpace.FunctionSpacing.Before'
68
        );
69
70
        $this->assertMatch($result3,
71
            'src/Domain/Common/InvalidPathException.php',
72
            2,
73
            'Squiz.Commenting.FileComment.Missing'
74
        );
75
        $this->assertSame(
76
            'Missing file doc comment',
77
            $result3->getMessage()
78
        );
79
80
        $this->assertMatch($result4,
81
            'src/Domain/Common/InvalidPathException.php',
82
            7,
83
            'Squiz.Commenting.ClassComment.Missing'
84
        );
85
86
        $this->assertMatch($result5,
87
            'src/Domain/Common/InvalidPathException.php',
88
            9,
89
            'Squiz.Commenting.FunctionComment.Missing'
90
        );
91
92
        $this->assertMatch($result6,
93
            'src/Domain/Common/InvalidPathException.php',
94
            11,
95
            'Generic.Files.LineLength.TooLong'
96
        );
97
    }
98
99
    public function testTypeGuesser(): void
100
    {
101
        $this->assertFalse($this->phpCodeSnifferJsonResultsParser->showTypeGuessingWarning());
102
    }
103
104
    public function testInvalidJsonInput(): void
105
    {
106
        $fileContents = $this->getResource('invalid-json.json');
107
        $this->expectException(InvalidContentTypeException::class);
108
        $this->phpCodeSnifferJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
109
    }
110
111
    /**
112
     * @psalm-return array<int,array{string}>
113
     */
114
    public function invalidFileProvider(): array
115
    {
116
        return [
117
            ['phpCodeSniffer/invalid-filename.json'],
118
            ['phpCodeSniffer/invalid-missing-files.json'],
119
            ['phpCodeSniffer/invalid-missing-line.json'],
120
            ['phpCodeSniffer/invalid-missing-message.json'],
121
            ['phpCodeSniffer/invalid-missing-type.json'],
122
        ];
123
    }
124
125
    /**
126
     * @dataProvider invalidFileProvider
127
     */
128
    public function testInvalidFileFormat(string $fileName): void
129
    {
130
        $fileContents = $this->getResource($fileName);
131
        $this->expectException(ParseAtLocationException::class);
132
        $this->phpCodeSnifferJsonResultsParser->convertFromString($fileContents, $this->projectRoot);
133
    }
134
}
135