File tree Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,28 @@ private function sanityCheck()
62
62
}
63
63
}
64
64
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
+
65
87
public function connect (): self
66
88
{
67
89
$ this ->sanityCheck ();
@@ -104,4 +126,4 @@ public function run(string $command)
104
126
105
127
return new SSHCommand ($ this ->resource , $ command );
106
128
}
107
- }
129
+ }
Original file line number Diff line number Diff line change 5
5
6
6
final class SSHConnectionTest extends TestCase
7
7
{
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
+
8
40
public function testSSHConnectionWithKeyPair ()
9
41
{
10
42
$ connection = (new SSHConnection ())
Original file line number Diff line number Diff line change
1
+ test.upload.download
You can’t perform that action at this time.
0 commit comments