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