Skip to content

Commit d33c3e5

Browse files
Add tests for FileSystem class (#51654)
1 parent f05f7bc commit d33c3e5

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

tests/Filesystem/FilesystemTest.php

+26-11
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,28 @@ public function testLines()
5959
{
6060
$path = self::$tempDir.'/file.txt';
6161

62-
$contents = LazyCollection::times(3)
63-
->map(function ($number) {
64-
return "line-{$number}";
65-
})
66-
->join("\n");
67-
62+
$contents = ' '.PHP_EOL.' spaces around '.PHP_EOL.PHP_EOL.'Line 2'.PHP_EOL.'1 trailing empty line ->'.PHP_EOL.PHP_EOL;
6863
file_put_contents($path, $contents);
6964

7065
$files = new Filesystem;
7166
$this->assertInstanceOf(LazyCollection::class, $files->lines($path));
7267

7368
$this->assertSame(
74-
['line-1', 'line-2', 'line-3'],
69+
[' ', ' spaces around ', '', 'Line 2', '1 trailing empty line ->', '', ''],
7570
$files->lines($path)->all()
7671
);
72+
73+
// an empty file:
74+
ftruncate(fopen($path, 'w'), 0);
75+
$this->assertSame([''], $files->lines($path)->all());
76+
}
77+
78+
public function testLinesThrowsExceptionNonexisitingFile()
79+
{
80+
$this->expectException(FileNotFoundException::class);
81+
$this->expectExceptionMessage('File does not exist at path '.__DIR__.'/unknown-file.txt.');
82+
83+
(new Filesystem)->lines(__DIR__.'/unknown-file.txt');
7784
}
7885

7986
public function testReplaceCreatesFile()
@@ -324,9 +331,9 @@ public function testMoveDirectoryReturnsFalseWhileOverwritingAndUnableToDeleteDe
324331
public function testGetThrowsExceptionNonexisitingFile()
325332
{
326333
$this->expectException(FileNotFoundException::class);
334+
$this->expectExceptionMessage('File does not exist at path '.self::$tempDir.'/unknown-file.txt.');
327335

328-
$files = new Filesystem;
329-
$files->get(self::$tempDir.'/unknown-file.txt');
336+
(new Filesystem)->get(self::$tempDir.'/unknown-file.txt');
330337
}
331338

332339
public function testGetRequireReturnsProperly()
@@ -339,9 +346,9 @@ public function testGetRequireReturnsProperly()
339346
public function testGetRequireThrowsExceptionNonExistingFile()
340347
{
341348
$this->expectException(FileNotFoundException::class);
349+
$this->expectExceptionMessage('File does not exist at path '.self::$tempDir.'/unknown-file.txt.');
342350

343-
$files = new Filesystem;
344-
$files->getRequire(self::$tempDir.'/file.php');
351+
(new Filesystem)->getRequire(self::$tempDir.'/unknown-file.txt');
345352
}
346353

347354
public function testJsonReturnsDecodedJsonData()
@@ -564,6 +571,14 @@ public function testRequireOnceRequiresFileProperly()
564571
$this->assertFalse(function_exists('random_function_xyz_changed'));
565572
}
566573

574+
public function testRequireOnceThrowsExceptionNonexisitingFile()
575+
{
576+
$this->expectException(FileNotFoundException::class);
577+
$this->expectExceptionMessage('File does not exist at path '.__DIR__.'/unknown-file.txt.');
578+
579+
(new Filesystem)->requireOnce(__DIR__.'/unknown-file.txt');
580+
}
581+
567582
public function testCopyCopiesFileProperly()
568583
{
569584
$filesystem = new Filesystem;

0 commit comments

Comments
 (0)