-
Notifications
You must be signed in to change notification settings - Fork 18
/
ZipArchiveAdapterTestCase.php
336 lines (270 loc) · 9.96 KB
/
ZipArchiveAdapterTestCase.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
declare(strict_types=1);
namespace League\Flysystem\ZipArchive;
use Generator;
use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase;
use League\Flysystem\Config;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\UnableToCopyFile;
use League\Flysystem\UnableToCreateDirectory;
use League\Flysystem\UnableToDeleteDirectory;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToSetVisibility;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\Visibility;
use function iterator_to_array;
/**
* @group zip
*/
abstract class ZipArchiveAdapterTestCase extends FilesystemAdapterTestCase
{
private const ARCHIVE = __DIR__ . '/test.zip';
/**
* @var StubZipArchiveProvider
*/
private static $archiveProvider;
protected function setUp(): void
{
static::$adapter = static::createFilesystemAdapter();
static::removeZipArchive();
parent::setUp();
}
public static function tearDownAfterClass(): void
{
static::removeZipArchive();
}
protected function tearDown(): void
{
static::removeZipArchive();
}
protected static function createFilesystemAdapter(): FilesystemAdapter
{
static::$archiveProvider = new StubZipArchiveProvider(self::ARCHIVE);
return new ZipArchiveAdapter(self::$archiveProvider, static::getRoot());
}
abstract protected static function getRoot(): string;
/**
* @test
*/
public function not_being_able_to_create_the_parent_directory(): void
{
$this->expectException(UnableToCreateParentDirectory::class);
(new ZipArchiveAdapter(new StubZipArchiveProvider('/no-way/this/will/work')))
->write('haha', 'lol', new Config());
}
/**
* @test
*/
public function not_being_able_to_write_a_file_because_the_parent_directory_could_not_be_created(): void
{
self::$archiveProvider->stubbedZipArchive()->failNextDirectoryCreation();
$this->expectException(UnableToWriteFile::class);
$this->adapter()->write('directoryName/is-here/filename.txt', 'contents', new Config());
}
/**
* @test
*
* @dataProvider scenariosThatCauseWritesToFail
*/
public function scenarios_that_cause_writing_a_file_to_fail(callable $scenario): void
{
$this->runScenario($scenario);
$this->expectException(UnableToWriteFile::class);
$this->runScenario(function () {
$handle = stream_with_contents('contents');
$this->adapter()->writeStream('some/path.txt', $handle, new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC]));
is_resource($handle) && @fclose($handle);
});
}
public static function scenariosThatCauseWritesToFail(): Generator
{
yield "writing a file fails when writing" => [function () {
static::$archiveProvider->stubbedZipArchive()->failNextWrite();
}];
yield "writing a file fails when setting visibility" => [function () {
static::$archiveProvider->stubbedZipArchive()->failWhenSettingVisibility();
}];
yield "writing a file fails to get the stream contents" => [function () {
mock_function('stream_get_contents', false);
}];
}
/**
* @test
*/
public function failing_to_delete_a_file(): void
{
$this->givenWeHaveAnExistingFile('path.txt');
static::$archiveProvider->stubbedZipArchive()->failNextDeleteName();
$this->expectException(UnableToDeleteFile::class);
$this->adapter()->delete('path.txt');
}
/**
* @test
*/
public function deleting_a_directory(): void
{
$this->givenWeHaveAnExistingFile('a.txt');
$this->givenWeHaveAnExistingFile('one/a.txt');
$this->givenWeHaveAnExistingFile('one/b.txt');
$this->givenWeHaveAnExistingFile('two/a.txt');
$items = iterator_to_array($this->adapter()->listContents('', true));
$this->assertCount(6, $items);
$this->adapter()->deleteDirectory('one');
$items = iterator_to_array($this->adapter()->listContents('', true));
$this->assertCount(3, $items);
}
/**
* @test
*/
public function deleting_a_prefixed_directory(): void
{
$this->givenWeHaveAnExistingFile('a.txt');
$this->givenWeHaveAnExistingFile('/one/a.txt');
$this->givenWeHaveAnExistingFile('one/b.txt');
$this->givenWeHaveAnExistingFile('two/a.txt');
$items = iterator_to_array($this->adapter()->listContents('', true));
$this->assertCount(6, $items);
$this->adapter()->deleteDirectory('one');
$items = iterator_to_array($this->adapter()->listContents('', true));
$this->assertCount(3, $items);
}
/**
* @test
*/
public function list_root_directory(): void
{
$this->givenWeHaveAnExistingFile('a.txt');
$this->givenWeHaveAnExistingFile('one/a.txt');
$this->givenWeHaveAnExistingFile('one/b.txt');
$this->givenWeHaveAnExistingFile('two/a.txt');
$this->assertCount(6, iterator_to_array($this->adapter()->listContents('', true)));
$this->assertCount(3, iterator_to_array($this->adapter()->listContents('', false)));
}
/**
* @test
*/
public function failing_to_create_a_directory(): void
{
static::$archiveProvider->stubbedZipArchive()->failNextDirectoryCreation();
$this->expectException(UnableToCreateDirectory::class);
$this->adapter()->createDirectory('somewhere', new Config);
}
/**
* @test
*/
public function failing_to_create_a_directory_because_setting_visibility_fails(): void
{
static::$archiveProvider->stubbedZipArchive()->failWhenSettingVisibility();
$this->expectException(UnableToCreateDirectory::class);
$this->adapter()->createDirectory('somewhere', new Config([Config::OPTION_DIRECTORY_VISIBILITY => Visibility::PRIVATE]));
}
/**
* @test
*/
public function failing_to_delete_a_directory(): void
{
static::$archiveProvider->stubbedZipArchive()->failWhenDeletingAnIndex();
$this->givenWeHaveAnExistingFile('here/path.txt');
$this->expectException(UnableToDeleteDirectory::class);
$this->adapter()->deleteDirectory('here');
}
/**
* @test
*/
public function setting_visibility_on_a_directory(): void
{
$adapter = $this->adapter();
$adapter->createDirectory('pri-dir', new Config([Config::OPTION_DIRECTORY_VISIBILITY => Visibility::PRIVATE]));
$adapter->createDirectory('pub-dir', new Config([Config::OPTION_DIRECTORY_VISIBILITY => Visibility::PUBLIC]));
$this->expectNotToPerformAssertions();
}
/**
* @test
*/
public function failing_to_move_a_file(): void
{
$this->givenWeHaveAnExistingFile('somewhere/here.txt');
static::$archiveProvider->stubbedZipArchive()->failNextDirectoryCreation();
$this->expectException(UnableToMoveFile::class);
$this->adapter()->move('somewhere/here.txt', 'to-here/path.txt', new Config);
}
/**
* @test
*/
public function failing_to_copy_a_file(): void
{
$this->givenWeHaveAnExistingFile('here.txt');
static::$archiveProvider->stubbedZipArchive()->failNextWrite();
$this->expectException(UnableToCopyFile::class);
$this->adapter()->copy('here.txt', 'here.txt', new Config);
}
/**
* @test
*/
public function failing_to_set_visibility_because_the_file_does_not_exist(): void
{
$this->expectException(UnableToSetVisibility::class);
$this->adapter()->setVisibility('path.txt', Visibility::PUBLIC);
}
/**
* @test
*/
public function deleting_a_directory_with_files_in_it(): void
{
$this->givenWeHaveAnExistingFile('nested/path-a.txt');
$this->givenWeHaveAnExistingFile('nested/path-b.txt');
$this->adapter()->deleteDirectory('nested');
$listing = iterator_to_array($this->adapter()->listContents('', true));
self::assertEquals([], $listing);
}
/**
* @test
*/
public function failing_to_set_visibility_because_setting_it_fails(): void
{
$this->givenWeHaveAnExistingFile('path.txt');
static::$archiveProvider->stubbedZipArchive()->failWhenSettingVisibility();
$this->expectException(UnableToSetVisibility::class);
$this->adapter()->setVisibility('path.txt', Visibility::PUBLIC);
}
/**
* @test
*
* @fixme Move to FilesystemAdapterTestCase once all adapters pass
*/
public function moving_a_file_and_overwriting(): void
{
$this->runScenario(function () {
$adapter = $this->adapter();
$adapter->write(
'source.txt',
'contents to be moved',
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
);
$adapter->write(
'destination.txt',
'contents to be overwritten',
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
);
$adapter->move('source.txt', 'destination.txt', new Config());
$this->assertFalse(
$adapter->fileExists('source.txt'),
'After moving a file should no longer exist in the original location.'
);
$this->assertTrue(
$adapter->fileExists('destination.txt'),
'After moving, a file should be present at the new location.'
);
$this->assertEquals(Visibility::PUBLIC, $adapter->visibility('destination.txt')->visibility());
$this->assertEquals('contents to be moved', $adapter->read('destination.txt'));
});
}
protected static function removeZipArchive(): void
{
if ( ! file_exists(self::ARCHIVE)) {
return;
}
unlink(self::ARCHIVE);
}
}