1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerTest\Service\FileSystem; |
9
|
|
|
|
10
|
|
|
use Codeception\Configuration; |
11
|
|
|
use Codeception\Test\Unit; |
12
|
|
|
use Generated\Shared\Transfer\FileSystemContentTransfer; |
13
|
|
|
use Generated\Shared\Transfer\FileSystemCopyTransfer; |
14
|
|
|
use Generated\Shared\Transfer\FileSystemCreateDirectoryTransfer; |
15
|
|
|
use Generated\Shared\Transfer\FileSystemDeleteDirectoryTransfer; |
16
|
|
|
use Generated\Shared\Transfer\FileSystemDeleteTransfer; |
17
|
|
|
use Generated\Shared\Transfer\FileSystemListTransfer; |
18
|
|
|
use Generated\Shared\Transfer\FileSystemQueryTransfer; |
19
|
|
|
use Generated\Shared\Transfer\FileSystemRenameTransfer; |
20
|
|
|
use Generated\Shared\Transfer\FileSystemStreamTransfer; |
21
|
|
|
use Generated\Shared\Transfer\FileSystemVisibilityTransfer; |
22
|
|
|
use Spryker\Service\FileSystem\FileSystemDependencyProvider; |
23
|
|
|
use Spryker\Service\FileSystem\FileSystemService; |
24
|
|
|
use Spryker\Service\FileSystem\FileSystemServiceFactory; |
25
|
|
|
use Spryker\Service\FileSystemExtension\Dependency\Exception\FileSystemReadException; |
26
|
|
|
use Spryker\Service\Flysystem\FlysystemDependencyProvider; |
27
|
|
|
use Spryker\Service\Flysystem\FlysystemService; |
28
|
|
|
use Spryker\Service\Flysystem\FlysystemServiceFactory; |
29
|
|
|
use Spryker\Service\Flysystem\Plugin\FileSystem\FileSystemReaderPlugin; |
30
|
|
|
use Spryker\Service\Flysystem\Plugin\FileSystem\FileSystemStreamPlugin; |
31
|
|
|
use Spryker\Service\Flysystem\Plugin\FileSystem\FileSystemWriterPlugin; |
32
|
|
|
use Spryker\Service\FlysystemLocalFileSystem\Plugin\Flysystem\LocalFilesystemBuilderPlugin; |
33
|
|
|
use Spryker\Service\Kernel\Container; |
34
|
|
|
use SprykerTest\Service\FileSystem\Stub\FileSystemConfigStub; |
35
|
|
|
use SprykerTest\Service\FileSystem\Stub\FlysystemConfigStub; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Auto-generated group annotations |
39
|
|
|
* |
40
|
|
|
* @group SprykerTest |
41
|
|
|
* @group Service |
42
|
|
|
* @group FileSystem |
43
|
|
|
* @group FileSystemServiceTest |
44
|
|
|
* Add your own group annotations below this line |
45
|
|
|
*/ |
46
|
|
|
class FileSystemServiceTest extends Unit |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public const RESOURCE_FILE_NAME = 'fileName.jpg'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
public const FILE_SYSTEM_DOCUMENT = 'customerFileSystem'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
public const FILE_SYSTEM_PRODUCT_IMAGE = 'productFileSystem'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
public const ROOT_DIRECTORY = 'fileSystemRoot/uploads/'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
public const PATH_DOCUMENT = 'documents/'; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
public const PATH_PRODUCT_IMAGE = 'images/product/'; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
public const FILE_DOCUMENT = 'customer.txt'; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var string |
85
|
|
|
*/ |
86
|
|
|
public const FILE_PRODUCT_IMAGE = 'image.png'; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
public const FILE_CONTENT = 'Hello World'; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var \Spryker\Service\FileSystem\FileSystemServiceInterface |
95
|
|
|
*/ |
96
|
|
|
protected $fileSystemService; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var string |
100
|
|
|
*/ |
101
|
|
|
protected $testDataFileSystemRootDirectory; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
protected function setUp(): void |
107
|
|
|
{ |
108
|
|
|
parent::setUp(); |
109
|
|
|
|
110
|
|
|
$this->testDataFileSystemRootDirectory = Configuration::dataDir() . static::ROOT_DIRECTORY; |
111
|
|
|
|
112
|
|
|
$container = new Container(); |
113
|
|
|
$container = $this->setupContainerAndFlysystemService($container); |
114
|
|
|
|
115
|
|
|
$config = new FileSystemConfigStub(); |
116
|
|
|
$factory = new FileSystemServiceFactory(); |
117
|
|
|
$factory->setConfig($config); |
118
|
|
|
$factory->setContainer($container); |
119
|
|
|
|
120
|
|
|
$this->fileSystemService = new FileSystemService(); |
121
|
|
|
$this->fileSystemService->setFactory($factory); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param \Spryker\Service\Kernel\Container $container |
126
|
|
|
* |
127
|
|
|
* @return \Spryker\Service\Kernel\Container |
128
|
|
|
*/ |
129
|
|
|
protected function setupContainerAndFlysystemService(Container $container): Container |
130
|
|
|
{ |
131
|
|
|
$flysystemContainer = new Container(); |
132
|
|
|
$flysystemContainer[FlysystemDependencyProvider::PLUGIN_COLLECTION_FILESYSTEM_BUILDER] = function () { |
133
|
|
|
return [ |
134
|
|
|
new LocalFilesystemBuilderPlugin(), |
135
|
|
|
]; |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
$flysystemContainer[FlysystemDependencyProvider::PLUGIN_COLLECTION_FLYSYSTEM] = function () { |
139
|
|
|
return []; |
140
|
|
|
}; |
141
|
|
|
|
142
|
|
|
$flysystemConfig = new FlysystemConfigStub(); |
143
|
|
|
|
144
|
|
|
$flysystemFactory = new FlysystemServiceFactory(); |
145
|
|
|
$flysystemFactory->setContainer($flysystemContainer); |
146
|
|
|
$flysystemFactory->setConfig($flysystemConfig); |
147
|
|
|
|
148
|
|
|
$flysystemService = new FlysystemService(); |
149
|
|
|
$flysystemService->setFactory($flysystemFactory); |
150
|
|
|
|
151
|
|
|
$fileSystemReaderPlugin = new FileSystemReaderPlugin(); |
152
|
|
|
$fileSystemReaderPlugin->setService($flysystemService); |
153
|
|
|
|
154
|
|
|
$fileSystemWriterPlugin = new FileSystemWriterPlugin(); |
155
|
|
|
$fileSystemWriterPlugin->setService($flysystemService); |
156
|
|
|
|
157
|
|
|
$fileSystemStreamPlugin = new FileSystemStreamPlugin(); |
158
|
|
|
$fileSystemStreamPlugin->setService($flysystemService); |
159
|
|
|
|
160
|
|
|
$container[FileSystemDependencyProvider::PLUGIN_READER] = function (Container $container) use ($fileSystemReaderPlugin) { |
161
|
|
|
return $fileSystemReaderPlugin; |
162
|
|
|
}; |
163
|
|
|
|
164
|
|
|
$container[FileSystemDependencyProvider::PLUGIN_WRITER] = function (Container $container) use ($fileSystemWriterPlugin) { |
165
|
|
|
return $fileSystemWriterPlugin; |
166
|
|
|
}; |
167
|
|
|
|
168
|
|
|
$container[FileSystemDependencyProvider::PLUGIN_STREAM] = function (Container $container) use ($fileSystemStreamPlugin) { |
169
|
|
|
return $fileSystemStreamPlugin; |
170
|
|
|
}; |
171
|
|
|
|
172
|
|
|
return $container; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
protected function tearDown(): void |
179
|
|
|
{ |
180
|
|
|
$this->directoryCleanup(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
public function testHasShouldReturnFalseWithNonExistingFile(): void |
187
|
|
|
{ |
188
|
|
|
$fileSystemQueryTransfer = $this->createDocumentQueryTransfer(); |
189
|
|
|
$fileSystemQueryTransfer->setPath('invalid filename'); |
190
|
|
|
|
191
|
|
|
$has = $this->fileSystemService->has($fileSystemQueryTransfer); |
192
|
|
|
|
193
|
|
|
$this->assertFalse($has); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
public function testHasShouldReturnTrueWithExistingFile(): void |
200
|
|
|
{ |
201
|
|
|
$fileSystemQueryTransfer = $this->createDocumentQueryTransfer(); |
202
|
|
|
$this->createDocumentFile(); |
203
|
|
|
|
204
|
|
|
$has = $this->fileSystemService->has($fileSystemQueryTransfer); |
205
|
|
|
|
206
|
|
|
$this->assertTrue($has); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return void |
211
|
|
|
*/ |
212
|
|
|
public function testReadWithNonExistingFileShouldThrowException(): void |
213
|
|
|
{ |
214
|
|
|
$fileSystemQueryTransfer = $this->createDocumentQueryTransfer(); |
215
|
|
|
$fileSystemQueryTransfer->setPath('invalid path'); |
216
|
|
|
|
217
|
|
|
$this->expectException(FileSystemReadException::class); |
218
|
|
|
|
219
|
|
|
$this->fileSystemService->read($fileSystemQueryTransfer); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
|
|
public function testReadWithExistingFileShouldReturnContent(): void |
226
|
|
|
{ |
227
|
|
|
$fileSystemQueryTransfer = $this->createDocumentQueryTransfer(); |
228
|
|
|
$this->createDocumentFile(); |
229
|
|
|
|
230
|
|
|
$contents = $this->fileSystemService->read($fileSystemQueryTransfer); |
231
|
|
|
|
232
|
|
|
$this->assertSame(static::FILE_CONTENT, $contents); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return void |
237
|
|
|
*/ |
238
|
|
|
public function testWrite(): void |
239
|
|
|
{ |
240
|
|
|
$fileSystemContentTransfer = $this->createContentTransfer(); |
241
|
|
|
|
242
|
|
|
$this->fileSystemService->write($fileSystemContentTransfer); |
243
|
|
|
|
244
|
|
|
$this->assertSame(static::FILE_CONTENT, $this->getDocumentFileContent()); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return void |
249
|
|
|
*/ |
250
|
|
|
public function testDelete(): void |
251
|
|
|
{ |
252
|
|
|
$fileSystemDeleteTransfer = new FileSystemDeleteTransfer(); |
253
|
|
|
$fileSystemDeleteTransfer->setFileSystemName(static::FILE_SYSTEM_DOCUMENT); |
254
|
|
|
$fileSystemDeleteTransfer->setPath('foo/' . static::FILE_DOCUMENT); |
255
|
|
|
$this->createDocumentFile(); |
256
|
|
|
|
257
|
|
|
$this->fileSystemService->delete($fileSystemDeleteTransfer); |
258
|
|
|
|
259
|
|
|
$this->assertFileDoesNotExist($this->getDocumentFileName()); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return void |
264
|
|
|
*/ |
265
|
|
|
public function testRename(): void |
266
|
|
|
{ |
267
|
|
|
$fileSystemRenameTransfer = new FileSystemRenameTransfer(); |
268
|
|
|
$fileSystemRenameTransfer->setFileSystemName(static::FILE_SYSTEM_DOCUMENT); |
269
|
|
|
$fileSystemRenameTransfer->setPath('foo/' . static::FILE_DOCUMENT); |
270
|
|
|
$fileSystemRenameTransfer->setNewPath('foo/NEW_' . static::FILE_DOCUMENT); |
271
|
|
|
$this->createDocumentFile(); |
272
|
|
|
|
273
|
|
|
$this->fileSystemService->rename($fileSystemRenameTransfer); |
274
|
|
|
|
275
|
|
|
$originalFile = $this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo/' . static::FILE_DOCUMENT; |
276
|
|
|
$renamedFile = $this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo/NEW_' . static::FILE_DOCUMENT; |
277
|
|
|
|
278
|
|
|
$this->assertFileDoesNotExist($originalFile); |
279
|
|
|
$this->assertFileExists($renamedFile); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return void |
284
|
|
|
*/ |
285
|
|
|
public function testCopy(): void |
286
|
|
|
{ |
287
|
|
|
$fileSystemCopyTransfer = new FileSystemCopyTransfer(); |
288
|
|
|
$fileSystemCopyTransfer->setFileSystemName(static::FILE_SYSTEM_DOCUMENT); |
289
|
|
|
$fileSystemCopyTransfer->setSourcePath('foo/' . static::FILE_DOCUMENT); |
290
|
|
|
$fileSystemCopyTransfer->setDestinationPath('foo/NEW_' . static::FILE_DOCUMENT); |
291
|
|
|
$this->createDocumentFile(); |
292
|
|
|
|
293
|
|
|
$this->fileSystemService->copy($fileSystemCopyTransfer); |
294
|
|
|
|
295
|
|
|
$originalFile = $this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo/' . static::FILE_DOCUMENT; |
296
|
|
|
$copiedFile = $this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo/NEW_' . static::FILE_DOCUMENT; |
297
|
|
|
|
298
|
|
|
$this->assertFileExists($originalFile); |
299
|
|
|
$this->assertFileExists($copiedFile); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @return void |
304
|
|
|
*/ |
305
|
|
|
public function testGetMimeType(): void |
306
|
|
|
{ |
307
|
|
|
$this->createDocumentFile(); |
308
|
|
|
$fileSystemQueryTransfer = $this->createDocumentQueryTransfer(); |
309
|
|
|
|
310
|
|
|
$mimeType = $this->fileSystemService->getMimetype($fileSystemQueryTransfer); |
311
|
|
|
|
312
|
|
|
$this->assertSame('text/plain', $mimeType); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return void |
317
|
|
|
*/ |
318
|
|
|
public function testGetTimestamp(): void |
319
|
|
|
{ |
320
|
|
|
$fileSystemQueryTransfer = $this->createDocumentQueryTransfer(); |
321
|
|
|
|
322
|
|
|
$timestampExpected = time(); |
323
|
|
|
$this->createDocumentFile(null, $timestampExpected); |
324
|
|
|
|
325
|
|
|
$timestamp = $this->fileSystemService->getTimestamp($fileSystemQueryTransfer); |
326
|
|
|
|
327
|
|
|
$this->assertSame($timestamp, $timestampExpected); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @return void |
332
|
|
|
*/ |
333
|
|
|
public function testGetSize(): void |
334
|
|
|
{ |
335
|
|
|
$fileSystemQueryTransfer = $this->createDocumentQueryTransfer(); |
336
|
|
|
$this->createDocumentFile(); |
337
|
|
|
|
338
|
|
|
$file = $this->getDocumentFileName(); |
339
|
|
|
$sizeExpected = filesize($file); |
340
|
|
|
|
341
|
|
|
$size = $this->fileSystemService->getSize($fileSystemQueryTransfer); |
342
|
|
|
|
343
|
|
|
$this->assertSame($sizeExpected, $size); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @return void |
348
|
|
|
*/ |
349
|
|
|
public function testIsPrivate(): void |
350
|
|
|
{ |
351
|
|
|
$fileSystemQueryTransfer = $this->createDocumentQueryTransfer(); |
352
|
|
|
$this->createDocumentFile(); |
353
|
|
|
|
354
|
|
|
$isPrivate = $this->fileSystemService->isPrivate($fileSystemQueryTransfer); |
355
|
|
|
|
356
|
|
|
$this->assertFalse($isPrivate); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @return void |
361
|
|
|
*/ |
362
|
|
|
public function testMarkAsPrivate(): void |
363
|
|
|
{ |
364
|
|
|
$fileSystemQueryTransfer = $this->createDocumentQueryTransfer(); |
365
|
|
|
$fileSystemVisibilityTransfer = $this->createDocumentVisibilityTransfer(); |
366
|
|
|
|
367
|
|
|
$this->createDocumentFile(); |
368
|
|
|
|
369
|
|
|
$isPrivate = $this->fileSystemService->isPrivate($fileSystemQueryTransfer); |
370
|
|
|
$this->assertFalse($isPrivate); |
371
|
|
|
|
372
|
|
|
$this->fileSystemService->markAsPrivate($fileSystemVisibilityTransfer); |
373
|
|
|
|
374
|
|
|
$isPrivate = $this->fileSystemService->isPrivate($fileSystemQueryTransfer); |
375
|
|
|
$this->assertTrue($isPrivate); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @return void |
380
|
|
|
*/ |
381
|
|
|
public function testMarkAsPublic(): void |
382
|
|
|
{ |
383
|
|
|
$fileSystemQueryTransfer = $this->createDocumentQueryTransfer(); |
384
|
|
|
$fileSystemVisibilityTransfer = $this->createDocumentVisibilityTransfer(); |
385
|
|
|
|
386
|
|
|
$this->createDocumentFile(); |
387
|
|
|
|
388
|
|
|
$this->fileSystemService->markAsPublic($fileSystemVisibilityTransfer); |
389
|
|
|
|
390
|
|
|
$isPublic = $this->fileSystemService->isPrivate($fileSystemQueryTransfer); |
391
|
|
|
$this->assertFalse($isPublic); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @return void |
396
|
|
|
*/ |
397
|
|
|
public function testCreateDirectory(): void |
398
|
|
|
{ |
399
|
|
|
$fileSystemCreateDirectoryTransfer = new FileSystemCreateDirectoryTransfer(); |
400
|
|
|
$fileSystemCreateDirectoryTransfer->setFileSystemName(static::FILE_SYSTEM_DOCUMENT); |
401
|
|
|
$fileSystemCreateDirectoryTransfer->setPath('/foo/bar'); |
402
|
|
|
|
403
|
|
|
$this->fileSystemService->createDirectory($fileSystemCreateDirectoryTransfer); |
404
|
|
|
|
405
|
|
|
$dir = $this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo/bar/'; |
406
|
|
|
$this->assertDirectoryExists($dir); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* @return void |
411
|
|
|
*/ |
412
|
|
|
public function testDeleteDirectory(): void |
413
|
|
|
{ |
414
|
|
|
$fileSystemDeleteDirectoryTransfer = new FileSystemDeleteDirectoryTransfer(); |
415
|
|
|
$fileSystemDeleteDirectoryTransfer->setFileSystemName(static::FILE_SYSTEM_DOCUMENT); |
416
|
|
|
$fileSystemDeleteDirectoryTransfer->setPath('foo/bar'); |
417
|
|
|
|
418
|
|
|
$dir = $this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo/bar'; |
419
|
|
|
mkdir($dir, 0777, true); |
420
|
|
|
|
421
|
|
|
$this->fileSystemService->deleteDirectory($fileSystemDeleteDirectoryTransfer); |
422
|
|
|
|
423
|
|
|
$this->assertDirectoryDoesNotExist($dir); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* @return void |
428
|
|
|
*/ |
429
|
|
|
public function testReadStream(): void |
430
|
|
|
{ |
431
|
|
|
$fileSystemStreamTransfer = $this->createStreamTransfer(); |
432
|
|
|
$this->createDocumentFile(); |
433
|
|
|
|
434
|
|
|
$stream = $this->fileSystemService->readStream($fileSystemStreamTransfer); |
435
|
|
|
|
436
|
|
|
$content = stream_get_contents($stream); |
437
|
|
|
if ($stream !== false) { |
438
|
|
|
fclose($stream); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
$this->assertSame(static::FILE_CONTENT, $content); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @return void |
446
|
|
|
*/ |
447
|
|
|
public function testWriteStream(): void |
448
|
|
|
{ |
449
|
|
|
$fileSystemStreamTransfer = $this->createStreamTransfer(); |
450
|
|
|
$this->createDocumentFileInRoot(); |
451
|
|
|
$file = $this->testDataFileSystemRootDirectory . static::FILE_DOCUMENT; |
452
|
|
|
$stream = fopen($file, 'r+'); |
453
|
|
|
|
454
|
|
|
$this->fileSystemService->writeStream($fileSystemStreamTransfer, $stream); |
455
|
|
|
|
456
|
|
|
if ($stream !== false) { |
457
|
|
|
fclose($stream); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
$file = $this->getDocumentFileName(); |
461
|
|
|
$content = file_get_contents($file); |
462
|
|
|
|
463
|
|
|
$this->assertFileExists($file); |
464
|
|
|
$this->assertSame(static::FILE_CONTENT, $content); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* @return void |
469
|
|
|
*/ |
470
|
|
|
public function testListContentsWithoutRecursiveShouldReturnOnlyFirstLevelFiles(): void |
471
|
|
|
{ |
472
|
|
|
// Arrange |
473
|
|
|
$fileSystemListTransfer = (new FileSystemListTransfer()) |
474
|
|
|
->setFileSystemName(static::FILE_SYSTEM_DOCUMENT) |
475
|
|
|
->setPath('/') |
476
|
|
|
->setRecursive(false); |
477
|
|
|
|
478
|
|
|
$this->createDocumentFile(); |
479
|
|
|
$this->createDocumentFileInRoot(); |
480
|
|
|
|
481
|
|
|
// Act |
482
|
|
|
$content = $this->fileSystemService->listContents($fileSystemListTransfer); |
483
|
|
|
|
484
|
|
|
// Assert |
485
|
|
|
$this->assertCount(1, $content); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* @return void |
490
|
|
|
*/ |
491
|
|
|
public function testListContentsWithRecursiveShouldReturnAllLevelsFiles(): void |
492
|
|
|
{ |
493
|
|
|
// Arrange |
494
|
|
|
$fileSystemListTransfer = (new FileSystemListTransfer()) |
495
|
|
|
->setFileSystemName(static::FILE_SYSTEM_DOCUMENT) |
496
|
|
|
->setPath('/') |
497
|
|
|
->setRecursive(true); |
498
|
|
|
|
499
|
|
|
$this->createDocumentFile(); |
500
|
|
|
$this->createDocumentFileInRoot(); |
501
|
|
|
|
502
|
|
|
// Act |
503
|
|
|
$content = $this->fileSystemService->listContents($fileSystemListTransfer); |
504
|
|
|
|
505
|
|
|
// Assert |
506
|
|
|
$this->assertCount(2, $content); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* @param string|null $content |
511
|
|
|
* @param string|null $modifiedTimestamp |
512
|
|
|
* |
513
|
|
|
* @return void |
514
|
|
|
*/ |
515
|
|
|
protected function createDocumentFile(?string $content = null, ?string $modifiedTimestamp = null): void |
516
|
|
|
{ |
517
|
|
|
$dir = $this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo'; |
518
|
|
|
if (!is_dir($dir)) { |
519
|
|
|
mkdir($dir, 0777, true); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
$file = $this->getDocumentFileName(); |
523
|
|
|
|
524
|
|
|
$h = fopen($file, 'w'); |
525
|
|
|
fwrite($h, $content ?: static::FILE_CONTENT); |
526
|
|
|
fclose($h); |
527
|
|
|
|
528
|
|
|
if ($modifiedTimestamp) { |
529
|
|
|
touch($file, $modifiedTimestamp); |
|
|
|
|
530
|
|
|
} |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* @param string|null $content |
535
|
|
|
* @param string|null $modifiedTimestamp |
536
|
|
|
* |
537
|
|
|
* @return void |
538
|
|
|
*/ |
539
|
|
|
protected function createDocumentFileInRoot(?string $content = null, ?string $modifiedTimestamp = null): void |
540
|
|
|
{ |
541
|
|
|
$file = $this->testDataFileSystemRootDirectory . static::FILE_DOCUMENT; |
542
|
|
|
|
543
|
|
|
$h = fopen($file, 'w'); |
544
|
|
|
fwrite($h, $content ?: static::FILE_CONTENT); |
545
|
|
|
fclose($h); |
546
|
|
|
|
547
|
|
|
if ($modifiedTimestamp) { |
548
|
|
|
touch($file, $modifiedTimestamp); |
|
|
|
|
549
|
|
|
} |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* @return string|bool |
554
|
|
|
*/ |
555
|
|
|
protected function getDocumentFileContent() |
556
|
|
|
{ |
557
|
|
|
$file = $this->getDocumentFileName(); |
558
|
|
|
if (!is_file($file)) { |
559
|
|
|
return false; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
return file_get_contents($file); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* @return void |
567
|
|
|
*/ |
568
|
|
|
protected function directoryCleanup(): void |
569
|
|
|
{ |
570
|
|
|
foreach ($this->getFileListForCleanup() as $file) { |
571
|
|
|
if (is_file($file)) { |
572
|
|
|
unlink($file); |
573
|
|
|
} |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
foreach ($this->getDirectoryListForCleanup() as $dir) { |
577
|
|
|
if (is_dir($dir)) { |
578
|
|
|
rmdir($dir); |
579
|
|
|
} |
580
|
|
|
} |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* @return list<string> |
585
|
|
|
*/ |
586
|
|
|
protected function getFileListForCleanup(): array |
587
|
|
|
{ |
588
|
|
|
return [ |
589
|
|
|
$this->getDocumentFileName(), |
590
|
|
|
$this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo/NEW_' . static::FILE_DOCUMENT, |
591
|
|
|
$this->testDataFileSystemRootDirectory . static::PATH_PRODUCT_IMAGE . static::FILE_PRODUCT_IMAGE, |
592
|
|
|
$this->testDataFileSystemRootDirectory . static::FILE_DOCUMENT, |
593
|
|
|
]; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* @return list<string> |
598
|
|
|
*/ |
599
|
|
|
protected function getDirectoryListForCleanup(): array |
600
|
|
|
{ |
601
|
|
|
return [ |
602
|
|
|
$this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'bar', |
603
|
|
|
$this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo/bar', |
604
|
|
|
$this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo', |
605
|
|
|
$this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT, |
606
|
|
|
$this->testDataFileSystemRootDirectory . static::PATH_PRODUCT_IMAGE, |
607
|
|
|
$this->testDataFileSystemRootDirectory . 'images/', |
608
|
|
|
]; |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* @return \Generated\Shared\Transfer\FileSystemQueryTransfer |
613
|
|
|
*/ |
614
|
|
|
protected function createDocumentQueryTransfer(): FileSystemQueryTransfer |
615
|
|
|
{ |
616
|
|
|
$fileSystemQueryTransfer = new FileSystemQueryTransfer(); |
617
|
|
|
$fileSystemQueryTransfer->setFileSystemName(static::FILE_SYSTEM_DOCUMENT); |
618
|
|
|
$fileSystemQueryTransfer->setPath('/foo/' . static::FILE_DOCUMENT); |
619
|
|
|
|
620
|
|
|
return $fileSystemQueryTransfer; |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* @return \Generated\Shared\Transfer\FileSystemVisibilityTransfer |
625
|
|
|
*/ |
626
|
|
|
protected function createDocumentVisibilityTransfer(): FileSystemVisibilityTransfer |
627
|
|
|
{ |
628
|
|
|
$fileSystemVisibilityTransfer = new FileSystemVisibilityTransfer(); |
629
|
|
|
$fileSystemVisibilityTransfer->setFileSystemName(static::FILE_SYSTEM_DOCUMENT); |
630
|
|
|
$fileSystemVisibilityTransfer->setPath('/foo/' . static::FILE_DOCUMENT); |
631
|
|
|
|
632
|
|
|
return $fileSystemVisibilityTransfer; |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
/** |
636
|
|
|
* @return \Generated\Shared\Transfer\FileSystemContentTransfer |
637
|
|
|
*/ |
638
|
|
|
protected function createContentTransfer(): FileSystemContentTransfer |
639
|
|
|
{ |
640
|
|
|
$fileSystemContentTransfer = new FileSystemContentTransfer(); |
641
|
|
|
$fileSystemContentTransfer->setFileSystemName(static::FILE_SYSTEM_DOCUMENT); |
642
|
|
|
$fileSystemContentTransfer->setPath('foo/' . static::FILE_DOCUMENT); |
643
|
|
|
$fileSystemContentTransfer->setContent(static::FILE_CONTENT); |
644
|
|
|
|
645
|
|
|
return $fileSystemContentTransfer; |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* @return \Generated\Shared\Transfer\FileSystemStreamTransfer |
650
|
|
|
*/ |
651
|
|
|
protected function createStreamTransfer(): FileSystemStreamTransfer |
652
|
|
|
{ |
653
|
|
|
$fileSystemStreamTransfer = new FileSystemStreamTransfer(); |
654
|
|
|
$fileSystemStreamTransfer->setFileSystemName(static::FILE_SYSTEM_DOCUMENT); |
655
|
|
|
$fileSystemStreamTransfer->setPath('foo/' . static::FILE_DOCUMENT); |
656
|
|
|
|
657
|
|
|
return $fileSystemStreamTransfer; |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* @return string |
662
|
|
|
*/ |
663
|
|
|
protected function getDocumentFileName(): string |
664
|
|
|
{ |
665
|
|
|
return $this->testDataFileSystemRootDirectory . static::PATH_DOCUMENT . 'foo/' . static::FILE_DOCUMENT; |
666
|
|
|
} |
667
|
|
|
} |
668
|
|
|
|