| Total Complexity | 13 |
| Total Lines | 201 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 66 | #[CoversClass(Filesystem::class)] |
||
| 67 | class FilesystemTest extends TestCase |
||
| 68 | { |
||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected static string $testDir; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array<string> |
||
| 76 | */ |
||
| 77 | protected static array $testFiles; |
||
| 78 | |||
| 79 | #[\Override] |
||
| 80 | public static function setUpBeforeClass(): void |
||
| 81 | { |
||
| 82 | self::$testDir = __DIR__ . DIRECTORY_SEPARATOR . 'dir1'; |
||
| 83 | self::$testFiles = [ |
||
| 84 | 'file1' => self::$testDir . DIRECTORY_SEPARATOR . 'file1', |
||
| 85 | 'file2' => self::$testDir . DIRECTORY_SEPARATOR . 'file2', |
||
| 86 | 'file3' => self::$testDir . DIRECTORY_SEPARATOR . 'file3.txt', |
||
| 87 | ]; |
||
| 88 | |||
| 89 | if (!is_dir(self::$testDir)) { |
||
| 90 | mkdir(self::$testDir); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (!file_exists(self::$testFiles['file1'])) { |
||
| 94 | touch(self::$testFiles['file1']); |
||
| 95 | } |
||
| 96 | |||
| 97 | if (!file_exists(self::$testFiles['file2'])) { |
||
| 98 | touch(self::$testFiles['file2']); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (!file_exists(self::$testFiles['file3'])) { |
||
| 102 | touch(self::$testFiles['file3']); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | #[\Override] |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Test Filesystem::lineCounter(). |
||
| 121 | */ |
||
| 122 | public function testLineCounter(): void |
||
| 123 | { |
||
| 124 | Filesystem::fileWrite(self::$testFiles['file1'], "This\nis\na\nnew\nline.\n"); |
||
| 125 | self::assertSame(5, array_sum(Filesystem::lineCounter(directory: self::$testDir, onlyLineCount: true))); |
||
| 126 | self::assertSame(0, array_sum(Filesystem::lineCounter(directory: self::$testDir, ignore: ['dir1'], onlyLineCount: true))); |
||
| 127 | self::assertSame(0, array_sum(Filesystem::lineCounter(self::$testDir, extensions: ['.txt'], onlyLineCount: true))); |
||
| 128 | |||
| 129 | self::assertSame([ |
||
| 130 | self::$testDir => [ |
||
| 131 | 'file1' => 5, |
||
| 132 | 'file2' => 0, |
||
| 133 | 'file3.txt' => 0, |
||
| 134 | ], |
||
| 135 | ], Filesystem::lineCounter(directory: self::$testDir)); |
||
| 136 | |||
| 137 | self::assertSame([], Filesystem::lineCounter(directory: self::$testDir, ignore: ['dir1'])); |
||
| 138 | self::assertSame([], Filesystem::lineCounter(self::$testDir, extensions: ['.txt'])); |
||
| 139 | |||
| 140 | Filesystem::fileWrite(self::$testFiles['file1']); |
||
| 141 | |||
| 142 | self::expectException(InvalidArgumentException::class); |
||
|
1 ignored issue
–
show
|
|||
| 143 | $count = array_sum(Filesystem::lineCounter('/this/should/not/exist', onlyLineCount: true)); |
||
| 144 | $count = count(Filesystem::lineCounter('/this/should/not/exist')); |
||
| 145 | |||
| 146 | $count = array_sum(Filesystem::lineCounter('/this/should/not/exist', ignore: ['dir1'], onlyLineCount: true)); |
||
| 147 | $count = count(Filesystem::lineCounter('/this/should/not/exist', ignore: ['dir1'])); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Test Filesystem::directorySize(). |
||
| 152 | */ |
||
| 153 | public function testDirectorySize(): void |
||
| 154 | { |
||
| 155 | Filesystem::fileWrite(self::$testFiles['file1'], '1234567890'); |
||
| 156 | Filesystem::fileWrite(self::$testFiles['file2'], implode('', range('a', 'z'))); |
||
| 157 | |||
| 158 | self::assertSame(10 + 26, Filesystem::directorySize(self::$testDir)); |
||
| 159 | self::assertSame(0, Filesystem::directorySize(self::$testDir, ['dir1'])); |
||
| 160 | |||
| 161 | Filesystem::fileWrite(self::$testFiles['file1']); |
||
| 162 | Filesystem::fileWrite(self::$testFiles['file2']); |
||
| 163 | |||
| 164 | self::expectException(InvalidArgumentException::class); |
||
|
1 ignored issue
–
show
|
|||
| 165 | $count = Filesystem::directorySize('/this/should/not/exist'); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Test Filesystem::directoryList(). |
||
| 170 | */ |
||
| 171 | public function testDirectoryList(): void |
||
| 172 | { |
||
| 173 | Filesystem::fileWrite(self::$testFiles['file1'], '1234567890'); |
||
| 174 | Filesystem::fileWrite(self::$testFiles['file2'], implode('', range('a', 'z'))); |
||
| 175 | |||
| 176 | $expected = [ |
||
| 177 | 0 => self::$testFiles['file1'], |
||
| 178 | 1 => self::$testFiles['file2'], |
||
| 179 | ]; |
||
| 180 | natsort($expected); |
||
| 181 | |||
| 182 | $actual = Filesystem::directoryList(self::$testDir); |
||
| 183 | natsort($actual); |
||
| 184 | |||
| 185 | self::assertSame([], array_diff($expected, $actual)); |
||
| 186 | self::assertSame([], Filesystem::directoryList(self::$testDir, ['dir1'])); |
||
| 187 | |||
| 188 | Filesystem::fileWrite(self::$testFiles['file1']); |
||
| 189 | Filesystem::fileWrite(self::$testFiles['file2']); |
||
| 190 | |||
| 191 | self::expectException(InvalidArgumentException::class); |
||
|
1 ignored issue
–
show
|
|||
| 192 | $count = Filesystem::directoryList('/this/should/not/exist'); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Test Filesystem::normalizeFilePath(). |
||
| 197 | */ |
||
| 198 | public function testNormalizeFilePath(): void |
||
| 199 | { |
||
| 200 | $separator = DIRECTORY_SEPARATOR; |
||
| 201 | |||
| 202 | $path1 = __DIR__ . $separator . 'dir1' . $separator . 'file1'; |
||
| 203 | self::assertSame($path1, Filesystem::normalizeFilePath($path1)); |
||
| 204 | |||
| 205 | $path2 = $path1 . $separator; |
||
| 206 | self::assertSame($path1, Filesystem::normalizeFilePath($path2)); |
||
| 207 | |||
| 208 | $path3 = str_replace($separator, '\\//', $path2); |
||
| 209 | self::assertSame($path1, Filesystem::normalizeFilePath($path3)); |
||
| 210 | |||
| 211 | $path4 = $path2 . '..' . $separator; |
||
| 212 | self::assertSame(str_replace($separator . 'file1', '', $path1), Filesystem::normalizeFilePath($path4)); |
||
| 213 | |||
| 214 | $path5 = $path4 . '..'; |
||
| 215 | self::assertSame(str_replace($separator . 'dir1' . $separator . 'file1', '', $path1), Filesystem::normalizeFilePath($path5)); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Test Filesystem::fileRead(). |
||
| 220 | */ |
||
| 221 | public function testFileRead(): void |
||
| 222 | { |
||
| 223 | Filesystem::fileWrite(self::$testFiles['file1'], "This is a test."); |
||
| 224 | |||
| 225 | /** @var string $data */ |
||
| 226 | $data = Filesystem::fileRead(self::$testFiles['file1']); |
||
| 227 | $data = trim($data); |
||
| 228 | |||
| 229 | self::assertSame('This is a test.', $data); |
||
| 230 | |||
| 231 | Filesystem::fileWrite(self::$testFiles['file1']); |
||
| 232 | |||
| 233 | self::expectException(InvalidArgumentException::class); |
||
|
1 ignored issue
–
show
|
|||
| 234 | $read = Filesystem::fileRead(self::$testFiles['file1'] . '.php'); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Test Filesystem::fileWrite(). |
||
| 239 | */ |
||
| 240 | public function testFileWrite(): void |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Test Filesystem::isReallyWritable(). |
||
| 257 | */ |
||
| 258 | public function testIsReallyWritable(): void |
||
| 267 | } |
||
| 268 | } |
||
| 269 |