mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-04-09 06:23:45 +00:00
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.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\DocStoreBundle\Service\Lock;
|
||||
|
||||
use Chill\MainBundle\Cron\CronJobInterface;
|
||||
use Chill\MainBundle\Entity\CronJobExecution;
|
||||
use Symfony\Component\Clock\ClockInterface;
|
||||
|
||||
final readonly class CleanOldLockCronJob implements CronJobInterface
|
||||
{
|
||||
private const KEY = 'clean-old-stored-object-lock';
|
||||
|
||||
public function __construct(private CleanOldLock $cleanOldLock, private ClockInterface $clock) {}
|
||||
|
||||
public function canRun(?CronJobExecution $cronJobExecution): bool
|
||||
{
|
||||
if (null === $cronJobExecution) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->clock->now() > $cronJobExecution->getLastStart()->add(new \DateInterval('PT12H'));
|
||||
}
|
||||
|
||||
public function getKey(): string
|
||||
{
|
||||
return self::KEY;
|
||||
}
|
||||
|
||||
public function run(array $lastExecutionData): ?array
|
||||
{
|
||||
($this->cleanOldLock)();
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\DocStoreBundle\Tests\Service\Lock;
|
||||
|
||||
use Chill\DocStoreBundle\Service\Lock\CleanOldLock;
|
||||
use Chill\DocStoreBundle\Service\Lock\CleanOldLockCronJob;
|
||||
use Chill\MainBundle\Entity\CronJobExecution;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Symfony\Component\Clock\MockClock;
|
||||
|
||||
/**
|
||||
* @covers \Chill\DocStoreBundle\Service\Lock\CleanOldLockCronJob
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class CleanOldLockCronJobTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private MockClock $clock;
|
||||
|
||||
/**
|
||||
* @var ObjectProphecy<CleanOldLock>
|
||||
*/
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user