adachsoft / lease-lock-contract-tests
Reusable PHPUnit contract tests and a frozen clock for lease lock implementations.
Package info
gitlab.com/a.adach/lease-lock-contract-tests
pkg:composer/adachsoft/lease-lock-contract-tests
Requires
- php: ^8.3
- adachsoft/lease-lock-contracts: ^0.1.0
- psr/clock: ^1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7.3
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^12.5
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/lease-lock-contract-tests provides a reusable PHPUnit behavioral contract and a deterministic PSR-20 FrozenClock for implementations of AdachSoft\LeaseLockContracts\LeaseLockInterface.
Features
- A reusable contract suite for lease acquisition, ownership, expiry, renewal, release, independent keys, and argument validation.
- A UTC-normalized frozen clock for deterministic time-dependent tests.
- Coverage of keys containing separators such as
:and/.
Requirements
- PHP
^8.3 - PHPUnit
^12.5 adachsoft/lease-lock-contracts^0.1.0psr/clock^1.0
Installation
Install the package as a development dependency in the project containing your lock implementation:
composer require --dev adachsoft/lease-lock-contract-tests
Usage
Extend AbstractLeaseLockContractTest and implement createLock() for your adapter. The factory must return a new lock instance on every call. Instances created in the same test must share backing storage, while storage must be isolated between test methods.
use AdachSoft\LeaseLockContracts\LeaseLockInterface;
use AdachSoft\LeaseLockContractTests\Contract\AbstractLeaseLockContractTest;
use Psr\Clock\ClockInterface;
final class FileLeaseLockContractTest extends AbstractLeaseLockContractTest
{
protected function createLock(ClockInterface $clock): LeaseLockInterface
{
return new FileLeaseLock($this->testDirectory, $clock);
}
}
For file-backed locks, create a unique temporary directory per test and clean it up afterward. For in-memory locks, share a storage object owned by the test instance. See the contract example for the setup requirements.
Frozen clock
AdachSoft\LeaseLockContractTests\Support\FrozenClock implements PSR-20 ClockInterface and normalizes time to UTC. Use advance(int $seconds) to move forward by a non-negative number of seconds or set(DateTimeImmutable $now) to set a specific instant. Pass the same clock instance to every lock instance created during one contract test.