|
15 | 15 |
|
16 | 16 | use CleverAge\FlysystemProcessBundle\Task\ListContentTask; |
17 | 17 | use CleverAge\ProcessBundle\Model\ProcessState; |
| 18 | +use League\Flysystem\DirectoryListing; |
| 19 | +use League\Flysystem\FileAttributes; |
| 20 | +use League\Flysystem\FilesystemOperator; |
18 | 21 | use League\Flysystem\StorageAttributes; |
19 | 22 | use PHPUnit\Framework\Attributes\CoversClass; |
| 23 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
20 | 24 | use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; |
21 | 25 |
|
22 | 26 | #[CoversClass(ListContentTask::class)] |
@@ -57,6 +61,46 @@ public function testEachExecutionListsTheStorageAgain(): void |
57 | 61 | self::assertSame(2, $this->listings['source']); |
58 | 62 | } |
59 | 63 |
|
| 64 | + /** |
| 65 | + * The listing is read item by item, not loaded in an array: only the items already output (and the one being |
| 66 | + * output) are read from the listing. |
| 67 | + */ |
| 68 | + public function testListingIsReadLazily(): void |
| 69 | + { |
| 70 | + $read = 0; |
| 71 | + $listing = new DirectoryListing((static function () use (&$read): \Generator { |
| 72 | + foreach (['a.txt', 'b.txt', 'c.txt'] as $path) { |
| 73 | + ++$read; |
| 74 | + yield new FileAttributes($path); |
| 75 | + } |
| 76 | + })()); |
| 77 | + $filesystem = $this->createStub(FilesystemOperator::class); |
| 78 | + $filesystem->method('listContents')->willReturn($listing); |
| 79 | + $task = new ListContentTask(new ServiceLocator(['source' => static fn (): FilesystemOperator => $filesystem])); |
| 80 | + $state = $this->createState(ListContentTask::class, ['filesystem' => 'source']); |
| 81 | + $task->initialize($state); |
| 82 | + |
| 83 | + $state->reset(true); |
| 84 | + $task->execute($state); |
| 85 | + self::assertSame(1, $read); |
| 86 | + self::assertTrue($task->next($state)); |
| 87 | + $task->execute($state); |
| 88 | + self::assertSame(2, $read); |
| 89 | + self::assertTrue($task->next($state)); |
| 90 | + $task->execute($state); |
| 91 | + self::assertFalse($task->next($state)); |
| 92 | + self::assertSame(3, $read); |
| 93 | + } |
| 94 | + |
| 95 | + public function testNoItemMatchingThePattern(): void |
| 96 | + { |
| 97 | + $this->createFiles('source', ['a.txt' => 'a']); |
| 98 | + [$task, $state] = $this->createTask(['file_pattern' => '/\.csv$/']); |
| 99 | + |
| 100 | + self::assertSame([], $this->iterate($task, $state, null)); |
| 101 | + self::assertFalse($task->next($state)); |
| 102 | + } |
| 103 | + |
60 | 104 | public function testNextBeforeExecute(): void |
61 | 105 | { |
62 | 106 | [$task, $state] = $this->createTask([]); |
|
0 commit comments