Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create root directory if it does not exist #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/SftpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/SftpAdapterTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand Down