Skip to content

Commit 2e7518d

Browse files
committed
Tests/Filters: new AbstractFilterTestCase
... to contain some basic utilities and logic for use in Filter related tests. Includes switching the `Filter/AcceptTest` to use the new abstract and to use the available utilities.
1 parent 160eb6a commit 2e7518d

File tree

2 files changed

+110
-36
lines changed

2 files changed

+110
-36
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Filters\GitModified class.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2023 PHPCSStandards Contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Filters;
11+
12+
use PHP_CodeSniffer\Config;
13+
use PHP_CodeSniffer\Filters\Filter;
14+
use PHP_CodeSniffer\Ruleset;
15+
use PHPUnit\Framework\TestCase;
16+
use RecursiveIteratorIterator;
17+
18+
/**
19+
* Base functionality and utilities for testing Filter classes.
20+
*/
21+
abstract class AbstractFilterTestCase extends TestCase
22+
{
23+
24+
/**
25+
* The Config object.
26+
*
27+
* @var \PHP_CodeSniffer\Config
28+
*/
29+
protected static $config;
30+
31+
/**
32+
* The Ruleset object.
33+
*
34+
* @var \PHP_CodeSniffer\Ruleset
35+
*/
36+
protected static $ruleset;
37+
38+
39+
/**
40+
* Initialize the config and ruleset objects.
41+
*
42+
* @beforeClass
43+
*
44+
* @return void
45+
*/
46+
public static function initializeConfigAndRuleset()
47+
{
48+
self::$config = new Config(['--standard=PSR1', '--report-width=80']);
49+
self::$ruleset = new Ruleset(self::$config);
50+
51+
}//end initializeConfigAndRuleset()
52+
53+
54+
/**
55+
* Retrieve an array of files which were accepted by a filter.
56+
*
57+
* @param \PHP_CodeSniffer\Filters\Filter $filter The Filter object under test.
58+
*
59+
* @return array<string>
60+
*/
61+
protected function getFilteredResultsAsArray(Filter $filter)
62+
{
63+
$iterator = new RecursiveIteratorIterator($filter);
64+
$files = [];
65+
foreach ($iterator as $file) {
66+
$files[] = $file;
67+
}
68+
69+
return $files;
70+
71+
}//end getFilteredResultsAsArray()
72+
73+
74+
/**
75+
* Translate Linux paths to Windows paths, when necessary.
76+
*
77+
* These type of tests should be able to run and pass on both *nix as well as Windows
78+
* based dev systems. This method is a helper to allow for this.
79+
*
80+
* @param array<string|array> $paths A single or multi-dimensional array containing
81+
* file paths.
82+
*
83+
* @return array<string|array>
84+
*/
85+
protected static function mapPathsToRuntimeOs(array $paths)
86+
{
87+
if (DIRECTORY_SEPARATOR !== '\\') {
88+
return $paths;
89+
}
90+
91+
foreach ($paths as $key => $value) {
92+
if (is_string($value) === true) {
93+
$paths[$key] = strtr($value, '/', '\\\\');
94+
} else if (is_array($value) === true) {
95+
$paths[$key] = self::mapPathsToRuntimeOs($value);
96+
}
97+
}
98+
99+
return $paths;
100+
101+
}//end mapPathsToRuntimeOs()
102+
103+
104+
}//end class

tests/Core/Filters/Filter/AcceptTest.php

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,16 @@
1313
use PHP_CodeSniffer\Config;
1414
use PHP_CodeSniffer\Filters\Filter;
1515
use PHP_CodeSniffer\Ruleset;
16-
use PHPUnit\Framework\TestCase;
16+
use PHP_CodeSniffer\Tests\Core\Filters\AbstractFilterTestCase;
1717

1818
/**
1919
* Tests for the \PHP_CodeSniffer\Filters\Filter::accept method.
2020
*
2121
* @covers \PHP_CodeSniffer\Filters\Filter
2222
*/
23-
class AcceptTest extends TestCase
23+
class AcceptTest extends AbstractFilterTestCase
2424
{
2525

26-
/**
27-
* The Config object.
28-
*
29-
* @var \PHP_CodeSniffer\Config
30-
*/
31-
protected static $config;
32-
33-
/**
34-
* The Ruleset object.
35-
*
36-
* @var \PHP_CodeSniffer\Ruleset
37-
*/
38-
protected static $ruleset;
39-
4026

4127
/**
4228
* Initialize the config and ruleset objects based on the `AcceptTest.xml` ruleset file.
@@ -66,16 +52,10 @@ public static function initializeConfigAndRuleset()
6652
*/
6753
public function testExcludePatterns($inputPaths, $expectedOutput)
6854
{
69-
$fakeDI = new \RecursiveArrayIterator($inputPaths);
70-
$filter = new Filter($fakeDI, '/', self::$config, self::$ruleset);
71-
$iterator = new \RecursiveIteratorIterator($filter);
72-
$files = [];
73-
74-
foreach ($iterator as $file) {
75-
$files[] = $file;
76-
}
55+
$fakeDI = new \RecursiveArrayIterator($inputPaths);
56+
$filter = new Filter($fakeDI, '/', self::$config, self::$ruleset);
7757

78-
$this->assertEquals($expectedOutput, $files);
58+
$this->assertEquals($expectedOutput, $this->getFilteredResultsAsArray($filter));
7959

8060
}//end testExcludePatterns()
8161

@@ -121,17 +101,7 @@ public function dataExcludePatterns()
121101
];
122102

123103
// Allow these tests to work on Windows as well.
124-
if (DIRECTORY_SEPARATOR === '\\') {
125-
foreach ($testCases as $key => $case) {
126-
foreach ($case as $nr => $param) {
127-
foreach ($param as $file => $value) {
128-
$testCases[$key][$nr][$file] = strtr($value, '/', '\\');
129-
}
130-
}
131-
}
132-
}
133-
134-
return $testCases;
104+
return self::mapPathsToRuntimeOs($testCases);
135105

136106
}//end dataExcludePatterns()
137107

0 commit comments

Comments
 (0)