tasks = new InMemoryTaskRepository; $this->images = new InMemoryTaskImageRepository; $this->files = new FakeTaskImageFiles; $transactions = new FakeTransactionManager; $finder = new TaskImageFinder($this->tasks, $this->images); $this->jobId = 5; $this->taskId = (int) $this->tasks->save(Task::reconstitute(1, $this->jobId, 11, new TaskTitle('Kuvaus'), null, TaskStatus::Waiting, null, null, null, [], 10))->id(); $this->add = new AddTaskImage($finder, $this->images, $this->files, $transactions, new FakeClock(new DateTimeImmutable('2026-11-25'))); $this->changeCaption = new ChangeTaskImageCaption($finder, $this->images, $transactions); $this->remove = new RemoveTaskImage($finder, $this->images, $this->files, $transactions); $this->removeTask = new RemoveTask($this->tasks, new FakeTaskUsage, $transactions, $this->images, $this->files); }); it('treats an empty caption as none and rejects too long captions', function () { expect(TaskImageCaption::fromNullable(' '))->toBeNull() ->and(TaskImageCaption::fromNullable(null))->toBeNull() ->and(TaskImageCaption::fromNullable(" Valot \n paikallaan ")?->value)->toBe('Valot paikallaan') ->and(fn () => TaskImageCaption::fromNullable(str_repeat('a', 256)))->toThrow(InvalidTaskImageCaption::class); }); it('adds an image with its files, uploader, caption and time', function () { $imageId = $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/upload.jpg', 'IMG_1234.HEIC.jpg', 'Valot'); $image = $this->images->images[$imageId]; expect($image->uploaderId())->toBe(10) ->and($image->caption()?->value)->toBe('Valot') ->and($image->originalName())->toBe('IMG_1234.HEIC.jpg') ->and($image->uploadedAt()->format('Y-m-d H:i'))->toBe('2026-11-25 12:00') ->and(array_keys($this->files->files))->toBe($image->file()->paths()); }); it('stores no files for an unreadable image, an invalid caption or a task of another job', function () { expect(fn () => $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/upload.txt', 'muistio.txt', null))->toThrow(InvalidTaskImage::class) ->and(fn () => $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/upload.jpg', 'kuva.jpg', str_repeat('a', 256)))->toThrow(InvalidTaskImageCaption::class) ->and(fn () => $this->add->execute(99, $this->taskId, 10, '/tmp/upload.jpg', 'kuva.jpg', null))->toThrow(TaskNotFound::class) ->and($this->files->files)->toBe([]); }); it('removes the stored files when the image cannot be saved', function () { $this->images->failOnSave = true; expect(fn () => $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/upload.jpg', 'kuva.jpg', null))->toThrow(RuntimeException::class) ->and($this->files->files)->toBe([]); }); it('lets only the uploader change or clear the caption', function () { $imageId = $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/upload.jpg', 'kuva.jpg', null); $this->changeCaption->execute($this->jobId, $this->taskId, $imageId, 10, 'Lavasteet'); expect($this->images->images[$imageId]->caption()?->value)->toBe('Lavasteet'); $this->changeCaption->execute($this->jobId, $this->taskId, $imageId, 10, ''); expect($this->images->images[$imageId]->caption())->toBeNull() ->and(fn () => $this->changeCaption->execute($this->jobId, $this->taskId, $imageId, 12, 'Toisen'))->toThrow(NotTaskImageUploader::class); }); it('lets the uploader remove an image with its files, and others only when they may remove anyone\'s', function () { $own = $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/a.jpg', 'a.jpg', null); $other = $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/b.jpg', 'b.jpg', null); expect(fn () => $this->remove->execute($this->jobId, $this->taskId, $other, 12, false))->toThrow(NotTaskImageUploader::class); $this->remove->execute($this->jobId, $this->taskId, $own, 10, false); $this->remove->execute($this->jobId, $this->taskId, $other, 12, true); expect($this->images->images)->toBe([]) ->and($this->files->files)->toBe([]) ->and(fn () => $this->remove->execute($this->jobId, $this->taskId, $own, 10, true))->toThrow(TaskImageNotFound::class); }); it('removes the files of the images of a removed task', function () { $this->add->execute($this->jobId, $this->taskId, 10, '/tmp/a.jpg', 'a.jpg', null); $this->removeTask->execute($this->jobId, $this->taskId); expect($this->files->files)->toBe([]); });