Skip to content

Commit

Permalink
Merge branch '3.x' into feat/testMoveAndCopyTasks
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect committed Aug 14, 2024
2 parents 526ce03 + c6cc4fe commit f650a7b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/PhpseclibV3/SftpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,19 @@ public function move(string $source, string $destination, Config $config): void
return;
}

if ($this->fileExists($destination)) {
$this->delete($destination);
if ($connection->rename($sourceLocation, $destinationLocation)) {
return;
}

if ( ! $connection->rename($sourceLocation, $destinationLocation)) {
throw UnableToMoveFile::fromLocationTo($source, $destination);
// Overwrite existing file / dir
if ($connection->is_file($destinationLocation)) {
$this->delete($destination);
if ($connection->rename($sourceLocation, $destinationLocation)) {
return;
}
}

throw UnableToMoveFile::fromLocationTo($source, $destination);
}

public function copy(string $source, string $destination, Config $config): void
Expand Down
32 changes: 32 additions & 0 deletions src/PhpseclibV3/SftpAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\Visibility;
use phpseclib3\Net\SFTP;

use function class_exists;
Expand Down Expand Up @@ -230,6 +231,37 @@ public function it_can_proactively_close_a_connection(): void

self::assertFalse(static::$connectionProvider->connection->isConnected());
}
/**
* @test
* @fixme Move to FilesystemAdapterTestCase once all adapters pass
*/
public function moving_a_file_and_overwriting(): void
{
$this->runScenario(function() {
$adapter = $this->adapter();
$adapter->write(
'source.txt',
'contents to be moved',
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
);
$adapter->write(
'destination.txt',
'contents to be overwritten',
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
);
$adapter->move('source.txt', 'destination.txt', new Config());
$this->assertFalse(
$adapter->fileExists('source.txt'),
'After moving a file should no longer exist in the original location.'
);
$this->assertTrue(
$adapter->fileExists('destination.txt'),
'After moving, a file should be present at the new location.'
);
$this->assertEquals(Visibility::PUBLIC, $adapter->visibility('destination.txt')->visibility());
$this->assertEquals('contents to be moved', $adapter->read('destination.txt'));
});
}

private static function connectionProvider(): StubSftpConnectionProvider
{
Expand Down

0 comments on commit f650a7b

Please sign in to comment.