Skip to content

Commit 9d00e0e

Browse files
authored
Merge pull request #1262 from rodrigoprimo/restore-file-list-tests
Files/FileList: restore and fix tests
2 parents 8222a52 + 0e289be commit 9d00e0e

File tree

6 files changed

+356
-0
lines changed

6 files changed

+356
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Abstract testcase class for testing FileList methods.
4+
*
5+
* @copyright 2025 PHPCSStandards Contributors
6+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Tests\Core\Files\FileList;
10+
11+
use PHP_CodeSniffer\Ruleset;
12+
use PHP_CodeSniffer\Tests\ConfigDouble;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Base functionality and utilities for testing FileList methods.
17+
*/
18+
abstract class AbstractFileListTestCase extends TestCase
19+
{
20+
21+
/**
22+
* The Config object.
23+
*
24+
* @var \PHP_CodeSniffer\Config
25+
*/
26+
protected static $config;
27+
28+
/**
29+
* The Ruleset object.
30+
*
31+
* @var \PHP_CodeSniffer\Ruleset
32+
*/
33+
protected static $ruleset;
34+
35+
36+
/**
37+
* Initialize the config and ruleset objects only once.
38+
*
39+
* @beforeClass
40+
*
41+
* @return void
42+
*/
43+
public static function initializeConfigAndRuleset()
44+
{
45+
// Wrapped in an `isset()` as the properties may have been set already (via a call to this method from a dataprovider).
46+
if (isset(self::$ruleset) === false) {
47+
self::$config = new ConfigDouble();
48+
self::$config->filter = __DIR__.'/FilterDouble.php';
49+
self::$ruleset = new Ruleset(self::$config);
50+
}
51+
52+
}//end initializeConfigAndRuleset()
53+
54+
55+
}//end class
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Files\FileList::addFile method.
4+
*
5+
* @copyright 2025 PHPCSStandards Contributors
6+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Tests\Core\Files\FileList;
10+
11+
use PHP_CodeSniffer\Files\File;
12+
use PHP_CodeSniffer\Files\FileList;
13+
use PHP_CodeSniffer\Ruleset;
14+
use PHP_CodeSniffer\Tests\ConfigDouble;
15+
16+
/**
17+
* Tests for the \PHP_CodeSniffer\Files\FileList::addFile method.
18+
*
19+
* @covers \PHP_CodeSniffer\Files\FileList::addFile
20+
*/
21+
final class AddFileTest extends AbstractFileListTestCase
22+
{
23+
24+
/**
25+
* The FileList object.
26+
*
27+
* @var \PHP_CodeSniffer\Files\FileList
28+
*/
29+
private $fileList;
30+
31+
32+
/**
33+
* Initialize the FileList object.
34+
*
35+
* @before
36+
*
37+
* @return void
38+
*/
39+
protected function initializeFileList()
40+
{
41+
self::$config->files = [];
42+
$this->fileList = new FileList(self::$config, self::$ruleset);
43+
44+
}//end initializeFileList()
45+
46+
47+
/**
48+
* Test adding a file to the list.
49+
*
50+
* @param string $fileName The name of the file to add.
51+
* @param bool $passFileObject Whether to pass a File object to addFile() or not.
52+
*
53+
* @dataProvider dataAddFile
54+
*
55+
* @return void
56+
*/
57+
public function testAddFile($fileName, $passFileObject=false)
58+
{
59+
$fileObject = null;
60+
61+
if ($passFileObject === true) {
62+
$fileObject = new File($fileName, self::$ruleset, self::$config);
63+
}
64+
65+
$this->assertCount(0, $this->fileList);
66+
67+
$this->fileList->addFile($fileName, $fileObject);
68+
69+
$fileListArray = iterator_to_array($this->fileList);
70+
71+
$this->assertCount(1, $this->fileList, 'File count mismatch');
72+
$this->assertArrayHasKey($fileName, $fileListArray, 'File not found in list');
73+
74+
if ($fileObject instanceof File) {
75+
$this->assertSame($fileObject, $fileListArray[$fileName], 'File object mismatch');
76+
} else {
77+
$this->assertInstanceOf(
78+
'PHP_CodeSniffer\Files\File',
79+
$fileListArray[$fileName],
80+
'File object not found in list'
81+
);
82+
}
83+
84+
}//end testAddFile()
85+
86+
87+
/**
88+
* Data provider for testAddFile.
89+
*
90+
* @return array<string, array<string, string|bool>>
91+
*/
92+
public static function dataAddFile()
93+
{
94+
return [
95+
'Regular file' => [
96+
'fileName' => 'test1.php',
97+
],
98+
'STDIN' => [
99+
'fileName' => 'STDIN',
100+
],
101+
'Regular file with file object' => [
102+
'fileName' => 'test1.php',
103+
'passFileObject' => true,
104+
],
105+
];
106+
107+
}//end dataAddFile()
108+
109+
110+
/**
111+
* Test that it is not possible to add the same file twice.
112+
*
113+
* @return void
114+
*/
115+
public function testAddFileShouldNotAddTheSameFileTwice()
116+
{
117+
$file1 = 'test1.php';
118+
$file2 = 'test2.php';
119+
$expectedFiles = [
120+
$file1,
121+
$file2,
122+
];
123+
124+
// Add $file1 once.
125+
$this->fileList->addFile($file1);
126+
$this->assertCount(1, $this->fileList);
127+
$this->assertSame([$file1], array_keys(iterator_to_array($this->fileList)));
128+
129+
// Try to add $file1 again. Should be ignored.
130+
$this->fileList->addFile($file1);
131+
$this->assertCount(1, $this->fileList);
132+
$this->assertSame([$file1], array_keys(iterator_to_array($this->fileList)));
133+
134+
// Add $file2. Should be added.
135+
$this->fileList->addFile($file2);
136+
$this->assertCount(2, $this->fileList);
137+
$this->assertSame($expectedFiles, array_keys(iterator_to_array($this->fileList)));
138+
139+
}//end testAddFileShouldNotAddTheSameFileTwice()
140+
141+
142+
}//end class
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Files\FileList::__construct method.
4+
*
5+
* @copyright 2025 PHPCSStandards Contributors
6+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Tests\Core\Files\FileList;
10+
11+
use PHP_CodeSniffer\Files\FileList;
12+
13+
/**
14+
* Tests for the \PHP_CodeSniffer\Files\FileList::__construct method.
15+
*
16+
* @covers \PHP_CodeSniffer\Files\FileList::__construct
17+
*/
18+
final class ConstructTest extends AbstractFileListTestCase
19+
{
20+
21+
22+
/**
23+
* Test the __construct() method.
24+
*
25+
* @param array<string> $files List of file paths in the Config class.
26+
* @param array<string> $expectedFiles List of expected file paths in the FileList.
27+
*
28+
* @dataProvider dataConstruct
29+
*
30+
* @return void
31+
*/
32+
public function testConstruct($files, $expectedFiles)
33+
{
34+
self::$config->files = $files;
35+
36+
$fileList = new FileList(self::$config, self::$ruleset);
37+
38+
$this->assertSame(self::$config, $fileList->config, 'Config object mismatch');
39+
$this->assertSame(self::$ruleset, $fileList->ruleset, 'Ruleset object mismatch');
40+
41+
$this->assertCount(count($expectedFiles), $fileList, 'File count mismatch');
42+
43+
$i = 0;
44+
45+
// Sort the values to make the tests stable as different OSes will read directories
46+
// in a different order and the order is not relevant for these tests. Just the values.
47+
$fileListArray = iterator_to_array($fileList);
48+
ksort($fileListArray);
49+
50+
foreach ($fileListArray as $filePath => $fileObject) {
51+
$this->assertSame(
52+
$expectedFiles[$i],
53+
$filePath,
54+
sprintf('File path mismatch: expected "%s", got "%s"', $expectedFiles[$i], $filePath)
55+
);
56+
$this->assertInstanceOf(
57+
'PHP_CodeSniffer\Files\File',
58+
$fileObject,
59+
sprintf('File object for "%s" is not an instance of PHP_CodeSniffer\Files\File', $filePath)
60+
);
61+
$i++;
62+
}
63+
64+
}//end testConstruct()
65+
66+
67+
/**
68+
* Data provider for testConstruct.
69+
*
70+
* @return array<string, array<string, array<string>>>
71+
*/
72+
public static function dataConstruct()
73+
{
74+
$fixturesDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
75+
76+
return [
77+
'No files' => [
78+
'files' => [],
79+
'expectedFiles' => [],
80+
],
81+
'Two files' => [
82+
'files' => [
83+
'file1.php',
84+
'file2.php',
85+
],
86+
'expectedFiles' => [
87+
'file1.php',
88+
'file2.php',
89+
],
90+
],
91+
'A directory' => [
92+
'files' => [$fixturesDir],
93+
'expectedFiles' => [
94+
$fixturesDir.'file1.php',
95+
$fixturesDir.'file2.php',
96+
],
97+
],
98+
'Same file twice' => [
99+
'files' => [
100+
'file1.php',
101+
'file1.php',
102+
],
103+
'expectedFiles' => [
104+
'file1.php',
105+
],
106+
],
107+
'File and then directory containing that file' => [
108+
'files' => [
109+
$fixturesDir.'file1.php',
110+
$fixturesDir,
111+
],
112+
'expectedFiles' => [
113+
$fixturesDir.'file1.php',
114+
$fixturesDir.'file2.php',
115+
],
116+
],
117+
];
118+
119+
}//end dataConstruct()
120+
121+
122+
}//end class
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Double of the filter class that will accept every file. Used in the FileList tests.
4+
*
5+
* @copyright 2025 PHPCSStandards Contributors
6+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Tests\Core\Files\FileList;
10+
11+
use PHP_CodeSniffer\Filters\Filter;
12+
use ReturnTypeWillChange;
13+
14+
final class FilterDouble extends Filter
15+
{
16+
17+
18+
/**
19+
* Accepts every file.
20+
*
21+
* @return true
22+
*/
23+
#[ReturnTypeWillChange]
24+
public function accept()
25+
{
26+
return true;
27+
28+
}//end accept()
29+
30+
31+
}//end class
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// Empty file for testing purposes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// Empty file for testing purposes.

0 commit comments

Comments
 (0)