assertExceptionThrown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
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