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