Skip to content

Commit 6de24b1

Browse files
committed
Squiz/ClassFileName: expand the tests
This commit adds some extra tests for the sniff. * Document handling of unfinished OO declarations (which contain enough info to still action). * Safeguard that comments in unexpected places doesn't cause problems. As this sniff looks at the file name of the file containing the tests, we cannot use the _normal_ `SniffNameUnitTest.#.inc` naming conventions for extra tests files, so the `getTestFiles()` method from the `AbstractSniffUnitTest` class needs to be overloaded to retrieve the test case files in a slightly different way.
1 parent e506d81 commit 6de24b1

File tree

4 files changed

+101
-36
lines changed

4 files changed

+101
-36
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Intentional parse error. This should be the only test in this file.
4+
// Live coding/missing curlies should not block the sniff from functioning.
5+
6+
class Class_FileName_Live_Coding_Fail_UnitTest
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Intentional parse error. This should be the only test in this file.
4+
// Live coding/missing curlies should not block the sniff from functioning.
5+
6+
class ClassFileNameLiveCodingPassUnitTest

src/Standards/Squiz/Tests/Classes/ClassFileNameUnitTest.inc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CLASSFILENAMEUNITTEST {}
1515
interface classFileNameUnitTest {}
1616
interface classfilenameunittest {}
1717
interface CLASSFILENAMEUNITTEST {}
18-
trait classFileNameUnitTest {}
18+
trait /*comment*/ classFileNameUnitTest {}
1919
trait classfilenameunittest {}
2020
trait CLASSFILENAMEUNITTEST {}
2121
enum classFileNameUnitTest {}
@@ -39,7 +39,6 @@ trait ExtraClassFileNameUnitTest {}
3939
enum CompletelyWrongClassName {}
4040
enum ClassFileNameUnitTestExtra {}
4141
enum ClassFileNameUnitTestInc {}
42-
enum ExtraClassFileNameUnitTest {}
43-
44-
45-
?>
42+
enum
43+
// Comment
44+
ExtraClassFileNameUnitTest {}

src/Standards/Squiz/Tests/Classes/ClassFileNameUnitTest.php

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Standards\Squiz\Tests\Classes;
1111

12+
use DirectoryIterator;
1213
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1314

1415
/**
@@ -20,46 +21,99 @@ final class ClassFileNameUnitTest extends AbstractSniffUnitTest
2021
{
2122

2223

24+
/**
25+
* Get a list of all test files to check.
26+
*
27+
* These will have the same base as the sniff name but different extensions.
28+
* We ignore the .php file as it is the class.
29+
*
30+
* @param string $testFileBase The base path that the unit tests files will have.
31+
*
32+
* @return string[]
33+
*/
34+
protected function getTestFiles($testFileBase)
35+
{
36+
$testFiles = [];
37+
38+
$dir = substr($testFileBase, 0, strrpos($testFileBase, DIRECTORY_SEPARATOR));
39+
$di = new DirectoryIterator($dir);
40+
41+
// Strip off the path and the "UnitTest." suffix from the $testFileBase to allow
42+
// for some less conventionally named test case files.
43+
$fileBase = str_replace($dir, '', $testFileBase);
44+
$fileBase = substr($fileBase, 1, -9);
45+
46+
foreach ($di as $file) {
47+
$fileName = $file->getBasename('UnitTest.inc');
48+
$extension = $file->getExtension();
49+
if (substr($fileName, 0, strlen($fileBase)) === $fileBase
50+
&& $extension === 'inc'
51+
) {
52+
$testFiles[] = $file->getPathname();
53+
}
54+
}
55+
56+
// Put them in order.
57+
sort($testFiles, SORT_NATURAL);
58+
59+
return $testFiles;
60+
61+
}//end getTestFiles()
62+
63+
2364
/**
2465
* Returns the lines where errors should occur.
2566
*
2667
* The key of the array should represent the line number and the value
2768
* should represent the number of errors that should occur on that line.
2869
*
70+
* @param string $testFile The name of the file being tested.
71+
*
2972
* @return array<int, int>
3073
*/
31-
public function getErrorList()
74+
public function getErrorList($testFile='')
3275
{
33-
return [
34-
12 => 1,
35-
13 => 1,
36-
14 => 1,
37-
15 => 1,
38-
16 => 1,
39-
17 => 1,
40-
18 => 1,
41-
19 => 1,
42-
20 => 1,
43-
21 => 1,
44-
22 => 1,
45-
23 => 1,
46-
27 => 1,
47-
28 => 1,
48-
29 => 1,
49-
30 => 1,
50-
31 => 1,
51-
32 => 1,
52-
33 => 1,
53-
34 => 1,
54-
35 => 1,
55-
36 => 1,
56-
37 => 1,
57-
38 => 1,
58-
39 => 1,
59-
40 => 1,
60-
41 => 1,
61-
42 => 1,
62-
];
76+
switch ($testFile) {
77+
case 'ClassFileNameUnitTest.inc':
78+
return [
79+
12 => 1,
80+
13 => 1,
81+
14 => 1,
82+
15 => 1,
83+
16 => 1,
84+
17 => 1,
85+
18 => 1,
86+
19 => 1,
87+
20 => 1,
88+
21 => 1,
89+
22 => 1,
90+
23 => 1,
91+
27 => 1,
92+
28 => 1,
93+
29 => 1,
94+
30 => 1,
95+
31 => 1,
96+
32 => 1,
97+
33 => 1,
98+
34 => 1,
99+
35 => 1,
100+
36 => 1,
101+
37 => 1,
102+
38 => 1,
103+
39 => 1,
104+
40 => 1,
105+
41 => 1,
106+
42 => 1,
107+
];
108+
109+
case 'ClassFileNameLiveCodingFailUnitTest.inc':
110+
return [
111+
6 => 1,
112+
];
113+
114+
default:
115+
return [];
116+
}//end switch
63117

64118
}//end getErrorList()
65119

0 commit comments

Comments
 (0)