Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add tests for FileSystem class #51654

Merged
merged 1 commit into from
May 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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