From c60383b6365c29cd4fb2865836c331fd8887e4fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 8 Apr 2026 22:26:43 +0200 Subject: [PATCH] Add `CleanOldLockCronJob` service and corresponding test suite - Introduced `CleanOldLockCronJob` to handle scheduled cleaning of old locks. - Implemented tests in `CleanOldLockCronJobTest` to verify behavior, including conditions for execution based on elapsed time. - Utilized `MockClock` for precise time-based testing scenarios. --- .../Service/Lock/CleanOldLockCronJob.php | 44 +++++++++ .../Service/Lock/CleanOldLockCronJobTest.php | 89 +++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/Bundle/ChillDocStoreBundle/Service/Lock/CleanOldLockCronJob.php create mode 100644 src/Bundle/ChillDocStoreBundle/Tests/Service/Lock/CleanOldLockCronJobTest.php 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)); + } +}