jobs = new InMemoryCustomerJobRepository; $this->tasks = new InMemoryTaskRepository; $customers = new FakeCustomerQueries; $catalog = new FakeCatalogQueries; $users = new FakeUserAccountQueries; $this->templates = new FakeWorkflowTemplateQueries; $transactions = new FakeTransactionManager; $customers->customers = [1 => 'Mainos Oy']; $catalog->addServiceUnit(1, 'Mediatuotanto'); $catalog->addServiceUnit(2, 'Painotuotanto'); $catalog->addServiceUnit(9, 'Kaikille yhteiset', shared: true); $catalog->addWorkType(11, 1, 'videokuvaus'); $catalog->addWorkType(12, 1, 'editointi'); $catalog->addWorkType(21, 2, 'painotyƶ'); $catalog->addWorkType(91, 9, 'suunnittelu'); $users->addUser(10, 'Eero'); $createJob = new CreateCustomerJob($this->jobs, $this->jobs, new JobDetailsFactory($customers, $catalog, $users), $transactions, new FakeClock(new DateTimeImmutable('2026-05-01'))); $this->jobId = $createJob->execute(new CustomerJobData('Video', 1, [1], 10, 'one_off', 'quote_request', 'normal'), 10); $this->tentativeJobId = $createJob->execute(new CustomerJobData('Alustava', 1, [1], 10, 'one_off', 'tentative', 'normal'), 10); $this->templates->addTemplate(1, 'Lyhyt video', 11, 1, [['Suunnittelu', 91], ['Kuvaus'], ['Editointi', 12]]); $this->templates->addTemplate(2, 'Painos', 21, 2, [['Painatus']]); $this->templates->addTemplate(3, 'Vanha', 11, 1, [['Kuvaus']], active: false); $this->templates->addTemplate(4, 'Rikki', 11, 1, [['Painatus', 21]]); $this->addTasks = new AddTasksFromWorkflowTemplate($this->jobs, $this->tasks, $this->templates, new TaskReferenceChecker($catalog, $users), $transactions); }); it('adds a waiting task per step in order, with the step work type and the adder as assignee', function () { expect($this->addTasks->execute($this->jobId, 1, 10))->toBe(3); $tasks = array_values($this->tasks->tasks); expect(array_map(fn (Task $task): array => [$task->title()->value, $task->workTypeId(), $task->status(), $task->assigneeUserIds()], $tasks))->toBe([ ['Suunnittelu', 91, TaskStatus::Waiting, [10]], ['Kuvaus', 11, TaskStatus::Waiting, [10]], ['Editointi', 12, TaskStatus::Waiting, [10]], ]); }); it('rejects a template out of use or for other service units', function (int $templateId) { $this->addTasks->execute($this->jobId, $templateId, 10); })->with([3, 2, 999])->throws(WorkflowTemplateNotAvailable::class); it('rejects a step whose work type is not available for the job', function () { expect(fn () => $this->addTasks->execute($this->jobId, 4, 10))->toThrow(InvalidTaskReference::class); }); it('adds no tasks before the job is a quote request', function () { $this->addTasks->execute($this->tentativeJobId, 1, 10); })->throws(TasksNotAllowedYet::class); it('gives each task the due date and estimate entered for its step', function () { $this->addTasks->execute($this->jobId, 1, 10, [ new TemplateTaskSchedule('2026-05-10', '1,5'), new TemplateTaskSchedule(null, '8'), ]); $tasks = array_values($this->tasks->tasks); expect(array_map(fn (Task $task): array => [$task->dueOn()?->format('Y-m-d'), $task->estimatedHours()?->format()], $tasks))->toBe([ ['2026-05-10', '1.5'], [null, '8'], [null, null], ]); }); it('rejects an estimate that is not a number', function () { $this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule(null, 'paljon')]); })->throws(InvalidEstimatedHours::class);