tasks = new FakeUpcomingTasks; $this->notifications = new InMemoryNotificationRepository; $this->clock = new FakeClock(new DateTimeImmutable('2026-11-26')); $this->notify = new NotifyTasksStartingSoon( $this->tasks, $this->notifications, new WorkingDays(new HolidayCalendar(new InMemoryHolidayOverrides)), new FakeTransactionManager, $this->clock, ); }); function upcomingTask(int $id, string $startsOn, array $recipients = [10]): UpcomingTask { return new UpcomingTask($id, 'Tehtävä '.$id, 5, '2026-0042', 'Messuvideo', $startsOn, $recipients); } it('notifies on Thursday about tasks starting on Friday, not later ones or today\'s', function () { $this->tasks->tasks = [upcomingTask(1, '2026-11-26'), upcomingTask(2, '2026-11-27'), upcomingTask(3, '2026-11-30')]; expect($this->notify->execute())->toBe(1) ->and($this->notifications->recipients())->toBe(['10:2']); }); it('notifies on Friday about tasks starting on Saturday to Monday', function () { $this->clock->today = new DateTimeImmutable('2026-11-27'); $this->tasks->tasks = [upcomingTask(1, '2026-11-28'), upcomingTask(2, '2026-11-30'), upcomingTask(3, '2026-12-01')]; $this->notify->execute(); expect($this->notifications->recipients())->toBe(['10:1', '10:2']); }); it('skips holidays: before Christmas it notifies up to the next working day after them', function () { $this->clock->today = new DateTimeImmutable('2026-12-23'); $this->tasks->tasks = [upcomingTask(1, '2026-12-28'), upcomingTask(2, '2026-12-29')]; $this->notify->execute(); expect($this->notifications->recipients())->toBe(['10:1']); }); it('notifies nobody on a day off', function () { $this->clock->today = new DateTimeImmutable('2026-11-29'); $this->tasks->tasks = [upcomingTask(1, '2026-11-30')]; expect($this->notify->execute())->toBe(0) ->and($this->notifications->notifications)->toBe([]); }); it('notifies each recipient once, also when run again', function () { $this->tasks->tasks = [upcomingTask(1, '2026-11-27', [10, 12])]; expect($this->notify->execute())->toBe(2) ->and($this->notify->execute())->toBe(0) ->and($this->notifications->recipients())->toBe(['10:1', '12:1']); $notification = array_values($this->notifications->notifications)[0]; expect($notification->params)->toBe([ 'task_id' => 1, 'task_title' => 'Tehtävä 1', 'job_id' => 5, 'job_number' => '2026-0042', 'job_name' => 'Messuvideo', 'starts_on' => '2026-11-27', ]); }); it('removes notifications older than a week', function () { $this->notifications->addIfNew(NewNotification::taskStartsSoon(10, 1, '2026-11-18', ['task_id' => 1], new DateTimeImmutable('2026-11-17 14:00'))); $this->notifications->addIfNew(NewNotification::taskStartsSoon(10, 2, '2026-11-20', ['task_id' => 2], new DateTimeImmutable('2026-11-19 14:00'))); $this->notify->execute(); expect($this->notifications->recipients())->toBe(['10:2']); });