fixed: take into account the first iteration of the cronjob

This commit is contained in:
Julien Fastré 2023-07-13 09:03:41 +02:00
parent a7842b2597
commit b0fcffea2d
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 10 additions and 3 deletions

View File

@ -27,6 +27,10 @@ final readonly class CollateAddressWithReferenceOrPostalCodeCronJob implements C
public function canRun(?CronJobExecution $cronJobExecution): bool
{
if (null === $cronJobExecution) {
return true;
}
$now = $this->clock->now();
return $now->sub(new \DateInterval('PT6H')) > $cronJobExecution->getLastStart();

View File

@ -29,10 +29,12 @@ class CollateAddressWithReferenceOrPostalCodeCronJobTest extends TestCase
/**
* @dataProvider provideDataCanRun
*/
public function testCanRun(\DateTimeImmutable $now, \DateTimeImmutable $lastExecution, bool $expected): void
public function testCanRun(\DateTimeImmutable $now, ?\DateTimeImmutable $lastExecution, bool $expected): void
{
$execution = new CronJobExecution('collate-address');
$execution ->setLastStart($lastExecution);
$execution = match ($lastExecution) {
null => null,
default => (new CronJobExecution('collate-address'))->setLastStart($lastExecution),
};
$clock = new MockClock($now);
$collator = $this->prophesize(CollateAddressWithReferenceOrPostalCodeInterface::class);
@ -60,6 +62,7 @@ class CollateAddressWithReferenceOrPostalCodeCronJobTest extends TestCase
yield [new \DateTimeImmutable('2023-07-10T12:00:00'), new \DateTimeImmutable('2023-07-10T11:00:00'), false];
yield [new \DateTimeImmutable('2023-07-10T12:00:00'), new \DateTimeImmutable('2023-07-10T05:00:00'), true];
yield [new \DateTimeImmutable('2023-07-10T12:00:00'), new \DateTimeImmutable('2023-07-01T12:00:00'), true];
yield [new \DateTimeImmutable('2023-07-10T12:00:00'), null, true];
}
}