InvalidPhpmdFileFormatTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 14
c 1
b 1
f 0
dl 0
loc 49
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A filenameDataProvider() 0 14 1
A assertExceptionThrown() 0 8 1
A testNotJsonFileSupplied() 0 3 1
A testInvalidFileFormat() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Plugins\ResultsParsers\PhpmdJsonResultsParser;
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\PhpmdJsonResultsParser\PhpmdJsonResultsParser;
11
use DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Helpers\ResourceLoaderTrait;
12
use PHPUnit\Framework\TestCase;
13
14
final class InvalidPhpmdFileFormatTest extends TestCase
15
{
16
    use ResourceLoaderTrait;
17
18
    /**
19
     * @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...
20
     */
21
    public function filenameDataProvider(): array
22
    {
23
        return [
24
            [
25
                'phpmd_missing_file.json',
26
            ],
27
            [
28
                'phpmd_missing_files.json',
29
            ],
30
            [
31
                'phpmd_missing_violations.json',
32
            ],
33
            [
34
                'phpmd_invalid_violation.json',
35
            ],
36
        ];
37
    }
38
39
    /**
40
     * @dataProvider filenameDataProvider
41
     */
42
    public function testInvalidFileFormat(string $fileName): void
43
    {
44
        $this->assertExceptionThrown(ParseAtLocationException::class, $fileName);
45
    }
46
47
    public function testNotJsonFileSupplied(): void
48
    {
49
        $this->assertExceptionThrown(InvalidContentTypeException::class, 'not.json');
50
    }
51
52
    /**
53
     * @param class-string<\Throwable> $exceptionType
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Throwable> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Throwable>.
Loading history...
54
     */
55
    private function assertExceptionThrown(string $exceptionType, string $fileName): void
56
    {
57
        $fileContents = $this->getResource("phpmd/$fileName");
58
        $projectRoot = ProjectRoot::fromProjectRoot('/vagrant/static-analysis-baseliner', '/home');
59
        $phpmdResultsParser = new PhpmdJsonResultsParser();
60
61
        $this->expectException($exceptionType);
62
        $phpmdResultsParser->convertFromString($fileContents, $projectRoot);
63
    }
64
}
65