Files
Toiminnanohjaus/tests/Fakes/InMemoryCustomerJobRepository.php
T
petriandClaude Opus 5.5 c149c56388 Add the quoted amount and completion time to customer jobs
- Jobs get a budget / quoted amount (cents, VAT 0). Only users who may
  see quotes see and edit it on the job form and page, and only they see
  its changes in the history. Others' saves keep the stored amount: the
  server decides whether the amount is set, not the browser.
- Jobs record when they were done: set when a job first reaches a status
  that locks its hours, kept through the billing statuses, cleared when
  management opens the lock. The billing export will use it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 11:46:36 +02:00

62 lines
1.8 KiB
PHP

<?php
namespace Tests\Fakes;
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\JobNumberSequence;
use App\Modules\Jobs\Domain\Entities\CustomerJob;
use App\Modules\Jobs\Domain\ValueObjects\FieldChange;
use App\Modules\Jobs\Domain\ValueObjects\JobNumber;
/**
* Customer jobs, their history and the yearly numbering, in memory.
*/
class InMemoryCustomerJobRepository implements CustomerJobRepository, JobNumberSequence
{
/** @var array<int, CustomerJob> */
public array $jobs = [];
/** @var list<array{int, int, FieldChange}> job id, user id, change */
public array $changes = [];
/** @var array<int, int> */
private array $lastNumbers = [];
private int $nextId = 1;
public function findById(int $id): ?CustomerJob
{
return isset($this->jobs[$id]) ? clone $this->jobs[$id] : null;
}
public function save(CustomerJob $job): CustomerJob
{
$id = $job->id() ?? $this->nextId++;
$this->jobs[$id] = CustomerJob::reconstitute($id, $job->number(), $job->details(), $job->createdBy(), $job->completedAt());
return clone $this->jobs[$id];
}
public function recordChanges(int $jobId, int $userId, array $changes): void
{
foreach ($changes as $change) {
$this->changes[] = [$jobId, $userId, $change];
}
}
public function next(int $year): JobNumber
{
$this->lastNumbers[$year] = ($this->lastNumbers[$year] ?? 0) + 1;
return new JobNumber($year, $this->lastNumbers[$year]);
}
/**
* @return list<string>
*/
public function changedFields(int $jobId): array
{
return array_values(array_map(fn (array $row): string => $row[2]->field, array_filter($this->changes, fn (array $row): bool => $row[0] === $jobId)));
}
}