The task calendar's daily maximum moves from the hidden setting jobs.daily_capacity_hours to shared.daily_capacity_hours, edited by the admin on the company details page. An empty field means 7.5 hours. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
177 lines
8.6 KiB
PHP
177 lines
8.6 KiB
PHP
<?php
|
|
|
|
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
|
|
use App\Modules\Jobs\Application\DTOs\TemplateTaskSchedule;
|
|
use App\Modules\Jobs\Application\Services\JobDetailsFactory;
|
|
use App\Modules\Jobs\Application\Services\TaskPlanner;
|
|
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
|
|
use App\Modules\Jobs\Application\UseCases\AddTasksFromWorkflowTemplate;
|
|
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
|
|
use App\Modules\Jobs\Domain\Entities\Task;
|
|
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
|
use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
|
|
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
|
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
|
|
use App\Modules\Jobs\Domain\Exceptions\WorkflowTemplateNotAvailable;
|
|
use App\Modules\Shared\Application\UseCases\DailyCapacity;
|
|
use App\Modules\Shared\Application\UseCases\HolidayCalendar;
|
|
use Tests\Fakes\FakeCatalogQueries;
|
|
use Tests\Fakes\FakeClock;
|
|
use Tests\Fakes\FakeCustomerQueries;
|
|
use Tests\Fakes\FakeEventDispatcher;
|
|
use Tests\Fakes\FakeSettings;
|
|
use Tests\Fakes\FakeTransactionManager;
|
|
use Tests\Fakes\FakeUserAccountQueries;
|
|
use Tests\Fakes\FakeWorkflowTemplateQueries;
|
|
use Tests\Fakes\InMemoryCustomerJobRepository;
|
|
use Tests\Fakes\InMemoryHolidayOverrides;
|
|
use Tests\Fakes\InMemoryTaskBookingRepository;
|
|
use Tests\Fakes\InMemoryTaskRepository;
|
|
|
|
beforeEach(function () {
|
|
$this->events = new FakeEventDispatcher;
|
|
$this->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');
|
|
$users->addUser(11, 'Aino');
|
|
$users->addUser(12, 'Pois', active: false);
|
|
|
|
$createJob = new CreateCustomerJob($this->jobs, $this->jobs, new JobDetailsFactory($customers, $catalog, $users), $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')), $this->events);
|
|
$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->bookings = new InMemoryTaskBookingRepository($this->tasks);
|
|
$planner = new TaskPlanner($this->bookings, new HolidayCalendar(new InMemoryHolidayOverrides), new DailyCapacity(tap(new FakeSettings, fn (FakeSettings $settings) => $settings->put(DailyCapacity::KEY, '8'))));
|
|
$this->addTasks = new AddTasksFromWorkflowTemplate($this->jobs, $this->tasks, $this->templates, new TaskReferenceChecker($catalog, $users), $planner, new FakeClock(new DateTimeImmutable('2026-05-01')), $transactions, $this->events);
|
|
});
|
|
|
|
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)->added)->toBe(3);
|
|
|
|
$tasks = array_values($this->tasks->tasks);
|
|
|
|
expect(array_map(fn (Task $task): array => [$task->title()->value, $task->workTypeId(), $task->status(), $task->assigneeUserId(), $task->isScheduled()], $tasks))->toBe([
|
|
['Suunnittelu', 91, TaskStatus::Waiting, 10, false],
|
|
['Kuvaus', 11, TaskStatus::Waiting, 10, false],
|
|
['Editointi', 12, TaskStatus::Waiting, 10, false],
|
|
]);
|
|
});
|
|
|
|
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);
|
|
|
|
/**
|
|
* @param array<int, Task> $tasks
|
|
* @return list<array{0: ?string, 1: ?string}>
|
|
*/
|
|
function plannedPeriods(array $tasks): array
|
|
{
|
|
return array_map(fn (Task $task): array => [$task->plannedFrom()?->format('Y-m-d'), $task->plannedTo()?->format('Y-m-d')], array_values($tasks));
|
|
}
|
|
|
|
it('gives each task the assignee chosen for its step', function () {
|
|
$this->addTasks->execute($this->jobId, 1, 10, [
|
|
new TemplateTaskSchedule(null, null, 11),
|
|
new TemplateTaskSchedule(null, null, null),
|
|
new TemplateTaskSchedule(null, null, 10),
|
|
]);
|
|
|
|
expect(array_map(fn (Task $task): ?int => $task->assigneeUserId(), array_values($this->tasks->tasks)))->toBe([11, null, 10]);
|
|
});
|
|
|
|
it('rejects an inactive assignee', function () {
|
|
try {
|
|
$this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule(null, null, 12)]);
|
|
$this->fail('No exception');
|
|
} catch (InvalidTaskReference $exception) {
|
|
expect($exception->field)->toBe('assignee');
|
|
}
|
|
});
|
|
|
|
it('schedules tasks to end on their due dates, booking backwards from the due date past full days', function () {
|
|
$result = $this->addTasks->execute($this->jobId, 1, 10, [
|
|
new TemplateTaskSchedule('2026-05-12', '12', 10),
|
|
new TemplateTaskSchedule('2026-05-12', '6', 10),
|
|
new TemplateTaskSchedule(null, '2', 10),
|
|
], scheduleToDueDates: true);
|
|
|
|
[$first, $second] = array_keys($this->tasks->tasks);
|
|
|
|
expect([$result->added, $result->scheduled, $result->late])->toBe([3, 2, 0])
|
|
->and(plannedPeriods($this->tasks->tasks))->toBe([['2026-05-11', '2026-05-12'], ['2026-05-08', '2026-05-12'], [null, null]])
|
|
->and($this->bookings->of($first, 10))->toBe(['2026-05-11' => 240, '2026-05-12' => 480])
|
|
->and($this->bookings->of($second, 10))->toBe(['2026-05-08' => 120, '2026-05-11' => 240]);
|
|
});
|
|
|
|
it('does not schedule before today and lets the rest run past the due date', function () {
|
|
$result = $this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule('2026-05-04', '20', 10)], scheduleToDueDates: true);
|
|
$taskId = array_key_first($this->tasks->tasks);
|
|
|
|
// Today, 1.5., is May Day, so only Monday 4.5. is free before the due date.
|
|
expect([$result->scheduled, $result->late])->toBe([1, 1])
|
|
->and(plannedPeriods([$this->tasks->tasks[$taskId]]))->toBe([['2026-05-04', '2026-05-06']])
|
|
->and($this->bookings->of($taskId, 10))->toBe(['2026-05-04' => 480, '2026-05-05' => 480, '2026-05-06' => 240]);
|
|
});
|
|
|
|
it('gives a task without an assignee the working days its estimate needs before the due date, without bookings', function () {
|
|
$this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule('2026-05-12', '12', null)], scheduleToDueDates: true);
|
|
$taskId = array_key_first($this->tasks->tasks);
|
|
|
|
expect(plannedPeriods([$this->tasks->tasks[$taskId]]))->toBe([['2026-05-11', '2026-05-12']])
|
|
->and($this->bookings->bookings[$taskId] ?? [])->toBe([]);
|
|
});
|
|
|
|
it('leaves the tasks unscheduled when scheduling to due dates is off', function () {
|
|
$result = $this->addTasks->execute($this->jobId, 1, 10, [new TemplateTaskSchedule('2026-05-12', '12', 10)]);
|
|
|
|
expect($result->scheduled)->toBe(0)
|
|
->and(plannedPeriods($this->tasks->tasks)[0])->toBe([null, null]);
|
|
});
|