Skip to content

Commit 9e7b9d2

Browse files
committed
fix: make possible to test with dates
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 4e50c6c commit 9e7b9d2

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

lib/Service/ReminderService.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,14 @@ protected function scheduleJob(string $startTime): ?DateTime {
136136
protected function getStartTime(string $startTime): ?\DateTime {
137137
$timezone = $this->dateTimeZone->getTimeZone();
138138

139-
$dateTime = new \DateTime($startTime, $timezone);
140-
$dateTime->setTimezone(new \DateTimeZone('UTC'));
141-
142-
$now = new \DateTime('now', new \DateTimeZone('UTC'));
143-
if ($dateTime > $now) {
144-
return $dateTime;
145-
}
139+
$now = $this->time->getDateTime('now', new \DateTimeZone('UTC'));
140+
$dateTime = clone $now;
146141
$dateTime->modify('+1 day');
147142

143+
$time = new \DateTime($startTime, $timezone);
144+
$dateTime->setTime((int)$time->format('H'), (int)$time->format('i'));
145+
$dateTime->setTimezone(new \DateTimeZone('UTC'));
146+
148147
return $dateTime;
149148
}
150149

tests/php/Unit/Service/ReminderServiceTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ final class ReminderServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
2323
protected IJobList|MockObject $jobList;
2424
protected IAppConfig $appConfig;
2525
protected IDateTimeZone $dateTimeZone;
26-
protected ITimeFactory $time;
26+
protected ITimeFactory|MockObject $time;
2727
protected SignRequestMapper|MockObject $signRequestMapper;
2828
protected IdentifyMethodService|MockObject $identifyMethodService;
2929

3030
public function setUp(): void {
3131
$this->jobList = $this->createMock(IJobList::class);
3232
$this->appConfig = $this->getMockAppConfig();
3333
$this->dateTimeZone = Server::get(IDateTimeZone::class);
34-
$this->time = Server::get(ITimeFactory::class);
34+
$this->time = $this->createMock(ITimeFactory::class);
3535
$this->signRequestMapper = $this->createMock(SignRequestMapper::class);
3636
$this->identifyMethodService = $this->createMock(IdentifyMethodService::class);
3737
}
@@ -105,7 +105,7 @@ public function testWillNotify(array $summarized, \DateTime $now, int $daysBefor
105105
}
106106

107107
public static function providerWillNotify(): array {
108-
$now = (new DateTime())->setTime(12, 0);
108+
$now = new DateTime('2025-10-09 12:00:00', new \DateTimeZone('UTC'));
109109

110110
return [
111111
'no notifications, should not send with all zero and null' => [
@@ -327,6 +327,11 @@ public function testSave(
327327
string $sendTimer,
328328
array $expected,
329329
): void {
330+
// Setup fixed time for consistent testing
331+
$fixedTime = new DateTime('2025-10-09 09:00:00', new \DateTimeZone('UTC'));
332+
$this->time->method('getDateTime')
333+
->willReturn($fixedTime);
334+
330335
$service = $this->getService();
331336
$actual = $service->save($daysBefore, $daysBetween, $max, $sendTimer);
332337
$this->assertEquals($expected, $actual);
@@ -347,7 +352,7 @@ public function testSave(
347352
}
348353

349354
public static function providerSave(): array {
350-
$now = (new DateTime());
355+
$now = (new DateTime('2025-10-09 09:00:00', new \DateTimeZone('UTC')));
351356
return [
352357
[
353358
'daysBefore' => 0, 'daysBetween' => 0, 'max' => 0, 'sendTimer' => '',

0 commit comments

Comments
 (0)