Skip to content

Commit

Permalink
Add tests for FileSystem class (#51654)
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 authored May 31, 2024
1 parent f05f7bc commit d33c3e5
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,28 @@ public function testLines()
{
$path = self::$tempDir.'/file.txt';

$contents = LazyCollection::times(3)
->map(function ($number) {
return "line-{$number}";
})
->join("\n");

$contents = ' '.PHP_EOL.' spaces around '.PHP_EOL.PHP_EOL.'Line 2'.PHP_EOL.'1 trailing empty line ->'.PHP_EOL.PHP_EOL;
file_put_contents($path, $contents);

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

$this->assertSame(
['line-1', 'line-2', 'line-3'],
[' ', ' spaces around ', '', 'Line 2', '1 trailing empty line ->', '', ''],
$files->lines($path)->all()
);

// an empty file:
ftruncate(fopen($path, 'w'), 0);
$this->assertSame([''], $files->lines($path)->all());
}

public function testLinesThrowsExceptionNonexisitingFile()
{
$this->expectException(FileNotFoundException::class);
$this->expectExceptionMessage('File does not exist at path '.__DIR__.'/unknown-file.txt.');

(new Filesystem)->lines(__DIR__.'/unknown-file.txt');
}

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

$files = new Filesystem;
$files->get(self::$tempDir.'/unknown-file.txt');
(new Filesystem)->get(self::$tempDir.'/unknown-file.txt');
}

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

$files = new Filesystem;
$files->getRequire(self::$tempDir.'/file.php');
(new Filesystem)->getRequire(self::$tempDir.'/unknown-file.txt');
}

public function testJsonReturnsDecodedJsonData()
Expand Down Expand Up @@ -564,6 +571,14 @@ public function testRequireOnceRequiresFileProperly()
$this->assertFalse(function_exists('random_function_xyz_changed'));
}

public function testRequireOnceThrowsExceptionNonexisitingFile()
{
$this->expectException(FileNotFoundException::class);
$this->expectExceptionMessage('File does not exist at path '.__DIR__.'/unknown-file.txt.');

(new Filesystem)->requireOnce(__DIR__.'/unknown-file.txt');
}

public function testCopyCopiesFileProperly()
{
$filesystem = new Filesystem;
Expand Down

0 comments on commit d33c3e5

Please sign in to comment.