Skip to content

Commit 0e289be

Browse files
committed
Files/FileList: fix tests to stop interfering with external standards tests
The AddFileTest.php tests introduced an issue that broke the ability to run sniff tests for external standards using the PHPCS native test framework. This problem was happening because `self::initializeConfigAndRuleset()` was called in a data provider method in `AddFileTest.php`. This means that the `Config` instance created inside `initializeConfigAndRuleset()` using `ConfigDouble` was created before `AllSniffs::suite()` had a chance to get the installed standards from the `CodeSniffer.conf` configuration file. When `AllSniffs::suite()` runs, `ConfigDouble` already overrode the `configData` and `configDataFile` properties, and `CodeSniffer.conf` is never read, and the tests for external standards were not included. To fix this problem, I opted to change the test to create an instance of the `File` class (which requires an instance of the `Config` class) in the test itself instead of doing that in the data provider.
1 parent a12ddb0 commit 0e289be

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

tests/Core/Files/FileList/AddFileTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,21 @@ protected function initializeFileList()
4747
/**
4848
* Test adding a file to the list.
4949
*
50-
* @param string $fileName The name of the file to add.
51-
* @param object|null $fileObject An optional file object to add instead of creating a new one.
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.
5252
*
5353
* @dataProvider dataAddFile
5454
*
5555
* @return void
5656
*/
57-
public function testAddFile($fileName, $fileObject=null)
57+
public function testAddFile($fileName, $passFileObject=false)
5858
{
59+
$fileObject = null;
60+
61+
if ($passFileObject === true) {
62+
$fileObject = new File($fileName, self::$ruleset, self::$config);
63+
}
64+
5965
$this->assertCount(0, $this->fileList);
6066

6167
$this->fileList->addFile($fileName, $fileObject);
@@ -65,7 +71,7 @@ public function testAddFile($fileName, $fileObject=null)
6571
$this->assertCount(1, $this->fileList, 'File count mismatch');
6672
$this->assertArrayHasKey($fileName, $fileListArray, 'File not found in list');
6773

68-
if (isset($fileObject) === true) {
74+
if ($fileObject instanceof File) {
6975
$this->assertSame($fileObject, $fileListArray[$fileName], 'File object mismatch');
7076
} else {
7177
$this->assertInstanceOf(
@@ -81,12 +87,10 @@ public function testAddFile($fileName, $fileObject=null)
8187
/**
8288
* Data provider for testAddFile.
8389
*
84-
* @return array<string, array<string, string|object>>
90+
* @return array<string, array<string, string|bool>>
8591
*/
8692
public static function dataAddFile()
8793
{
88-
self::initializeConfigAndRuleset();
89-
9094
return [
9195
'Regular file' => [
9296
'fileName' => 'test1.php',
@@ -95,8 +99,8 @@ public static function dataAddFile()
9599
'fileName' => 'STDIN',
96100
],
97101
'Regular file with file object' => [
98-
'fileName' => 'test1.php',
99-
'fileObject' => new File('test1.php', self::$ruleset, self::$config),
102+
'fileName' => 'test1.php',
103+
'passFileObject' => true,
100104
],
101105
];
102106

0 commit comments

Comments
 (0)