Skip to content

Commit 2c8083f

Browse files
authored
Merge pull request #1808 from tinect/feat/testMoveAndCopyTasks
feat: add dedicated test cases to ensure copy and move methods to always overwrite target
2 parents 0ee937a + f650a7b commit 2c8083f

File tree

10 files changed

+90
-20
lines changed

10 files changed

+90
-20
lines changed

src/AdapterTestUtilities/FilesystemAdapterTestCase.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,58 @@ public function copying_a_file_with_collision(): void
824824
});
825825
}
826826

827+
/**
828+
* @test
829+
*/
830+
public function moving_a_file_with_collision(): void
831+
{
832+
$this->runScenario(function () {
833+
$adapter = $this->adapter();
834+
$adapter->write('path.txt', 'new contents', new Config());
835+
$adapter->write('new-path.txt', 'contents', new Config());
836+
837+
$adapter->move('path.txt', 'new-path.txt', new Config());
838+
839+
$oldFileExists = $adapter->fileExists('path.txt');
840+
$this->assertFalse($oldFileExists);
841+
842+
$contents = $adapter->read('new-path.txt');
843+
$this->assertEquals('new contents', $contents);
844+
});
845+
}
846+
847+
/**
848+
* @test
849+
*/
850+
public function copying_a_file_with_same_destination(): void
851+
{
852+
$this->runScenario(function () {
853+
$adapter = $this->adapter();
854+
$adapter->write('path.txt', 'new contents', new Config());
855+
856+
$adapter->copy('path.txt', 'path.txt', new Config());
857+
$contents = $adapter->read('path.txt');
858+
859+
$this->assertEquals('new contents', $contents);
860+
});
861+
}
862+
863+
/**
864+
* @test
865+
*/
866+
public function moving_a_file_with_same_destination(): void
867+
{
868+
$this->runScenario(function () {
869+
$adapter = $this->adapter();
870+
$adapter->write('path.txt', 'new contents', new Config());
871+
872+
$adapter->move('path.txt', 'path.txt', new Config());
873+
874+
$contents = $adapter->read('path.txt');
875+
$this->assertEquals('new contents', $contents);
876+
});
877+
}
878+
827879
protected function assertFileExistsAtPath(string $path): void
828880
{
829881
$this->runScenario(function () use ($path) {

src/FilesystemTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,10 @@ public function publicUrl(string $path, Config $config): string
504504
*/
505505
public function copying_from_and_to_the_same_location_fails(): void
506506
{
507-
$this->expectExceptionObject(UnableToCopyFile::fromLocationTo('from.txt', 'from.txt'));
507+
$this->expectExceptionObject(UnableToCopyFile::sourceAndDestinationAreTheSame('from.txt', 'from.txt'));
508508

509-
$this->filesystem->copy('from.txt', 'from.txt');
509+
$config = [Config::OPTION_COPY_IDENTICAL_PATH => ResolveIdenticalPathConflict::FAIL];
510+
$this->filesystem->copy('from.txt', 'from.txt', $config);
510511
}
511512

512513
/**
@@ -516,7 +517,8 @@ public function moving_from_and_to_the_same_location_fails(): void
516517
{
517518
$this->expectExceptionObject(UnableToMoveFile::fromLocationTo('from.txt', 'from.txt'));
518519

519-
$this->filesystem->move('from.txt', 'from.txt');
520+
$config = [Config::OPTION_MOVE_IDENTICAL_PATH => ResolveIdenticalPathConflict::FAIL];
521+
$this->filesystem->move('from.txt', 'from.txt', $config);
520522
}
521523

522524
/**

src/GridFS/GridFSAdapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ public function listContents(string $path, bool $deep): iterable
345345

346346
public function move(string $source, string $destination, Config $config): void
347347
{
348+
if ($source === $destination) {
349+
return;
350+
}
351+
352+
if ($this->fileExists($destination)) {
353+
$this->delete($destination);
354+
}
355+
348356
try {
349357
$result = $this->bucket->getFilesCollection()->updateMany(
350358
['filename' => $this->prefixer->prefixPath($source)],

src/GridFS/GridFSAdapterTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use League\Flysystem\UnableToWriteFile;
1616
use MongoDB\Client;
1717
use MongoDB\Database;
18-
use MongoDB\Driver\ReadPreference;
1918
use function getenv;
2019

2120
/**

src/InMemory/InMemoryFilesystemAdapter.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,14 @@ public function move(string $source, string $destination, Config $config): void
223223
$sourcePath = $this->preparePath($source);
224224
$destinationPath = $this->preparePath($destination);
225225

226-
if ( ! $this->fileExists($source) || $this->fileExists($destination)) {
226+
if ( ! $this->fileExists($source)) {
227227
throw UnableToMoveFile::fromLocationTo($source, $destination);
228228
}
229229

230-
$this->files[$destinationPath] = $this->files[$sourcePath];
231-
unset($this->files[$sourcePath]);
230+
if ($sourcePath !== $destinationPath) {
231+
$this->files[$destinationPath] = $this->files[$sourcePath];
232+
unset($this->files[$sourcePath]);
233+
}
232234

233235
if ($visibility = $config->get(Config::OPTION_VISIBILITY)) {
234236
$this->setVisibility($destination, $visibility);

src/InMemory/InMemoryFilesystemAdapterTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,6 @@ public function moving_a_file_successfully(): void
190190
$this->assertTrue($adapter->fileExists('new-path.txt'));
191191
}
192192

193-
/**
194-
* @test
195-
*/
196-
public function moving_a_file_with_collision(): void
197-
{
198-
$this->expectException(UnableToMoveFile::class);
199-
$adapter = $this->adapter();
200-
$adapter->write('path.txt', 'contents', new Config());
201-
$adapter->write('new-path.txt', 'contents', new Config());
202-
$adapter->move('path.txt', 'new-path.txt', new Config());
203-
}
204-
205193
/**
206194
* @test
207195
*/

src/Local/LocalFilesystemAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function copy(string $source, string $destination, Config $config): void
271271
$this->resolveDirectoryVisibility($config->get(Config::OPTION_DIRECTORY_VISIBILITY))
272272
);
273273

274-
if ( ! @copy($sourcePath, $destinationPath)) {
274+
if ($sourcePath !== $destinationPath && ! @copy($sourcePath, $destinationPath)) {
275275
throw UnableToCopyFile::because(error_get_last()['message'] ?? 'unknown', $source, $destination);
276276
}
277277

src/PhpseclibV3/SftpAdapter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ public function move(string $source, string $destination, Config $config): void
323323
throw UnableToMoveFile::fromLocationTo($source, $destination, $exception);
324324
}
325325

326+
if ($sourceLocation === $destinationLocation) {
327+
return;
328+
}
329+
326330
if ($connection->rename($sourceLocation, $destinationLocation)) {
327331
return;
328332
}

src/WebDAV/WebDAVAdapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ private function normalizeObject(array $object): array
332332

333333
public function move(string $source, string $destination, Config $config): void
334334
{
335+
if ($source === $destination) {
336+
return;
337+
}
338+
335339
if ($this->manualMove) {
336340
$this->manualMove($source, $destination);
337341

@@ -369,6 +373,10 @@ private function manualMove(string $source, string $destination): void
369373

370374
public function copy(string $source, string $destination, Config $config): void
371375
{
376+
if ($source === $destination) {
377+
return;
378+
}
379+
372380
if ($this->manualCopy) {
373381
$this->manualCopy($source, $destination);
374382

src/ZipArchive/ZipArchiveAdapter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,13 @@ public function move(string $source, string $destination, Config $config): void
340340
$archive = $this->zipArchiveProvider->createZipArchive();
341341

342342
if ($archive->locateName($this->pathPrefixer->prefixPath($destination)) !== false) {
343+
if ($source === $destination) {
344+
//update the config of the file
345+
$this->copy($source, $destination, $config);
346+
347+
return;
348+
}
349+
343350
$this->delete($destination);
344351
$this->copy($source, $destination, $config);
345352
$this->delete($source);

0 commit comments

Comments
 (0)