Skip to content

Commit 2592077

Browse files
Xaver Lopenstedtmmoll
authored andcommitted
Refactor tests by introducting AbstractMo4SniffUnitTest
1 parent b371b3d commit 2592077

8 files changed

+288
-383
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
44
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
55

6+
## [8.0.0] -
7+
### Changed
8+
- refactored tests
9+
610
## [7.0.0] - 2021-04-22
711
### Added
812
- Add PHP 8.0 support
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}

MO4/Tests/Arrays/ArrayDoubleArrowAlignmentUnitTest.php

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
namespace MO4\Tests\Arrays;
1616

17-
use PHP_CodeSniffer\Exceptions\RuntimeException;
18-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
17+
use MO4\Tests\AbstractMo4SniffUnitTest;
1918

2019
/**
2120
* Unit test class for @see ArrayDoubleArrowAlignmentSniff
@@ -25,84 +24,50 @@
2524
*
2625
* @author Xaver Loppenstedt <[email protected]>
2726
*
28-
* @copyright 2013-2017 Xaver Loppenstedt, some rights reserved.
27+
* @copyright 2013-2021 Xaver Loppenstedt, some rights reserved.
2928
*
3029
* @license http://spdx.org/licenses/MIT MIT License
3130
*
3231
* @link https://github.com/mayflower/mo4-coding-standard
3332
*/
34-
class ArrayDoubleArrowAlignmentUnitTest extends AbstractSniffUnitTest
33+
class ArrayDoubleArrowAlignmentUnitTest extends AbstractMo4SniffUnitTest
3534
{
36-
/**
37-
* Returns the lines where errors should occur.
38-
*
39-
* The key of the array should represent the line number and the value
40-
* should represent the number of errors that should occur on that line.
41-
*
42-
* @param string $testFile test file
43-
*
44-
* @return array<int, int>
45-
*
46-
* @throws RuntimeException
47-
*/
48-
protected function getErrorList(string $testFile = ''): array
49-
{
50-
switch ($testFile) {
51-
case 'ArrayDoubleArrowAlignmentUnitTest.pass.inc':
52-
return [];
53-
case 'ArrayDoubleArrowAlignmentUnitTest.fail.inc':
54-
return [
55-
5 => 1,
56-
10 => 1,
57-
17 => 2,
58-
18 => 2,
59-
22 => 1,
60-
28 => 1,
61-
38 => 1,
62-
43 => 1,
63-
45 => 1,
64-
49 => 1,
65-
51 => 1,
66-
58 => 1,
67-
59 => 1,
68-
61 => 1,
69-
67 => 1,
70-
70 => 1,
71-
71 => 1,
72-
73 => 1,
73-
82 => 1,
74-
83 => 1,
75-
85 => 1,
76-
93 => 1,
77-
94 => 1,
78-
97 => 1,
79-
105 => 1,
80-
130 => 1,
81-
132 => 1,
82-
134 => 1,
83-
136 => 2,
84-
140 => 1,
85-
141 => 1,
86-
145 => 2,
87-
149 => 1,
88-
];
89-
}
90-
91-
throw new RuntimeException(
92-
\sprintf('%s%s is not handled by %s', \sprintf('Testfile %s in ', $testFile), __DIR__, self::class)
93-
);
94-
}
95-
96-
/**
97-
* Returns the lines where warnings should occur.
98-
*
99-
* The key of the array should represent the line number and the value
100-
* should represent the number of warnings that should occur on that line.
101-
*
102-
* @return array<int, int>
103-
*/
104-
protected function getWarningList(): array
105-
{
106-
return [];
107-
}
35+
protected $expectedErrorList = [
36+
'ArrayDoubleArrowAlignmentUnitTest.pass.inc' => [],
37+
'ArrayDoubleArrowAlignmentUnitTest.fail.inc' => [
38+
5 => 1,
39+
10 => 1,
40+
17 => 2,
41+
18 => 2,
42+
22 => 1,
43+
28 => 1,
44+
38 => 1,
45+
43 => 1,
46+
45 => 1,
47+
49 => 1,
48+
51 => 1,
49+
58 => 1,
50+
59 => 1,
51+
61 => 1,
52+
67 => 1,
53+
70 => 1,
54+
71 => 1,
55+
73 => 1,
56+
82 => 1,
57+
83 => 1,
58+
85 => 1,
59+
93 => 1,
60+
94 => 1,
61+
97 => 1,
62+
105 => 1,
63+
130 => 1,
64+
132 => 1,
65+
134 => 1,
66+
136 => 2,
67+
140 => 1,
68+
141 => 1,
69+
145 => 2,
70+
149 => 1,
71+
],
72+
];
10873
}

MO4/Tests/Arrays/MultiLineArrayUnitTest.php

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
namespace MO4\Tests\Arrays;
1616

17-
use PHP_CodeSniffer\Exceptions\RuntimeException;
18-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
17+
use MO4\Tests\AbstractMo4SniffUnitTest;
1918

2019
/**
2120
* Unit test class for @see MultiLineArraySniff
@@ -25,58 +24,24 @@
2524
*
2625
* @author Xaver Loppenstedt <[email protected]>
2726
*
28-
* @copyright 2013-2017 Xaver Loppenstedt, some rights reserved.
27+
* @copyright 2013-2021 Xaver Loppenstedt, some rights reserved.
2928
*
3029
* @license http://spdx.org/licenses/MIT MIT License
3130
*
3231
* @link https://github.com/mayflower/mo4-coding-standard
3332
*/
34-
class MultiLineArrayUnitTest extends AbstractSniffUnitTest
33+
class MultiLineArrayUnitTest extends AbstractMo4SniffUnitTest
3534
{
36-
/**
37-
* Returns the lines where errors should occur.
38-
*
39-
* The key of the array should represent the line number and the value
40-
* should represent the number of errors that should occur on that line.
41-
*
42-
* @param string $testFile test file
43-
*
44-
* @return array<int, int>
45-
*
46-
* @throws RuntimeException
47-
*/
48-
protected function getErrorList(string $testFile = ''): array
49-
{
50-
switch ($testFile) {
51-
case 'MultiLineArrayUnitTest.pass.inc':
52-
return [];
53-
case 'MultiLineArrayUnitTest.fail.inc':
54-
return [
55-
4 => 1,
56-
12 => 1,
57-
18 => 2,
58-
22 => 1,
59-
24 => 1,
60-
28 => 1,
61-
32 => 1,
62-
];
63-
}
64-
65-
throw new RuntimeException(
66-
\sprintf('%s%s is not handled by %s', \sprintf('Testfile %s in ', $testFile), __DIR__, self::class)
67-
);
68-
}
69-
70-
/**
71-
* Returns the lines where warnings should occur.
72-
*
73-
* The key of the array should represent the line number and the value
74-
* should represent the number of warnings that should occur on that line.
75-
*
76-
* @return array<int, int>
77-
*/
78-
protected function getWarningList(): array
79-
{
80-
return [];
81-
}
35+
protected $expectedErrorList = [
36+
'MultiLineArrayUnitTest.pass.inc' => [],
37+
'MultiLineArrayUnitTest.fail.inc' => [
38+
4 => 1,
39+
12 => 1,
40+
18 => 2,
41+
22 => 1,
42+
24 => 1,
43+
28 => 1,
44+
32 => 1,
45+
],
46+
];
8247
}

0 commit comments

Comments
 (0)