diff --git a/src/Bundle/ChillDocStoreBundle/Service/Lock/CleanOldLockCronJob.php b/src/Bundle/ChillDocStoreBundle/Service/Lock/CleanOldLockCronJob.php new file mode 100644 index 000000000..3d9cee814 --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Service/Lock/CleanOldLockCronJob.php @@ -0,0 +1,44 @@ +clock->now() > $cronJobExecution->getLastStart()->add(new \DateInterval('PT12H')); + } + + public function getKey(): string + { + return self::KEY; + } + + public function run(array $lastExecutionData): ?array + { + ($this->cleanOldLock)(); + + return []; + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Service/Lock/CleanOldLockCronJobTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Service/Lock/CleanOldLockCronJobTest.php new file mode 100644 index 000000000..3830f31b0 --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Service/Lock/CleanOldLockCronJobTest.php @@ -0,0 +1,89 @@ + + */ + private ObjectProphecy $cleanOldLock; + + private CleanOldLockCronJob $cronJob; + + protected function setUp(): void + { + $this->clock = new MockClock('2024-01-02 12:00:00'); + $this->cleanOldLock = $this->prophesize(CleanOldLock::class); + $this->cronJob = new CleanOldLockCronJob( + $this->cleanOldLock->reveal(), + $this->clock + ); + } + + public function testGetKey(): void + { + self::assertSame('clean-old-stored-object-lock', $this->cronJob->getKey()); + } + + public function testRun(): void + { + $this->cleanOldLock->__invoke()->shouldBeCalled(); + + $result = $this->cronJob->run([]); + + self::assertSame([], $result); + } + + public function testCanRunWhenNullExecution(): void + { + self::assertTrue($this->cronJob->canRun(null)); + } + + public function testCanRunWhenLastStartIsWithin12Hours(): void + { + $execution = new CronJobExecution('key'); + // lastStart is "now" (2024-01-02 12:00:00) + // clock is "now" + // now > lastStart + 12h is FALSE + + self::assertFalse($this->cronJob->canRun($execution)); + } + + public function testCanRunWhenLastStartIsOlderThan12Hours(): void + { + $execution = new CronJobExecution('key'); + $execution->setLastStart(new \DateTimeImmutable('2024-01-01 12:00:00')); + // clock is 2024-01-02 12:00:00 + // lastStart + 12h is 2024-01-02 00:00:00 + // 2024-01-02 12:00:00 > 2024-01-02 00:00:00 is TRUE + + self::assertTrue($this->cronJob->canRun($execution)); + } +}