Passed
Push — master ( b371b3...259207 )
by Michael
02:50 queued 15s
created

AbstractMo4SniffUnitTest::getErrorList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the mo4-coding-standard (phpcs standard)
5
 *
6
 * @author  Xaver Loppenstedt <[email protected]>
7
 *
8
 * @license http://spdx.org/licenses/MIT MIT License
9
 *
10
 * @link    https://github.com/mayflower/mo4-coding-standard
11
 */
12
13
declare(strict_types=1);
14
15
namespace MO4\Tests;
16
17
use PHP_CodeSniffer\Exceptions\RuntimeException;
18
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
19
20
/**
21
 * Abstract class to make the writing of tests more convenient.
22
 *
23
 * A sniff unit test checks a .inc file for expected violations of a single
24
 * coding standard.
25
 *
26
 * Expected errors and warnings are stored in the class fields $expectedErrorList
27
 * and $expectedWarningList
28
 *
29
 * @author    Xaver Loppenstedt <[email protected]>
30
 *
31
 * @copyright 2013-2021 Xaver Loppenstedt, some rights reserved.
32
 *
33
 * @license   http://spdx.org/licenses/MIT MIT License
34
 *
35
 * @link      https://github.com/mayflower/mo4-coding-standard
36
 */
37
abstract class AbstractMo4SniffUnitTest extends AbstractSniffUnitTest
38
{
39
    /**
40
     * Array or Array containing the test file as key and as value the key-value pairs with line number and number of#
41
     * errors as describe in @see AbstractSniffUnitTest::getErrorList
42
     *
43
     * When the array is empty, the test will pass.
44
     *
45
     * @var array
46
     */
47
    protected $expectedErrorList = [];
48
49
    /**
50
     * Array or Array containing the test file as key and as value the key-value pairs with line number and number of#
51
     * errors as describe in @see AbstractSniffUnitTest::getWarningList
52
     *
53
     * When the array is empty, the test will pass.
54
     *
55
     * @var array
56
     */
57
    protected $expectedWarningList = [];
58
59
    /**
60
     * Returns the lines where errors should occur.
61
     *
62
     * The key of the array should represent the line number and the value
63
     * should represent the number of errors that should occur on that line.
64
     *
65
     * @param string $testFile test file
66
     *
67
     * @return array<int, int>
68
     *
69
     * @throws RuntimeException
70
     */
71
    protected function getErrorList(string $testFile = ''): array
72
    {
73
        return $this->getRecordForTestFile($testFile, $this->expectedErrorList);
74
    }
75
76
    /**
77
     * Returns the lines where warnings should occur.
78
     *
79
     * The key of the array should represent the line number and the value
80
     * should represent the number of warnings that should occur on that line.
81
     *
82
     * @param string $testFile test file
83
     *
84
     * @return array<int, int>
85
     *
86
     * @throws RuntimeException
87
     */
88
    protected function getWarningList(string $testFile = ''): array
89
    {
90
        return $this->getRecordForTestFile($testFile, $this->expectedWarningList);
91
    }
92
93
    /**
94
     * Returns the lines where warnings should occur for the error or warning list.
95
     *
96
     * The key of the array should represent the line number and the value
97
     * should represent the number of warnings that should occur on that line.
98
     *
99
     * @param string $testFile test file
100
     * @param array  $list     record for test file
101
     *
102
     * @return array<int, int>
103
     *
104
     * @throws RuntimeException
105
     */
106
    private function getRecordForTestFile(string $testFile, array $list): array
107
    {
108
        if ([] === $list) {
109
            return [];
110
        }
111
112
        if (!\array_key_exists($testFile, $list)) {
113
            throw new RuntimeException(
114
                \sprintf('%s%s is not handled by %s', \sprintf('Testfile %s in ', $testFile), __DIR__, self::class)
115
            );
116
        }
117
118
        return $list[$testFile];
119
    }
120
}
121