Refactor ChillDocumentLockManager to use database locks and add related tests

- Replaced Redis-based locking mechanism with database-based `StoredObjectLock` management using `EntityManagerInterface`.
- Integrated `MockClock` and `Security` components for lock timing and user association.
- Updated test cases to include database persistence and user assignment during lock operations.
- Implemented `onKernelTerminate` event listener to handle deferred database flush for lock updates.
This commit is contained in:
2026-03-31 17:32:53 +02:00
parent 277e4fa490
commit 76d3612d33
2 changed files with 118 additions and 30 deletions

View File

@@ -12,11 +12,15 @@ declare(strict_types=1);
namespace Chill\WopiBundle\Tests\Service\Wopi;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Redis\ChillRedis;
use Chill\MainBundle\Test\RandomUserTrait;
use Chill\WopiBundle\Service\Wopi\ChillDocumentLockManager;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\RequestInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Security\Core\Security;
/**
* @internal
@@ -27,14 +31,31 @@ final class ChillDocumentLockManagerTest extends KernelTestCase
{
use ProphecyTrait;
use RandomUserTrait;
private MockClock $clock;
/**
* @var ObjectProphecy<Security>
*/
private ObjectProphecy $security;
private EntityManagerInterface $em;
protected function setUp(): void
{
self::bootKernel();
$this->em = self::getContainer()->get('doctrine.orm.entity_manager');
$this->security = $this->prophesize(Security::class);
$this->clock = new MockClock();
}
public function testRelock()
{
$user = $this->getRandomUser($this->em);
$this->security->getUser()->willReturn($user);
$manager = $this->makeManager(1);
$document = new StoredObject();
$request = $this->prophesize(RequestInterface::class);
@@ -50,15 +71,22 @@ final class ChillDocumentLockManagerTest extends KernelTestCase
$this->assertTrue($manager->deleteLock($document, $request->reveal()));
sleep(3); // wait for redis to remove the key
$this->clock->sleep(10);
$this->assertFalse($manager->hasLock($document, $request->reveal()));
$this->em->remove($document);
$this->em->flush();
}
public function testSingleLock()
{
$user = $this->getRandomUser($this->em);
$this->security->getUser()->willReturn($user);
$manager = $this->makeManager(1);
$document = new StoredObject();
$this->em->persist($document);
$this->em->flush();
$request = $this->prophesize(RequestInterface::class);
$this->assertFalse($manager->hasLock($document, $request->reveal()));
@@ -69,15 +97,16 @@ final class ChillDocumentLockManagerTest extends KernelTestCase
$this->assertTrue($manager->deleteLock($document, $request->reveal()));
sleep(3); // wait for redis to remove the key
$this->clock->sleep(10);
$this->assertFalse($manager->hasLock($document, $request->reveal()));
$this->em->remove($document);
$this->em->flush();
}
private function makeManager(int $ttlAfterDeleteSeconds = -1): ChillDocumentLockManager
{
$redis = self::getContainer()->get(ChillRedis::class);
return new ChillDocumentLockManager($redis, $ttlAfterDeleteSeconds);
return new ChillDocumentLockManager($this->security->reveal(), $this->em, $this->clock, $ttlAfterDeleteSeconds);
}
}