mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-04-04 20:13:42 +00:00
Add method to check lock activity and corresponding tests
- Added `isActiveAt` method in `StoredObjectLock` to check if a lock is active at a specific time. - Implemented `isLockedAt` method in `StoredObject` to determine if an object is locked at a given time. - Included unit tests for `isActiveAt` method in `StoredObjectLockTest` to validate various scenarios.
This commit is contained in:
@@ -131,6 +131,25 @@ class StoredObject implements Document, TrackCreationInterface
|
||||
$this->locks->removeElement($lock);
|
||||
}
|
||||
|
||||
public function isLockedAt(\DateTimeImmutable $at): bool
|
||||
{
|
||||
foreach ($this->locks as $lock) {
|
||||
if ($lock->isActiveAt($at)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, StoredObjectLock>
|
||||
*/
|
||||
public function getLocks(): Collection
|
||||
{
|
||||
return $this->locks;
|
||||
}
|
||||
|
||||
public function addGenerationTrial(): self
|
||||
{
|
||||
++$this->generationTrialsCounter;
|
||||
|
||||
@@ -118,4 +118,15 @@ class StoredObjectLock
|
||||
{
|
||||
return $this->expireAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the lock must be considered as active.
|
||||
*
|
||||
* A StoredObjectLock is active if there isn't any expiration date, or
|
||||
* if the expiration date and time is before the given time.
|
||||
*/
|
||||
public function isActiveAt(\DateTimeImmutable $at): bool
|
||||
{
|
||||
return null === $this->getExpireAt() || $at < $this->getExpireAt();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
<?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\Entity;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Entity\StoredObjectLock;
|
||||
use Chill\DocStoreBundle\Entity\StoredObjectLockMethodEnum;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @covers \Chill\DocStoreBundle\Entity\StoredObjectLock
|
||||
*/
|
||||
class StoredObjectLockTest extends TestCase
|
||||
{
|
||||
public function testIsActiveAtWithNoExpiration(): void
|
||||
{
|
||||
$storedObject = new StoredObject();
|
||||
$now = new \DateTimeImmutable();
|
||||
$lock = new StoredObjectLock(
|
||||
$storedObject,
|
||||
StoredObjectLockMethodEnum::WOPI,
|
||||
$now,
|
||||
'token',
|
||||
null
|
||||
);
|
||||
|
||||
self::assertTrue($lock->isActiveAt($now));
|
||||
self::assertTrue($lock->isActiveAt($now->modify('+1 year')));
|
||||
}
|
||||
|
||||
public function testIsActiveAtWithFutureExpiration(): void
|
||||
{
|
||||
$storedObject = new StoredObject();
|
||||
$now = new \DateTimeImmutable();
|
||||
$expireAt = $now->modify('+1 hour');
|
||||
$lock = new StoredObjectLock(
|
||||
$storedObject,
|
||||
StoredObjectLockMethodEnum::WOPI,
|
||||
$now,
|
||||
'token',
|
||||
$expireAt
|
||||
);
|
||||
|
||||
self::assertTrue($lock->isActiveAt($now));
|
||||
self::assertTrue($lock->isActiveAt($now->modify('+30 minutes')));
|
||||
}
|
||||
|
||||
public function testIsActiveAtWithPastExpiration(): void
|
||||
{
|
||||
$storedObject = new StoredObject();
|
||||
$now = new \DateTimeImmutable();
|
||||
$expireAt = $now->modify('-1 hour');
|
||||
$lock = new StoredObjectLock(
|
||||
$storedObject,
|
||||
StoredObjectLockMethodEnum::WOPI,
|
||||
$now->modify('-2 hours'),
|
||||
'token',
|
||||
$expireAt
|
||||
);
|
||||
|
||||
self::assertFalse($lock->isActiveAt($now));
|
||||
self::assertFalse($lock->isActiveAt($expireAt));
|
||||
self::assertFalse($lock->isActiveAt($expireAt->modify('+1 second')));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user