Skip to content

Commit

Permalink
Support directories in --to option for backup:get
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Haines committed Jan 28, 2020
1 parent aaa799d commit 17e56a9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ class Request implements ConfigAwareInterface, ContainerAwareInterface, LoggerAw
*/
public function download($url, $target)
{
if (is_dir($target)) {
if (substr($target, -1) == DIRECTORY_SEPARATOR) {
$target = $target . basename($url);
} else {
$target = $target . DIRECTORY_SEPARATOR . basename($url);
}
}

if ($this->getContainer()->get(LocalMachineHelper::class)->getFilesystem()->exists($target)) {
throw new TerminusException('Target file {target} already exists.', compact('target'));
}
Expand Down
41 changes: 41 additions & 0 deletions tests/unit_tests/Request/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,47 @@ public function testDownloadPathExists()
$this->assertNull($out);
}

/**
* Tests a successful download with the target being a directory
*/
public function testDownloadTargetDirectory()
{
$domain = 'pantheon.io';
$url = "http://$domain/somefile.tar.gz";
$target = './';
$target_with_file = './somefile.tar.gz';

$this->container->expects($this->at(0))
->method('get')
->with($this->equalTo(LocalMachineHelper::class))
->willReturn($this->local_machine_helper);
$this->local_machine_helper->expects($this->once())
->method('getFilesystem')
->with()
->willReturn($this->filesystem);
$this->filesystem->expects($this->once())
->method('exists')
->with($target_with_file)
->willReturn(false);
$this->container->expects($this->at(1))
->method('get')
->with(
$this->equalTo(Client::class),
$this->equalTo([['base_uri' => $domain, RequestOptions::VERIFY => true,],])
)
->willReturn($this->client);
$this->client->expects($this->once())
->method('request')
->with(
$this->equalTo('GET'),
$this->equalTo($url),
$this->equalTo(['sink' => $target_with_file,])
);

$out = $this->request->download($url, $target);
$this->assertNull($out);
}

public function testRequest()
{
$this->session->method('get')->with('session')->willReturn(false);
Expand Down

0 comments on commit 17e56a9

Please sign in to comment.