mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-04-30 17:59:33 +00:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user