From ed6ae543b95562f6689b7f8eaf717b0c694bf0e6 Mon Sep 17 00:00:00 2001 From: "a.moseev" Date: Wed, 27 Nov 2019 21:55:02 +0300 Subject: [PATCH] Create root directory if it does not exist --- src/SftpAdapter.php | 4 ++++ tests/SftpAdapterTests.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/SftpAdapter.php b/src/SftpAdapter.php index 013e13e..2dc5574 100644 --- a/src/SftpAdapter.php +++ b/src/SftpAdapter.php @@ -263,6 +263,10 @@ protected function setConnectionRoot() return; } + if (! $this->connection->is_dir($root) && ! $this->connection->mkdir($root)) { + throw new \RuntimeException('Root does not exist and could not be created: ' . $root . "\n" . "sftp errors:" . implode(",", $this->connection->getSFTPErrors())); + } + if (! $this->connection->chdir($root)) { throw new InvalidRootException('Root is invalid or does not exist: '.$root); } diff --git a/tests/SftpAdapterTests.php b/tests/SftpAdapterTests.php index e6cf3f4..b6ec8d0 100644 --- a/tests/SftpAdapterTests.php +++ b/tests/SftpAdapterTests.php @@ -545,11 +545,41 @@ public function testConnectWithRoot($filesystem, $adapter, $mock) $adapter->setRoot('/root'); $adapter->setNetSftpConnection($mock); $mock->shouldReceive('login')->with('test', 'test')->andReturn(true); + $mock->shouldReceive('is_dir')->with('/root/')->andReturn(true); $mock->shouldReceive('chdir')->with('/root/')->andReturn(true); $adapter->connect(); $adapter->disconnect(); } + /** + * @dataProvider adapterProvider + */ + public function testConnectWithNotExistedRoot($filesystem, $adapter, $mock) + { + $adapter->setRoot('/root'); + $adapter->setNetSftpConnection($mock); + $mock->shouldReceive('login')->with('test', 'test')->andReturn(true); + $mock->shouldReceive('is_dir')->with('/root/')->andReturn(false); + $mock->shouldReceive('mkdir')->with('/root/')->andReturn(true); + $mock->shouldReceive('chdir')->with('/root/')->andReturn(true); + $adapter->connect(); + $adapter->disconnect(); + } + + /** + * @dataProvider adapterProvider + * @expectedException RuntimeException + */ + public function testConnectWithNotExistedRootAndCouldNotBeCreated($filesystem, $adapter, $mock) + { + $adapter->setRoot('/root'); + $adapter->setNetSftpConnection($mock); + $mock->shouldReceive('login')->with('test', 'test')->andReturn(true); + $mock->shouldReceive('is_dir')->with('/root/')->andReturn(false); + $mock->shouldReceive('mkdir')->with('/root/')->andReturn(false); + $adapter->connect(); + } + /** * @dataProvider adapterProvider * @expectedException RuntimeException @@ -559,6 +589,7 @@ public function testConnectWithInvalidRoot($filesystem, $adapter, $mock) $adapter->setRoot('/root'); $adapter->setNetSftpConnection($mock); $mock->shouldReceive('login')->with('test', 'test')->andReturn(true); + $mock->shouldReceive('is_dir')->with('/root/')->andReturn(true); $mock->shouldReceive('chdir')->with('/root/')->andReturn(false); $adapter->connect(); }