-
Notifications
You must be signed in to change notification settings - Fork 95
/
SftpAdapterTest.php
235 lines (188 loc) · 5.81 KB
/
SftpAdapterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
declare(strict_types=1);
namespace League\Flysystem\PhpseclibV2;
use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase;
use League\Flysystem\Config;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\UnableToCopyFile;
use League\Flysystem\UnableToCreateDirectory;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToWriteFile;
use function class_exists;
/**
* @group sftp
* @group legacy
*/
class SftpAdapterTest extends FilesystemAdapterTestCase
{
/**
* @var ConnectionProvider
*/
private static $connectionProvider;
/**
* @var SftpStub
*/
private $connection;
public static function setUpBeforeClass(): void
{
if ( ! class_exists('phpseclib\Net\SFTP')) {
self::markTestSkipped("PHPSecLib V2 is not installed");
}
}
protected static function createFilesystemAdapter(): FilesystemAdapter
{
return new SftpAdapter(
static::connectionProvider(),
'/upload'
);
}
/**
* @before
*/
public function setupConnectionProvider(): void
{
/** @var SftpStub $connection */
$connection = static::connectionProvider()->provideConnection();
$this->connection = $connection;
$this->connection->reset();
}
/**
* @test
*/
public function failing_to_create_a_directory(): void
{
$adapter = $this->adapterWithInvalidRoot();
$this->expectException(UnableToCreateDirectory::class);
$adapter->createDirectory('not-gonna-happen', new Config());
}
/**
* @test
*/
public function failing_to_write_a_file(): void
{
$adapter = $this->adapterWithInvalidRoot();
$this->expectException(UnableToWriteFile::class);
$adapter->write('not-gonna-happen', 'na-ah', new Config());
}
/**
* @test
*/
public function failing_to_read_a_file(): void
{
$adapter = $this->adapterWithInvalidRoot();
$this->expectException(UnableToReadFile::class);
$adapter->read('not-gonna-happen');
}
/**
* @test
*/
public function failing_to_read_a_file_as_a_stream(): void
{
$adapter = $this->adapterWithInvalidRoot();
$this->expectException(UnableToReadFile::class);
$adapter->readStream('not-gonna-happen');
}
/**
* @test
*/
public function failing_to_write_a_file_using_streams(): void
{
$adapter = $this->adapterWithInvalidRoot();
$writeHandle = stream_with_contents('contents');
$this->expectException(UnableToWriteFile::class);
try {
$adapter->writeStream('not-gonna-happen', $writeHandle, new Config());
} finally {
fclose($writeHandle);
}
}
/**
* @test
*/
public function detecting_mimetype(): void
{
$adapter = $this->adapter();
$adapter->write('file.svg', (string) file_get_contents(__DIR__ . '/../AdapterTestUtilities/test_files/flysystem.svg'), new Config());
$mimeType = $adapter->mimeType('file.svg');
$this->assertStringStartsWith('image/svg+xml', $mimeType->mimeType());
}
/**
* @test
*/
public function failing_to_chmod_when_writing(): void
{
$this->connection->failOnChmod('/upload/path.txt');
$adapter = $this->adapter();
$this->expectException(UnableToWriteFile::class);
$adapter->write('path.txt', 'contents', new Config(['visibility' => 'public']));
}
/**
* @test
*/
public function failing_to_move_a_file_cause_the_parent_directory_cant_be_created(): void
{
$adapter = $this->adapterWithInvalidRoot();
$this->expectException(UnableToMoveFile::class);
$adapter->move('path.txt', 'new-path.txt', new Config());
}
/**
* @test
*/
public function failing_to_copy_a_file(): void
{
$adapter = $this->adapterWithInvalidRoot();
$this->expectException(UnableToCopyFile::class);
$adapter->copy('path.txt', 'new-path.txt', new Config());
}
/**
* @test
*/
public function failing_to_copy_a_file_because_writing_fails(): void
{
$this->givenWeHaveAnExistingFile('path.txt', 'contents');
$adapter = $this->adapter();
$this->connection->failOnPut('/upload/new-path.txt');
$this->expectException(UnableToCopyFile::class);
$adapter->copy('path.txt', 'new-path.txt', new Config());
}
/**
* @test
*/
public function failing_to_chmod_when_writing_with_a_stream(): void
{
$writeStream = stream_with_contents('contents');
$this->connection->failOnChmod('/upload/path.txt');
$adapter = $this->adapter();
$this->expectException(UnableToWriteFile::class);
try {
$adapter->writeStream('path.txt', $writeStream, new Config(['visibility' => 'public']));
} finally {
@fclose($writeStream);
}
}
/**
* @test
*/
public function list_contents_directory_does_not_exist(): void
{
$contents = $this->adapter()->listContents('/does_not_exist', false);
$this->assertCount(0, iterator_to_array($contents));
}
private static function connectionProvider(): ConnectionProvider
{
if ( ! static::$connectionProvider instanceof ConnectionProvider) {
static::$connectionProvider = new StubSftpConnectionProvider('localhost', 'foo', 'pass', 2222);
}
return static::$connectionProvider;
}
/**
* @return SftpAdapter
*/
private function adapterWithInvalidRoot(): SftpAdapter
{
$provider = static::connectionProvider();
$adapter = new SftpAdapter($provider, '/invalid');
return $adapter;
}
}