Skip to content

Commit 0b2a971

Browse files
authored
Merge pull request #4 from open-source-contributions/issue_#1
Resolves issue #1
2 parents e924a6a + cf12a1f commit 0b2a971

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/SSHConnection.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ private function sanityCheck()
6262
}
6363
}
6464

65+
public function upload(string $localPath, string $remotePath): bool
66+
{
67+
if (!is_resource($this->resource)) {
68+
throw new InvalidArgumentException('The session resource is invalid.');
69+
}
70+
71+
if (!file_exists($localPath)) {
72+
throw new InvalidArgumentException('The localPath is invalid.');
73+
}
74+
75+
return ssh2_scp_send($this->resource, $localPath, $remotePath);
76+
}
77+
78+
public function download(string $remotePath, string $localPath): bool
79+
{
80+
if (!is_resource($this->resource)) {
81+
throw new InvalidArgumentException('The session resource is invalid.');
82+
}
83+
84+
return ssh2_scp_recv($this->resource, $remotePath, $localPath);
85+
}
86+
6587
public function connect(): self
6688
{
6789
$this->sanityCheck();
@@ -104,4 +126,4 @@ public function run(string $command)
104126

105127
return new SSHCommand($this->resource, $command);
106128
}
107-
}
129+
}

tests/Integration/SSHConnectionTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,38 @@
55

66
final class SSHConnectionTest extends TestCase
77
{
8+
public function testUpload()
9+
{
10+
$connection = (new SSHConnection())
11+
->to('localhost')
12+
->onPort(22)
13+
->as('travis')
14+
->withKeyPair('/home/travis/.ssh/id_rsa.pub', '/home/travis/.ssh/id_rsa')
15+
->connect();
16+
17+
$remotePath = __DIR__ . '/../fixtures/upload.txt';
18+
$localPath = __DIR__ . '/../fixtures/file.txt';
19+
20+
$this->assertTrue($connection->upload($localPath, $remotePath));
21+
$this->assertFileExists($remotePath);
22+
}
23+
24+
public function testDownload()
25+
{
26+
$connection = (new SSHConnection())
27+
->to('localhost')
28+
->onPort(22)
29+
->as('travis')
30+
->withKeyPair('/home/travis/.ssh/id_rsa.pub', '/home/travis/.ssh/id_rsa')
31+
->connect();
32+
33+
$remotePath = __DIR__ . '/../fixtures/file.txt';
34+
$localPath = __DIR__ . '/../fixtures/download.txt';
35+
36+
$this->assertTrue($connection->download($remotePath, $localPath));
37+
$this->assertFileExists($localPath);
38+
}
39+
840
public function testSSHConnectionWithKeyPair()
941
{
1042
$connection = (new SSHConnection())

tests/fixtures/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.upload.download

0 commit comments

Comments
 (0)