Fix RefreshAddressToGeographicalUnitMaterializedViewCronJobTest: use ClockInterface to handle properly now and use more random tests

This commit is contained in:
2023-07-27 23:52:38 +02:00
parent 5f6e506300
commit dd21694544
2 changed files with 30 additions and 43 deletions

View File

@@ -16,12 +16,15 @@ use Chill\MainBundle\Entity\CronJobExecution;
use DateInterval;
use DateTimeImmutable;
use Doctrine\DBAL\Connection;
use Symfony\Component\Clock\ClockInterface;
use UnexpectedValueException;
class RefreshAddressToGeographicalUnitMaterializedViewCronJob implements CronJobInterface
final readonly class RefreshAddressToGeographicalUnitMaterializedViewCronJob implements CronJobInterface
{
public function __construct(private readonly Connection $connection)
{
public function __construct(
private Connection $connection,
private ClockInterface $clock,
) {
}
public function canRun(?CronJobExecution $cronJobExecution): bool
@@ -34,7 +37,7 @@ class RefreshAddressToGeographicalUnitMaterializedViewCronJob implements CronJob
throw new UnexpectedValueException();
}
$now = new DateTimeImmutable('now');
$now = $this->clock->now();
return $cronJobExecution->getLastStart() < $now->sub(new DateInterval('P1D'))
// introduce a random component to ensure a roll when multiple instances are hosted on same machines