Skip to content

Commit a134f89

Browse files
committed
Files/FileList: add basic tests for the FileList::addFile() method
This is just an initial set of tests. It does not fully cover the FileList::addFile() method.
1 parent f85270c commit a134f89

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Files\FileList::addFile method.
4+
*
5+
* @copyright 2019-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+
use PHP_CodeSniffer\Tests\ConfigDouble;
13+
use PHPUnit\Framework\TestCase;
14+
use stdClass;
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 TestCase
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+
$config = new ConfigDouble();
42+
$config->filter = __DIR__.'/FilterDouble.php';
43+
$config->files = [];
44+
$ruleset = $this->getMockBuilder('PHP_CodeSniffer\Ruleset')
45+
->disableOriginalConstructor()
46+
->getMock();
47+
$this->fileList = new FileList($config, $ruleset);
48+
49+
}//end initializeFileList()
50+
51+
52+
/**
53+
* Test adding a file to the list.
54+
*
55+
* @param string $fileName The name of the file to add.
56+
* @param object|null $fileObject An optional file object to add instead of creating a new one.
57+
*
58+
* @dataProvider dataAddFile
59+
*
60+
* @return void
61+
*/
62+
public function testAddFile($fileName, $fileObject=null)
63+
{
64+
$this->assertCount(0, $this->fileList);
65+
66+
$this->fileList->addFile($fileName, $fileObject);
67+
68+
$fileListArray = iterator_to_array($this->fileList);
69+
70+
$this->assertCount(1, $this->fileList, 'File count mismatch');
71+
$this->assertArrayHasKey($fileName, $fileListArray, 'File not found in list');
72+
73+
if (isset($fileObject) === true) {
74+
$this->assertSame($fileObject, $fileListArray[$fileName], 'File object mismatch');
75+
} else {
76+
$this->assertInstanceOf(
77+
'PHP_CodeSniffer\Files\File',
78+
$fileListArray[$fileName],
79+
'File object not found in list'
80+
);
81+
}
82+
83+
}//end testAddFile()
84+
85+
86+
/**
87+
* Data provider for testAddFile.
88+
*
89+
* @return array<string, array<string>>
90+
*/
91+
public static function dataAddFile()
92+
{
93+
return [
94+
'Regular file' => ['fileName' => 'test1.php'],
95+
'STDIN' => ['fileName' => 'STDIN'],
96+
'Regular file with file object' => [
97+
'fileName' => 'test1.php',
98+
'fileObject' => new stdClass(),
99+
],
100+
];
101+
102+
}//end dataAddFile()
103+
104+
105+
}//end class

0 commit comments

Comments
 (0)