workTypes = new InMemoryWorkTypeRepository; $this->templates = new InMemoryWorkflowTemplateRepository; $transactions = new FakeTransactionManager; $builder = new WorkflowStepBuilder($this->workTypes, $units); $this->add = new AddWorkflowTemplate($this->templates, $this->workTypes, $builder, $transactions); $this->update = new UpdateWorkflowTemplate($this->templates, $this->workTypes, $builder, $transactions); $this->setActive = new SetWorkflowTemplateActive($this->templates, $transactions); $this->setWorkTypeActive = new SetCatalogItemActive($units, $services, $this->workTypes, $transactions); $addUnit = new AddServiceUnit($units, $transactions); $addWorkType = new AddWorkType($units, $services, $this->workTypes, $transactions); $media = $addUnit->execute('Mediatuotanto'); $marketing = $addUnit->execute('Markkinointi'); $shared = (int) $units->save(ServiceUnit::create(new CatalogName('Kaikille yhteiset'), 3, shared: true))->id(); $this->videography = $addWorkType->execute($media, null, 'videokuvaus'); $this->editing = $addWorkType->execute($media, null, 'editointi'); $this->graphics = $addWorkType->execute($marketing, null, 'graafinen työ'); $this->planning = $addWorkType->execute($shared, null, 'suunnittelu'); $this->data = fn (string $name, array $steps, ?int $workTypeId = null): WorkflowTemplateData => new WorkflowTemplateData( $name, $workTypeId ?? $this->videography, array_map(fn (array $step): WorkflowStepData => new WorkflowStepData(...$step), $steps), ); }); it('adds a template with steps in order and their own work types', function () { $id = $this->add->execute(($this->data)('Lyhyt video', [ ['Suunnittelu', null, $this->planning], [' Kuvaus ', 'Kuvauspäivä asiakkaalla'], ['Editointi', '', $this->editing], ])); $template = $this->templates->findById($id); expect($template?->name()->value)->toBe('Lyhyt video') ->and($template?->isActive())->toBeTrue() ->and(array_map(fn ($step) => [$step->title->value, $step->description, $step->workTypeId], $template?->steps() ?? []))->toBe([ ['Suunnittelu', null, $this->planning], ['Kuvaus', 'Kuvauspäivä asiakkaalla', null], ['Editointi', null, $this->editing], ]); }); it('needs at least one step with a title, and tells which step is wrong', function () { expect(fn () => $this->add->execute(($this->data)('Tyhjä', [])))->toThrow(WorkflowTemplateNeedsSteps::class); try { $this->add->execute(($this->data)('Lyhyt video', [['Kuvaus'], [' ']])); $this->fail('The empty title was accepted.'); } catch (InvalidWorkflowStepTitle $exception) { expect($exception->stepIndex)->toBe(1); } }); it('rejects a name the work type already has, but allows it for another work type', function () { $this->add->execute(($this->data)('Lyhyt video', [['Kuvaus']])); $this->add->execute(($this->data)('lyhyt video', [['Kuvaus']], $this->editing)); expect(fn () => $this->add->execute(($this->data)('LYHYT VIDEO', [['Kuvaus']])))->toThrow(WorkflowTemplateNameTaken::class); }); it('allows a step work type only from the same unit or the shared unit, and in use', function () { try { $this->add->execute(($this->data)('Lyhyt video', [['Kuvaus'], ['Grafiikka', null, $this->graphics]])); $this->fail('A work type of another unit was accepted.'); } catch (WorkflowStepWorkTypeNotAllowed $exception) { expect($exception->stepIndex)->toBe(1); } $this->setWorkTypeActive->execute(CatalogLevel::WorkType, $this->editing, false); expect(fn () => $this->add->execute(($this->data)('Lyhyt video', [['Editointi', null, $this->editing]]))) ->toThrow(WorkflowStepWorkTypeNotAllowed::class); }); it('needs a template work type in use', function () { $this->setWorkTypeActive->execute(CatalogLevel::WorkType, $this->videography, false); $this->add->execute(($this->data)('Lyhyt video', [['Kuvaus']])); })->throws(CatalogItemNotFound::class); it('updates name and steps, keeping step work types taken out of use since', function () { $id = $this->add->execute(($this->data)('Lyhyt video', [['Kuvaus'], ['Editointi', null, $this->editing]])); $this->setWorkTypeActive->execute(CatalogLevel::WorkType, $this->editing, false); $this->update->execute($id, ($this->data)('Lyhyt video 2', [['Editointi', null, $this->editing], ['Toimitus']])); $template = $this->templates->findById($id); expect($template?->name()->value)->toBe('Lyhyt video 2') ->and(array_map(fn ($step) => $step->title->value, $template?->steps() ?? []))->toBe(['Editointi', 'Toimitus']); expect(fn () => $this->update->execute(999, ($this->data)('X', [['Kuvaus']])))->toThrow(WorkflowTemplateNotFound::class); }); it('takes a template out of use and back', function () { $id = $this->add->execute(($this->data)('Lyhyt video', [['Kuvaus']])); $this->setActive->execute($id, false); expect($this->templates->findById($id)?->isActive())->toBeFalse(); $this->setActive->execute($id, true); expect($this->templates->findById($id)?->isActive())->toBeTrue(); });