Passed
Pull Request — master (#184)
by Xaver
02:49
created

AbstractMo4SniffUnitTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 74
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorList() 0 13 3
A getWarningList() 0 13 3
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
        if ([] === $this->expectedErrorList) {
74
            return [];
75
        }
76
77
        if (!\array_key_exists($testFile, $this->expectedErrorList)) {
78
            throw new RuntimeException(
79
                \sprintf('%s%s is not handled by %s', \sprintf('Testfile %s in ', $testFile), __DIR__, self::class)
80
            );
81
        }
82
83
        return $this->expectedErrorList[$testFile];
84
    }
85
86
    /**
87
     * Returns the lines where warnings should occur.
88
     *
89
     * The key of the array should represent the line number and the value
90
     * should represent the number of warnings that should occur on that line.
91
     *
92
     * @param string $testFile test file
93
     *
94
     * @return array<int, int>
95
     *
96
     * @throws RuntimeException
97
     */
98
    protected function getWarningList(string $testFile = ''): array
99
    {
100
        if ([] === $this->expectedWarningList) {
101
            return [];
102
        }
103
104
        if (!\array_key_exists($testFile, $this->expectedWarningList)) {
105
            throw new RuntimeException(
106
                \sprintf('%s%s is not handled by %s', \sprintf('Testfile %s in ', $testFile), __DIR__, self::class)
107
            );
108
        }
109
110
        return $this->expectedWarningList[$testFile];
111
    }
112
}
113