From de810cf715f3a60295a28569112225c2754a47da Mon Sep 17 00:00:00 2001 From: faizel <15678863291@163.com> Date: Mon, 9 Dec 2024 21:54:26 +0800 Subject: [PATCH] test(src): add unit tests for Filesystem::copy method. --- src/Support/tests/Unit/FilesystemTest.php | 59 +++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/Support/tests/Unit/FilesystemTest.php diff --git a/src/Support/tests/Unit/FilesystemTest.php b/src/Support/tests/Unit/FilesystemTest.php new file mode 100644 index 0000000..195b9a8 --- /dev/null +++ b/src/Support/tests/Unit/FilesystemTest.php @@ -0,0 +1,59 @@ +sourceDir = sys_get_temp_dir() . '/source_' . uniqid(); + $this->targetDir = sys_get_temp_dir() . '/target_' . uniqid(); + + BaseFileSystem::createDir($this->sourceDir); + BaseFileSystem::createDir($this->targetDir); + }); + + afterEach(function () { + BaseFileSystem::delete($this->sourceDir); + BaseFileSystem::delete($this->targetDir); + }); + + it('can copy nested directories while preserving existing files', function () { + BaseFileSystem::createDir($this->sourceDir . '/subDir'); + file_put_contents($this->sourceDir . '/file1.txt', 'content1'); + file_put_contents($this->sourceDir . '/subDir/file2.txt', 'content2'); + + BaseFileSystem::createDir($this->targetDir . '/subDir'); + file_put_contents($this->targetDir . '/subDir/existing.txt', 'existing content'); + + Filesystem::copy($this->sourceDir, $this->targetDir); + + expect($this->targetDir . '/subDir')->toBeDirectory() + ->and($this->targetDir . '/file1.txt')->toBeFile() + ->and($this->targetDir . '/subDir/file2.txt')->toBeFile() + ->and($this->targetDir . '/subDir/existing.txt')->toBeFile() + ->and(file_get_contents($this->targetDir . '/file1.txt'))->toBe('content1') + ->and(file_get_contents($this->targetDir . '/subDir/file2.txt'))->toBe('content2') + ->and(file_get_contents($this->targetDir . '/subDir/existing.txt'))->toBe('existing content') + ->and($this->sourceDir)->not->toBeDirectory(); + }); + + it('can copy without deleting source directory', function () { + file_put_contents($this->sourceDir . '/test.txt', 'test content'); + + Filesystem::copy($this->sourceDir, $this->targetDir, false); + + expect($this->sourceDir)->toBeDirectory() + ->and($this->sourceDir . '/test.txt')->toBeFile() + ->and($this->targetDir . '/test.txt')->toBeFile() + ->and(file_get_contents($this->targetDir . '/test.txt'))->toBe('test content'); + }); +});