Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion src/Support/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ public static function copy(string $source, string $target, bool $deleteSource =
if ($file === '.' || $file === '..') {
continue;
}
\Nette\Utils\FileSystem::copy($source . '/' . $file, $target . '/' . $file);

$sourcePath = $source . '/' . $file;
$targetPath = $target . '/' . $file;
if (is_dir($sourcePath)) {
self::copy($sourcePath, $targetPath, $deleteSource);
} else {
\Nette\Utils\FileSystem::copy($sourcePath, $targetPath);
}
}
if ($deleteSource) {
\Nette\Utils\FileSystem::delete($source);
Expand Down
59 changes: 59 additions & 0 deletions src/Support/tests/Unit/FilesystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
use Mine\Support\Filesystem;
use Nette\Utils\FileSystem as BaseFileSystem;

describe('copy', function () {
beforeEach(function () {
$this->sourceDir = sys_get_temp_dir() . '/source_' . uniqid();

Check failure on line 17 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 17 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 17 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 17 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Undefined variable: $this

Check failure on line 17 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Undefined variable: $this

Check failure on line 17 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 17 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Undefined variable: $this

Check failure on line 17 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 17 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / build Code coverage report (ubuntu-latest, 8.1, v5.1.2)

Undefined variable: $this
$this->targetDir = sys_get_temp_dir() . '/target_' . uniqid();

Check failure on line 18 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 18 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 18 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 18 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Undefined variable: $this

Check failure on line 18 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Undefined variable: $this

Check failure on line 18 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 18 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Undefined variable: $this

Check failure on line 18 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 18 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / build Code coverage report (ubuntu-latest, 8.1, v5.1.2)

Undefined variable: $this

BaseFileSystem::createDir($this->sourceDir);

Check failure on line 20 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 20 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 20 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 20 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Undefined variable: $this

Check failure on line 20 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Undefined variable: $this

Check failure on line 20 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 20 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Undefined variable: $this

Check failure on line 20 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 20 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / build Code coverage report (ubuntu-latest, 8.1, v5.1.2)

Undefined variable: $this
BaseFileSystem::createDir($this->targetDir);

Check failure on line 21 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 21 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 21 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 21 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Undefined variable: $this

Check failure on line 21 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Undefined variable: $this

Check failure on line 21 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 21 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Undefined variable: $this

Check failure on line 21 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 21 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / build Code coverage report (ubuntu-latest, 8.1, v5.1.2)

Undefined variable: $this
});

afterEach(function () {
BaseFileSystem::delete($this->sourceDir);

Check failure on line 25 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 25 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 25 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 25 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Undefined variable: $this

Check failure on line 25 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Undefined variable: $this

Check failure on line 25 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 25 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Undefined variable: $this

Check failure on line 25 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 25 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / build Code coverage report (ubuntu-latest, 8.1, v5.1.2)

Undefined variable: $this
BaseFileSystem::delete($this->targetDir);

Check failure on line 26 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 26 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 26 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 26 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Undefined variable: $this

Check failure on line 26 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Undefined variable: $this

Check failure on line 26 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 26 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Undefined variable: $this

Check failure on line 26 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 26 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / build Code coverage report (ubuntu-latest, 8.1, v5.1.2)

Undefined variable: $this
});

it('can copy nested directories while preserving existing files', function () {
BaseFileSystem::createDir($this->sourceDir . '/subDir');

Check failure on line 30 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 30 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 30 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 30 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Undefined variable: $this

Check failure on line 30 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Undefined variable: $this

Check failure on line 30 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 30 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Undefined variable: $this

Check failure on line 30 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 30 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / build Code coverage report (ubuntu-latest, 8.1, v5.1.2)

Undefined variable: $this
file_put_contents($this->sourceDir . '/file1.txt', 'content1');

Check failure on line 31 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 31 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 31 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 31 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Undefined variable: $this

Check failure on line 31 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Undefined variable: $this

Check failure on line 31 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 31 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Undefined variable: $this

Check failure on line 31 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 31 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / build Code coverage report (ubuntu-latest, 8.1, v5.1.2)

Undefined variable: $this
file_put_contents($this->sourceDir . '/subDir/file2.txt', 'content2');

Check failure on line 32 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 32 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 32 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 32 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Undefined variable: $this

Check failure on line 32 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Undefined variable: $this

Check failure on line 32 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 32 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Undefined variable: $this

Check failure on line 32 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 32 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / build Code coverage report (ubuntu-latest, 8.1, v5.1.2)

Undefined variable: $this

BaseFileSystem::createDir($this->targetDir . '/subDir');

Check failure on line 34 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 34 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 34 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 34 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.2 Swoole-master

Undefined variable: $this

Check failure on line 34 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-master

Undefined variable: $this

Check failure on line 34 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-v5.0.3

Undefined variable: $this

Check failure on line 34 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.1 Swoole-master

Undefined variable: $this

Check failure on line 34 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / Test on PHP8.3 Swoole-v5.1.2

Undefined variable: $this

Check failure on line 34 in src/Support/tests/Unit/FilesystemTest.php

View workflow job for this annotation

GitHub Actions / build Code coverage report (ubuntu-latest, 8.1, v5.1.2)

Undefined variable: $this
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');
});
});
Loading