From 401d14142b55b16ae8a59ec6524ae8bc58f47849 Mon Sep 17 00:00:00 2001 From: Iman Ghafoori Date: Fri, 31 May 2024 03:22:51 +0330 Subject: [PATCH] Add tests for FileSystem class --- tests/Filesystem/FilesystemTest.php | 37 ++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/tests/Filesystem/FilesystemTest.php b/tests/Filesystem/FilesystemTest.php index 54fc3b0b2bc..eddd0d31848 100755 --- a/tests/Filesystem/FilesystemTest.php +++ b/tests/Filesystem/FilesystemTest.php @@ -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 = 'Line 1'.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'], + ['Line 1', '', '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() @@ -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() @@ -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() @@ -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;