Skip to content

Commit f650a7b

Browse files
committed
Merge branch '3.x' into feat/testMoveAndCopyTasks
2 parents 526ce03 + c6cc4fe commit f650a7b

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/PhpseclibV3/SftpAdapter.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,19 @@ public function move(string $source, string $destination, Config $config): void
327327
return;
328328
}
329329

330-
if ($this->fileExists($destination)) {
331-
$this->delete($destination);
330+
if ($connection->rename($sourceLocation, $destinationLocation)) {
331+
return;
332332
}
333333

334-
if ( ! $connection->rename($sourceLocation, $destinationLocation)) {
335-
throw UnableToMoveFile::fromLocationTo($source, $destination);
334+
// Overwrite existing file / dir
335+
if ($connection->is_file($destinationLocation)) {
336+
$this->delete($destination);
337+
if ($connection->rename($sourceLocation, $destinationLocation)) {
338+
return;
339+
}
336340
}
341+
342+
throw UnableToMoveFile::fromLocationTo($source, $destination);
337343
}
338344

339345
public function copy(string $source, string $destination, Config $config): void

src/PhpseclibV3/SftpAdapterTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use League\Flysystem\UnableToMoveFile;
1313
use League\Flysystem\UnableToReadFile;
1414
use League\Flysystem\UnableToWriteFile;
15+
use League\Flysystem\Visibility;
1516
use phpseclib3\Net\SFTP;
1617

1718
use function class_exists;
@@ -230,6 +231,37 @@ public function it_can_proactively_close_a_connection(): void
230231

231232
self::assertFalse(static::$connectionProvider->connection->isConnected());
232233
}
234+
/**
235+
* @test
236+
* @fixme Move to FilesystemAdapterTestCase once all adapters pass
237+
*/
238+
public function moving_a_file_and_overwriting(): void
239+
{
240+
$this->runScenario(function() {
241+
$adapter = $this->adapter();
242+
$adapter->write(
243+
'source.txt',
244+
'contents to be moved',
245+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
246+
);
247+
$adapter->write(
248+
'destination.txt',
249+
'contents to be overwritten',
250+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
251+
);
252+
$adapter->move('source.txt', 'destination.txt', new Config());
253+
$this->assertFalse(
254+
$adapter->fileExists('source.txt'),
255+
'After moving a file should no longer exist in the original location.'
256+
);
257+
$this->assertTrue(
258+
$adapter->fileExists('destination.txt'),
259+
'After moving, a file should be present at the new location.'
260+
);
261+
$this->assertEquals(Visibility::PUBLIC, $adapter->visibility('destination.txt')->visibility());
262+
$this->assertEquals('contents to be moved', $adapter->read('destination.txt'));
263+
});
264+
}
233265

234266
private static function connectionProvider(): StubSftpConnectionProvider
235267
{

0 commit comments

Comments
 (0)