|
| 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 |
0 commit comments