93 lines
4.2 KiB
PHP
93 lines
4.2 KiB
PHP
<?php
|
|
|
|
use App\Modules\TimeTracking\Application\Services\TimeBooking;
|
|
use App\Modules\TimeTracking\Application\UseCases\AddTaskHours;
|
|
use App\Modules\TimeTracking\Application\UseCases\ApproveWeek;
|
|
use App\Modules\TimeTracking\Application\UseCases\CancelWeekApproval;
|
|
use App\Modules\TimeTracking\Application\UseCases\RemoveTimeEntry;
|
|
use App\Modules\TimeTracking\Application\UseCases\SetDayHours;
|
|
use App\Modules\TimeTracking\Application\UseCases\UpdateTimeEntry;
|
|
use App\Modules\TimeTracking\Domain\Exceptions\BillingNotesMissing;
|
|
use App\Modules\TimeTracking\Domain\Exceptions\TimeApproved;
|
|
use Tests\Fakes\FakeClock;
|
|
use Tests\Fakes\FakeTaskQueries;
|
|
use Tests\Fakes\FakeTransactionManager;
|
|
use Tests\Fakes\InMemoryTimeEntryRepository;
|
|
|
|
beforeEach(function () {
|
|
$this->tasks = new FakeTaskQueries;
|
|
$this->entries = new InMemoryTimeEntryRepository;
|
|
$booking = new TimeBooking($this->tasks, $this->entries);
|
|
$transactions = new FakeTransactionManager;
|
|
|
|
$this->setDayHours = new SetDayHours($this->entries, $booking, $transactions);
|
|
$this->addTaskHours = new AddTaskHours($this->entries, $booking, $transactions);
|
|
$this->updateEntry = new UpdateTimeEntry($this->entries, $booking, $transactions);
|
|
$this->removeEntry = new RemoveTimeEntry($this->entries, $booking, $transactions);
|
|
$this->approveWeek = new ApproveWeek($this->entries, new FakeClock(new DateTimeImmutable('2026-11-30')), $transactions);
|
|
$this->cancelApproval = new CancelWeekApproval($this->entries, $transactions);
|
|
|
|
$this->tasks->addTask(1);
|
|
$this->tasks->addTask(2);
|
|
|
|
// Week 48: Monday 23.11.2026.
|
|
$this->setDayHours->execute(10, 1, '2026-11-23', '4');
|
|
$this->setDayHours->execute(10, 1, '2026-11-29', '2');
|
|
$this->setDayHours->execute(10, 1, '2026-11-30', '3');
|
|
$this->setDayHours->execute(11, 1, '2026-11-23', '5');
|
|
|
|
// Every entry has its billing note, which the approval requires.
|
|
foreach ($this->entries->entries as $entry) {
|
|
$entry->describe(null, 'Suunnittelu', null);
|
|
$this->entries->save($entry);
|
|
}
|
|
});
|
|
|
|
it('approves the user\'s unapproved hours of the week only', function () {
|
|
expect($this->approveWeek->execute(10, '2026-11-23', approverId: 99))->toBe(2);
|
|
|
|
$approved = array_filter($this->entries->entries, fn ($entry) => $entry->isApproved());
|
|
|
|
expect(array_map(fn ($entry) => [$entry->userId(), $entry->workedOn()->format('Y-m-d'), $entry->approvedBy()], array_values($approved)))->toBe([
|
|
[10, '2026-11-23', 99],
|
|
[10, '2026-11-29', 99],
|
|
])->and($this->approveWeek->execute(10, '2026-11-23', 99))->toBe(0);
|
|
});
|
|
|
|
it('locks approved hours against every change', function (Closure $change) {
|
|
$this->approveWeek->execute(10, '2026-11-23', 10);
|
|
|
|
expect(fn () => $change->call($this))->toThrow(TimeApproved::class);
|
|
})->with([
|
|
'week cell' => [fn () => $this->setDayHours->execute(10, 1, '2026-11-23', '5')],
|
|
'emptied cell' => [fn () => $this->setDayHours->execute(10, 1, '2026-11-23', '')],
|
|
'task page' => [fn () => $this->addTaskHours->execute(10, 1, '2026-11-23', '1')],
|
|
'edit' => [fn () => $this->updateEntry->execute(10, 1, '1', null)],
|
|
'remove' => [fn () => $this->removeEntry->execute(10, 1)],
|
|
]);
|
|
|
|
it('lets new hours be added to an approved week on another task', function () {
|
|
$this->approveWeek->execute(10, '2026-11-23', 10);
|
|
|
|
$this->setDayHours->execute(10, 2, '2026-11-23', '1');
|
|
|
|
expect($this->entries->findForDay(10, 2, new DateTimeImmutable('2026-11-23'))?->isApproved())->toBeFalse();
|
|
});
|
|
|
|
it('cancels the approval of a week so the hours can be changed again', function () {
|
|
$this->approveWeek->execute(10, '2026-11-23', 99);
|
|
|
|
expect($this->cancelApproval->execute(10, '2026-11-23'))->toBe(2);
|
|
|
|
$this->setDayHours->execute(10, 1, '2026-11-23', '5');
|
|
|
|
expect($this->entries->findForDay(10, 1, new DateTimeImmutable('2026-11-23'))?->hours()->format())->toBe('5');
|
|
});
|
|
|
|
it('refuses to approve a week while an entry has no billing note', function () {
|
|
$this->setDayHours->execute(10, 2, '2026-11-24', '1');
|
|
|
|
expect(fn () => $this->approveWeek->execute(10, '2026-11-23', approverId: 99))->toThrow(BillingNotesMissing::class);
|
|
expect(array_filter($this->entries->entries, fn ($entry) => $entry->isApproved()))->toBeEmpty();
|
|
});
|