diff --git a/README.md b/README.md index adb440a..4ef4186 100644 --- a/README.md +++ b/README.md @@ -34,18 +34,17 @@ composer require cybercog/php-db-locker ```php $dbConnection = new PDO($dsn, $username, $password); -$postgresLocker = new \Cog\DbLocker\Locker\PostgresAdvisoryLocker(); -$postgresLockId = \Cog\DbLocker\LockId\PostgresLockId::fromKeyValue('user', '4'); +$locker = new \Cog\DbLocker\Postgres\PostgresAdvisoryLocker(); +$lockId = \Cog\DbLocker\Postgres\PostgresLockKey::create('user', '4'); $dbConnection->beginTransaction(); -$isLockAcquired = $postgresLocker->acquireLock( +$lock = $locker->acquireSessionLevelLockHandler( $dbConnection, - $postgresLockId, - \Cog\DbLocker\Locker\PostgresAdvisoryLockScopeEnum::Transaction, - \Cog\DbLocker\Locker\PostgresAdvisoryLockTypeEnum::NonBlocking, - \Cog\DbLocker\Locker\PostgresLockModeEnum::Exclusive, + $lockId, + \Cog\DbLocker\Postgres\Enum\PostgresLockWaitModeEnum::NonBlocking, + \Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum::Exclusive, ); -if ($isLockAcquired) { +if ($lock->wasAcquired) { // Execute logic if lock was successful } else { // Execute logic if lock acquisition has been failed @@ -58,22 +57,24 @@ $dbConnection->commit(); ```php $dbConnection = new PDO($dsn, $username, $password); -$postgresLocker = new \Cog\DbLocker\Locker\PostgresAdvisoryLocker(); -$postgresLockId = \Cog\DbLocker\LockId\PostgresLockId::fromKeyValue('user', '4'); - -$isLockAcquired = $postgresLocker->acquireLock( - $dbConnection, - $postgresLockId, - \Cog\DbLocker\Locker\PostgresAdvisoryLockScopeEnum::Session, - \Cog\DbLocker\Locker\PostgresAdvisoryLockTypeEnum::NonBlocking, - \Cog\DbLocker\Locker\PostgresLockModeEnum::Exclusive, -); -if ($isLockAcquired) { - // Execute logic if lock was successful -} else { - // Execute logic if lock acquisition has been failed +$locker = new \Cog\DbLocker\Postgres\PostgresAdvisoryLocker(); +$lockId = \Cog\DbLocker\Postgres\PostgresLockKey::create('user', '4'); + +try { + $lock = $locker->acquireSessionLevelLockHandler( + $dbConnection, + $lockId, + \Cog\DbLocker\Postgres\Enum\PostgresLockWaitModeEnum::NonBlocking, + \Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum::Exclusive, + ); + if ($lock->wasAcquired) { + // Execute logic if lock was successful + } else { + // Execute logic if lock acquisition has been failed + } +} finally { + $lock->release(); } -$postgresLocker->releaseLock($dbConnection, $postgresLockId); ``` ## Changelog diff --git a/src/LockId/LockId.php b/src/LockId/LockId.php deleted file mode 100644 index 6f51925..0000000 --- a/src/LockId/LockId.php +++ /dev/null @@ -1,35 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Cog\DbLocker\LockId; - -use InvalidArgumentException; - -final class LockId -{ - public function __construct( - public readonly string $key, - public readonly string $value = '', - ) { - if ($key === '') { - throw new InvalidArgumentException('LockId key is empty'); - } - } - - public function __toString(): string - { - return $this->value !== '' - ? $this->key . ':' . $this->value - : $this->key; - } -} diff --git a/src/Locker/PostgresAdvisoryLocker.php b/src/Locker/PostgresAdvisoryLocker.php deleted file mode 100644 index 46e4fda..0000000 --- a/src/Locker/PostgresAdvisoryLocker.php +++ /dev/null @@ -1,141 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Cog\DbLocker\Locker; - -use Cog\DbLocker\LockId\PostgresLockId; -use LogicException; -use PDO; - -final class PostgresAdvisoryLocker -{ - /** - * Acquire an advisory lock with configurable scope, mode and behavior. - */ - public function acquireLock( - PDO $dbConnection, - PostgresLockId $postgresLockId, - PostgresAdvisoryLockScopeEnum $scope = PostgresAdvisoryLockScopeEnum::Transaction, - PostgresAdvisoryLockTypeEnum $type = PostgresAdvisoryLockTypeEnum::NonBlocking, - PostgresLockModeEnum $mode = PostgresLockModeEnum::Exclusive, - ): bool { - if ($scope === PostgresAdvisoryLockScopeEnum::Transaction && $dbConnection->inTransaction() === false) { - throw new LogicException( - "Transaction-level advisory lock `$postgresLockId->humanReadableValue` cannot be acquired outside of transaction", - ); - } - - $sql = match ([$scope, $type, $mode]) { - [ - PostgresAdvisoryLockScopeEnum::Transaction, - PostgresAdvisoryLockTypeEnum::NonBlocking, - PostgresLockModeEnum::Exclusive, - ] => 'SELECT PG_TRY_ADVISORY_XACT_LOCK(:class_id, :object_id);', - [ - PostgresAdvisoryLockScopeEnum::Transaction, - PostgresAdvisoryLockTypeEnum::Blocking, - PostgresLockModeEnum::Exclusive, - ] => 'SELECT PG_ADVISORY_XACT_LOCK(:class_id, :object_id);', - [ - PostgresAdvisoryLockScopeEnum::Transaction, - PostgresAdvisoryLockTypeEnum::NonBlocking, - PostgresLockModeEnum::Share, - ] => 'SELECT PG_TRY_ADVISORY_XACT_LOCK_SHARED(:class_id, :object_id);', - [ - PostgresAdvisoryLockScopeEnum::Transaction, - PostgresAdvisoryLockTypeEnum::Blocking, - PostgresLockModeEnum::Share, - ] => 'SELECT PG_ADVISORY_XACT_LOCK_SHARED(:class_id, :object_id);', - [ - PostgresAdvisoryLockScopeEnum::Session, - PostgresAdvisoryLockTypeEnum::NonBlocking, - PostgresLockModeEnum::Exclusive, - ] => 'SELECT PG_TRY_ADVISORY_LOCK(:class_id, :object_id);', - [ - PostgresAdvisoryLockScopeEnum::Session, - PostgresAdvisoryLockTypeEnum::Blocking, - PostgresLockModeEnum::Exclusive, - ] => 'SELECT PG_ADVISORY_LOCK(:class_id, :object_id);', - [ - PostgresAdvisoryLockScopeEnum::Session, - PostgresAdvisoryLockTypeEnum::NonBlocking, - PostgresLockModeEnum::Share, - ] => 'SELECT PG_TRY_ADVISORY_LOCK_SHARED(:class_id, :object_id);', - [ - PostgresAdvisoryLockScopeEnum::Session, - PostgresAdvisoryLockTypeEnum::Blocking, - PostgresLockModeEnum::Share, - ] => 'SELECT PG_ADVISORY_LOCK_SHARED(:class_id, :object_id);', - }; - $sql .= " -- $postgresLockId->humanReadableValue"; - - $statement = $dbConnection->prepare($sql); - $statement->execute( - [ - 'class_id' => $postgresLockId->classId, - 'object_id' => $postgresLockId->objectId, - ], - ); - - return $statement->fetchColumn(0); - } - - /** - * Release session level advisory lock. - */ - public function releaseLock( - PDO $dbConnection, - PostgresLockId $postgresLockId, - PostgresAdvisoryLockScopeEnum $scope = PostgresAdvisoryLockScopeEnum::Session, - PostgresLockModeEnum $mode = PostgresLockModeEnum::Exclusive, - ): bool { - if ($scope === PostgresAdvisoryLockScopeEnum::Transaction) { - throw new \InvalidArgumentException('Transaction-level advisory lock cannot be released'); - } - - $sql = match ($mode) { - PostgresLockModeEnum::Exclusive => 'SELECT PG_ADVISORY_UNLOCK(:class_id, :object_id);', - PostgresLockModeEnum::Share => 'SELECT PG_ADVISORY_UNLOCK_SHARED(:class_id, :object_id);', - }; - $sql .= " -- $postgresLockId->humanReadableValue"; - - $statement = $dbConnection->prepare($sql); - $statement->execute( - [ - 'class_id' => $postgresLockId->classId, - 'object_id' => $postgresLockId->objectId, - ], - ); - - return $statement->fetchColumn(0); - } - - /** - * Release all session level advisory locks held by the current session. - */ - public function releaseAllLocks( - PDO $dbConnection, - PostgresAdvisoryLockScopeEnum $scope = PostgresAdvisoryLockScopeEnum::Session, - ): void { - if ($scope === PostgresAdvisoryLockScopeEnum::Transaction) { - throw new \InvalidArgumentException('Transaction-level advisory lock cannot be released'); - } - - $statement = $dbConnection->prepare( - <<<'SQL' - SELECT PG_ADVISORY_UNLOCK_ALL(); - SQL, - ); - $statement->execute(); - } -} diff --git a/src/Locker/PostgresLockModeEnum.php b/src/Locker/PostgresLockModeEnum.php deleted file mode 100644 index 21900a3..0000000 --- a/src/Locker/PostgresLockModeEnum.php +++ /dev/null @@ -1,14 +0,0 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Cog\DbLocker\Postgres\LockHandle; + +use Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum; +use Cog\DbLocker\Postgres\PostgresAdvisoryLocker; +use Cog\DbLocker\Postgres\PostgresLockKey; +use PDO; + +/** + * @internal + */ +final class SessionLevelLockHandle +{ + private bool $isReleased = false; + + public function __construct( + private readonly PDO $dbConnection, + private readonly PostgresAdvisoryLocker $locker, + public readonly PostgresLockKey $lockId, + public readonly PostgresLockAccessModeEnum $accessMode, + public readonly bool $wasAcquired, + ) {} + + /** + * Explicitly release the lock if it was acquired and not yet released. + * + * @return bool True if the lock was released, false if it wasn't acquired or already released + */ + public function release(): bool + { + /** + * This code is mimicking the behavior of DB lock release. + */ + if (!$this->wasAcquired || $this->isReleased) { + return false; + } + + $wasReleased = $this->locker->releaseSessionLevelLock( + $this->dbConnection, + $this->lockId, + ); + + if ($wasReleased) { + $this->isReleased = true; + } + + return $wasReleased; + } + + /** + * Automatically release the lock when the handle is destroyed. + */ + public function __destruct() + { + // TODO: Do we need to + $this->release(); + } +} \ No newline at end of file diff --git a/src/Postgres/LockHandle/TransactionLevelLockHandle.php b/src/Postgres/LockHandle/TransactionLevelLockHandle.php new file mode 100644 index 0000000..999b12f --- /dev/null +++ b/src/Postgres/LockHandle/TransactionLevelLockHandle.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Cog\DbLocker\Postgres\LockHandle; + +/** + * @internal + */ +final class TransactionLevelLockHandle +{ + public function __construct( + public readonly bool $wasAcquired, + ) {} +} \ No newline at end of file diff --git a/src/Postgres/PostgresAdvisoryLocker.php b/src/Postgres/PostgresAdvisoryLocker.php new file mode 100644 index 0000000..9b55507 --- /dev/null +++ b/src/Postgres/PostgresAdvisoryLocker.php @@ -0,0 +1,219 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Cog\DbLocker\Postgres; + +use Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum; +use Cog\DbLocker\Postgres\Enum\PostgresLockLevelEnum; +use Cog\DbLocker\Postgres\Enum\PostgresLockWaitModeEnum; +use Cog\DbLocker\Postgres\LockHandle\SessionLevelLockHandle; +use Cog\DbLocker\Postgres\LockHandle\TransactionLevelLockHandle; +use LogicException; +use PDO; + +final class PostgresAdvisoryLocker +{ + /** + * Acquire a transaction-level advisory lock with configurable wait and access modes. + * + * TODO: Cover with tests + */ + public function acquireTransactionLevelLockHandler( + PDO $dbConnection, + PostgresLockKey $postgresLockId, + PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking, + PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive, + ): TransactionLevelLockHandle { + return new TransactionLevelLockHandle( + wasAcquired: $this->acquireLock( + $dbConnection, + $postgresLockId, + PostgresLockLevelEnum::Transaction, + $waitMode, + $accessMode, + ), + ); + } + + /** + * Acquire a transaction-level advisory lock with configurable wait and access modes. + */ + public function acquireTransactionLevelLock( + PDO $dbConnection, + PostgresLockKey $postgresLockId, + PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking, + PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive, + ): bool { + return $this->acquireLock( + $dbConnection, + $postgresLockId, + PostgresLockLevelEnum::Transaction, + $waitMode, + $accessMode, + ); + } + + /** + * Acquire a session-level advisory lock with configurable wait and access modes. + * + * TODO: Write that transaction-level is recommended. + * TODO: Cover with tests + */ + public function acquireSessionLevelLockHandler( + PDO $dbConnection, + PostgresLockKey $postgresLockId, + PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking, + PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive, + ): SessionLevelLockHandle { + return new SessionLevelLockHandle( + $dbConnection, + $this, + $postgresLockId, + $accessMode, + wasAcquired: $this->acquireLock( + $dbConnection, + $postgresLockId, + PostgresLockLevelEnum::Session, + $waitMode, + $accessMode, + ), + ); + } + + /** + * Acquire a session-level advisory lock with configurable wait and access modes. + * + * TODO: Write that transaction-level is recommended. + */ + public function acquireSessionLevelLock( + PDO $dbConnection, + PostgresLockKey $postgresLockId, + PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking, + PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive, + ): bool { + return $this->acquireLock( + $dbConnection, + $postgresLockId, + PostgresLockLevelEnum::Session, + $waitMode, + $accessMode, + ); + } + + /** + * Release session level advisory lock. + */ + public function releaseSessionLevelLock( + PDO $dbConnection, + PostgresLockKey $postgresLockId, + PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive, + ): bool { + $sql = match ($accessMode) { + PostgresLockAccessModeEnum::Exclusive => 'SELECT PG_ADVISORY_UNLOCK(:class_id, :object_id);', + PostgresLockAccessModeEnum::Share => 'SELECT PG_ADVISORY_UNLOCK_SHARED(:class_id, :object_id);', + }; + $sql .= " -- $postgresLockId->humanReadableValue"; + + $statement = $dbConnection->prepare($sql); + $statement->execute( + [ + 'class_id' => $postgresLockId->classId, + 'object_id' => $postgresLockId->objectId, + ], + ); + + return $statement->fetchColumn(0); + } + + /** + * Release all session level advisory locks held by the current session. + */ + public function releaseAllSessionLevelLocks( + PDO $dbConnection, + ): void { + $statement = $dbConnection->prepare( + <<<'SQL' + SELECT PG_ADVISORY_UNLOCK_ALL(); + SQL, + ); + $statement->execute(); + } + + private function acquireLock( + PDO $dbConnection, + PostgresLockKey $postgresLockId, + PostgresLockLevelEnum $level, + PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking, + PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive, + ): bool { + if ($level === PostgresLockLevelEnum::Transaction && $dbConnection->inTransaction() === false) { + throw new LogicException( + "Transaction-level advisory lock `$postgresLockId->humanReadableValue` cannot be acquired outside of transaction", + ); + } + + $sql = match ([$level, $waitMode, $accessMode]) { + [ + PostgresLockLevelEnum::Transaction, + PostgresLockWaitModeEnum::NonBlocking, + PostgresLockAccessModeEnum::Exclusive, + ] => 'SELECT PG_TRY_ADVISORY_XACT_LOCK(:class_id, :object_id);', + [ + PostgresLockLevelEnum::Transaction, + PostgresLockWaitModeEnum::Blocking, + PostgresLockAccessModeEnum::Exclusive, + ] => 'SELECT PG_ADVISORY_XACT_LOCK(:class_id, :object_id);', + [ + PostgresLockLevelEnum::Transaction, + PostgresLockWaitModeEnum::NonBlocking, + PostgresLockAccessModeEnum::Share, + ] => 'SELECT PG_TRY_ADVISORY_XACT_LOCK_SHARED(:class_id, :object_id);', + [ + PostgresLockLevelEnum::Transaction, + PostgresLockWaitModeEnum::Blocking, + PostgresLockAccessModeEnum::Share, + ] => 'SELECT PG_ADVISORY_XACT_LOCK_SHARED(:class_id, :object_id);', + [ + PostgresLockLevelEnum::Session, + PostgresLockWaitModeEnum::NonBlocking, + PostgresLockAccessModeEnum::Exclusive, + ] => 'SELECT PG_TRY_ADVISORY_LOCK(:class_id, :object_id);', + [ + PostgresLockLevelEnum::Session, + PostgresLockWaitModeEnum::Blocking, + PostgresLockAccessModeEnum::Exclusive, + ] => 'SELECT PG_ADVISORY_LOCK(:class_id, :object_id);', + [ + PostgresLockLevelEnum::Session, + PostgresLockWaitModeEnum::NonBlocking, + PostgresLockAccessModeEnum::Share, + ] => 'SELECT PG_TRY_ADVISORY_LOCK_SHARED(:class_id, :object_id);', + [ + PostgresLockLevelEnum::Session, + PostgresLockWaitModeEnum::Blocking, + PostgresLockAccessModeEnum::Share, + ] => 'SELECT PG_ADVISORY_LOCK_SHARED(:class_id, :object_id);', + }; + $sql .= " -- $postgresLockId->humanReadableValue"; + + $statement = $dbConnection->prepare($sql); + $statement->execute( + [ + 'class_id' => $postgresLockId->classId, + 'object_id' => $postgresLockId->objectId, + ], + ); + + return $statement->fetchColumn(0); + } +} diff --git a/src/LockId/PostgresLockId.php b/src/Postgres/PostgresLockKey.php similarity index 85% rename from src/LockId/PostgresLockId.php rename to src/Postgres/PostgresLockKey.php index 344e617..3537284 100644 --- a/src/LockId/PostgresLockId.php +++ b/src/Postgres/PostgresLockKey.php @@ -11,11 +11,11 @@ declare(strict_types=1); -namespace Cog\DbLocker\LockId; +namespace Cog\DbLocker\Postgres; use InvalidArgumentException; -final class PostgresLockId +final class PostgresLockKey { private const DB_INT32_VALUE_MIN = -2_147_483_648; private const DB_INT32_VALUE_MAX = 2_147_483_647; @@ -39,18 +39,20 @@ private function __construct( } } - public static function fromKeyValue( - string $key, + public static function create( + string $namespace, string $value = '', ): self { return new self( - classId: self::convertStringToSignedInt32($key), + classId: self::convertStringToSignedInt32($namespace), objectId: self::convertStringToSignedInt32($value), - humanReadableValue: "$key:$value", + // TODO: Do we need to sanitize it? + // TODO: Do we need to omit ":" on end if no value is passed + humanReadableValue: "$namespace:$value", ); } - public static function fromIntKeys( + public static function createFromInternalIds( int $classId, int $objectId, ): self { diff --git a/test/Integration/AbstractIntegrationTestCase.php b/test/Integration/AbstractIntegrationTestCase.php index bdafc8e..cbc1e73 100644 --- a/test/Integration/AbstractIntegrationTestCase.php +++ b/test/Integration/AbstractIntegrationTestCase.php @@ -13,8 +13,8 @@ namespace Cog\Test\DbLocker\Integration; -use Cog\DbLocker\Locker\PostgresLockModeEnum; -use Cog\DbLocker\LockId\PostgresLockId; +use Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum; +use Cog\DbLocker\Postgres\PostgresLockKey; use PDO; use PHPUnit\Framework\TestCase; @@ -44,8 +44,8 @@ protected function initPostgresPdoConnection(): PDO protected function assertPgAdvisoryLockExistsInConnection( PDO $dbConnection, - PostgresLockId $postgresLockId, - PostgresLockModeEnum $mode = PostgresLockModeEnum::Exclusive, + PostgresLockKey $postgresLockId, + PostgresLockAccessModeEnum $mode = PostgresLockAccessModeEnum::Exclusive, ): void { $row = $this->findPostgresAdvisoryLockInConnection( $dbConnection, @@ -63,8 +63,8 @@ protected function assertPgAdvisoryLockExistsInConnection( protected function assertPgAdvisoryLockMissingInConnection( PDO $dbConnection, - PostgresLockId $postgresLockId, - PostgresLockModeEnum $mode = PostgresLockModeEnum::Exclusive, + PostgresLockKey $postgresLockId, + PostgresLockAccessModeEnum $mode = PostgresLockAccessModeEnum::Exclusive, ): void { $row = $this->findPostgresAdvisoryLockInConnection( $dbConnection, @@ -95,8 +95,8 @@ protected function assertPgAdvisoryLocksCount( private function findPostgresAdvisoryLockInConnection( PDO $dbConnection, - PostgresLockId $postgresLockId, - PostgresLockModeEnum $mode, + PostgresLockKey $postgresLockId, + PostgresLockAccessModeEnum $mode, ): object | null { $statement = $dbConnection->prepare( <<<'SQL' diff --git a/test/Integration/Locker/PostgresAdvisoryLockerTest.php b/test/Integration/Postgres/PostgresAdvisoryLockerTest.php similarity index 60% rename from test/Integration/Locker/PostgresAdvisoryLockerTest.php rename to test/Integration/Postgres/PostgresAdvisoryLockerTest.php index 3a6400b..c7ec210 100644 --- a/test/Integration/Locker/PostgresAdvisoryLockerTest.php +++ b/test/Integration/Postgres/PostgresAdvisoryLockerTest.php @@ -11,12 +11,11 @@ declare(strict_types=1); -namespace Cog\Test\DbLocker\Integration\Locker; +namespace Cog\Test\DbLocker\Integration\Postgres; -use Cog\DbLocker\Locker\PostgresAdvisoryLocker; -use Cog\DbLocker\Locker\PostgresAdvisoryLockScopeEnum; -use Cog\DbLocker\Locker\PostgresLockModeEnum; -use Cog\DbLocker\LockId\PostgresLockId; +use Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum; +use Cog\DbLocker\Postgres\PostgresAdvisoryLocker; +use Cog\DbLocker\Postgres\PostgresLockKey; use Cog\Test\DbLocker\Integration\AbstractIntegrationTestCase; use LogicException; use PHPUnit\Framework\Attributes\DataProvider; @@ -28,65 +27,63 @@ final class PostgresAdvisoryLockerTest extends AbstractIntegrationTestCase #[DataProvider('provideItCanTryAcquireLockWithinSessionData')] public function testItCanTryAcquireLockWithinSession( - PostgresLockModeEnum $mode, + PostgresLockAccessModeEnum $accessMode, ): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); + $postgresLockId = PostgresLockKey::create('test'); - $isLockAcquired = $locker->acquireLock( + $isLockAcquired = $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId, - scope: PostgresAdvisoryLockScopeEnum::Session, - mode: $mode, + accessMode: $accessMode, ); $this->assertTrue($isLockAcquired); $this->assertPgAdvisoryLocksCount(1); - $this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId, $mode); + $this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId, $accessMode); } public static function provideItCanTryAcquireLockWithinSessionData(): array { return [ 'exclusive lock' => [ - PostgresLockModeEnum::Exclusive, + PostgresLockAccessModeEnum::Exclusive, ], 'share lock' => [ - PostgresLockModeEnum::Share, + PostgresLockAccessModeEnum::Share, ], ]; } #[DataProvider('provideItCanTryAcquireLockWithinTransactionData')] public function testItCanTryAcquireLockWithinTransaction( - PostgresLockModeEnum $mode, + PostgresLockAccessModeEnum $accessMode, ): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); + $postgresLockId = PostgresLockKey::create('test'); $dbConnection->beginTransaction(); - $isLockAcquired = $locker->acquireLock( + $isLockAcquired = $locker->acquireTransactionLevelLock( $dbConnection, $postgresLockId, - scope: PostgresAdvisoryLockScopeEnum::Transaction, - mode: $mode, + accessMode: $accessMode, ); $this->assertTrue($isLockAcquired); $this->assertPgAdvisoryLocksCount(1); - $this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId, $mode); + $this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId, $accessMode); } public static function provideItCanTryAcquireLockWithinTransactionData(): array { return [ 'exclusive lock' => [ - PostgresLockModeEnum::Exclusive, + PostgresLockAccessModeEnum::Exclusive, ], 'share lock' => [ - PostgresLockModeEnum::Share, + PostgresLockAccessModeEnum::Share, ], ]; } @@ -96,12 +93,11 @@ public function testItCanTryAcquireLockFromIntKeysCornerCases(): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromIntKeys(self::DB_INT32_VALUE_MIN, 0); + $postgresLockId = PostgresLockKey::createFromInternalIds(self::DB_INT32_VALUE_MIN, 0); - $isLockAcquired = $locker->acquireLock( + $isLockAcquired = $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId, - scope: PostgresAdvisoryLockScopeEnum::Session, ); $this->assertTrue($isLockAcquired); @@ -135,17 +131,15 @@ public function testItCanTryAcquireLockInSameConnectionOnlyOnce(): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); + $postgresLockId = PostgresLockKey::create('test'); - $isLockAcquired1 = $locker->acquireLock( + $isLockAcquired1 = $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); - $isLockAcquired2 = $locker->acquireLock( + $isLockAcquired2 = $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); $this->assertTrue($isLockAcquired1); @@ -158,18 +152,16 @@ public function testItCanTryAcquireMultipleLocksInOneConnection(): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId1 = PostgresLockId::fromKeyValue('test1'); - $postgresLockId2 = PostgresLockId::fromKeyValue('test2'); + $postgresLockId1 = PostgresLockKey::create('test1'); + $postgresLockId2 = PostgresLockKey::create('test2'); - $isLock1Acquired = $locker->acquireLock( + $isLock1Acquired = $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId1, - PostgresAdvisoryLockScopeEnum::Session, ); - $isLock2Acquired = $locker->acquireLock( + $isLock2Acquired = $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId2, - PostgresAdvisoryLockScopeEnum::Session, ); $this->assertTrue($isLock1Acquired); @@ -184,17 +176,15 @@ public function testItCannotAcquireSameLockInTwoConnections(): void $locker = $this->initLocker(); $dbConnection1 = $this->initPostgresPdoConnection(); $dbConnection2 = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); - $locker->acquireLock( + $postgresLockId = PostgresLockKey::create('test'); + $locker->acquireSessionLevelLock( $dbConnection1, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); - $isLockAcquired = $locker->acquireLock( + $isLockAcquired = $locker->acquireSessionLevelLock( $dbConnection2, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); $this->assertFalse($isLockAcquired); @@ -204,22 +194,21 @@ public function testItCannotAcquireSameLockInTwoConnections(): void #[DataProvider('provideItCanReleaseLockData')] public function testItCanReleaseLock( - PostgresLockModeEnum $mode, + PostgresLockAccessModeEnum $accessMode, ): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); - $locker->acquireLock( + $postgresLockId = PostgresLockKey::create('test'); + $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId, - scope: PostgresAdvisoryLockScopeEnum::Session, - mode: $mode, + accessMode: $accessMode, ); - $isLockReleased = $locker->releaseLock( + $isLockReleased = $locker->releaseSessionLevelLock( $dbConnection, $postgresLockId, - mode: $mode, + accessMode: $accessMode, ); $this->assertTrue($isLockReleased); @@ -230,33 +219,32 @@ public static function provideItCanReleaseLockData(): array { return [ 'exclusive lock' => [ - PostgresLockModeEnum::Exclusive, + PostgresLockAccessModeEnum::Exclusive, ], 'share lock' => [ - PostgresLockModeEnum::Share, + PostgresLockAccessModeEnum::Share, ], ]; } #[DataProvider('provideItCanNotReleaseLockOfDifferentModesData')] public function testItCanNotReleaseLockOfDifferentModes( - PostgresLockModeEnum $acquireMode, - PostgresLockModeEnum $releaseMode, + PostgresLockAccessModeEnum $acquireMode, + PostgresLockAccessModeEnum $releaseMode, ): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); - $locker->acquireLock( + $postgresLockId = PostgresLockKey::create('test'); + $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId, - scope: PostgresAdvisoryLockScopeEnum::Session, - mode: $acquireMode, + accessMode: $acquireMode, ); - $isLockReleased = $locker->releaseLock( + $isLockReleased = $locker->releaseSessionLevelLock( $dbConnection, $postgresLockId, - mode: $releaseMode, + accessMode: $releaseMode, ); $this->assertFalse($isLockReleased); @@ -268,12 +256,12 @@ public static function provideItCanNotReleaseLockOfDifferentModesData(): array { return [ 'release exclusive lock as share' => [ - 'acquireMode' => PostgresLockModeEnum::Exclusive, - 'releaseMode' => PostgresLockModeEnum::Share, + 'acquireAccessMode' => PostgresLockAccessModeEnum::Exclusive, + 'releaseAccessMode' => PostgresLockAccessModeEnum::Share, ], 'release share lock as exclusive' => [ - 'acquireMode' => PostgresLockModeEnum::Share, - 'releaseMode' => PostgresLockModeEnum::Exclusive, + 'acquireAccessMode' => PostgresLockAccessModeEnum::Share, + 'releaseAccessMode' => PostgresLockAccessModeEnum::Exclusive, ], ]; } @@ -282,20 +270,18 @@ public function testItCanReleaseLockTwiceIfAcquiredTwice(): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); - $locker->acquireLock( + $postgresLockId = PostgresLockKey::create('test'); + $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); - $locker->acquireLock( + $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); - $isLockReleased1 = $locker->releaseLock($dbConnection, $postgresLockId); - $isLockReleased2 = $locker->releaseLock($dbConnection, $postgresLockId); + $isLockReleased1 = $locker->releaseSessionLevelLock($dbConnection, $postgresLockId); + $isLockReleased2 = $locker->releaseSessionLevelLock($dbConnection, $postgresLockId); $this->assertTrue($isLockReleased1); $this->assertTrue($isLockReleased2); @@ -307,21 +293,19 @@ public function testItCanTryAcquireLockInSecondConnectionAfterRelease(): void $locker = $this->initLocker(); $dbConnection1 = $this->initPostgresPdoConnection(); $dbConnection2 = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); - $locker->acquireLock( + $postgresLockId = PostgresLockKey::create('test'); + $locker->acquireSessionLevelLock( $dbConnection1, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); - $locker->releaseLock( + $locker->releaseSessionLevelLock( $dbConnection1, $postgresLockId, ); - $isLockAcquired = $locker->acquireLock( + $isLockAcquired = $locker->acquireSessionLevelLock( $dbConnection2, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); $this->assertTrue($isLockAcquired); @@ -334,23 +318,20 @@ public function testItCannotAcquireLockInSecondConnectionAfterOneReleaseTwiceLoc $locker = $this->initLocker(); $dbConnection1 = $this->initPostgresPdoConnection(); $dbConnection2 = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); - $locker->acquireLock( + $postgresLockId = PostgresLockKey::create('test'); + $locker->acquireSessionLevelLock( $dbConnection1, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); - $locker->acquireLock( + $locker->acquireSessionLevelLock( $dbConnection1, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); - $isLockReleased = $locker->releaseLock($dbConnection1, $postgresLockId); - $isLockAcquired = $locker->acquireLock( + $isLockReleased = $locker->releaseSessionLevelLock($dbConnection1, $postgresLockId); + $isLockAcquired = $locker->acquireSessionLevelLock( $dbConnection2, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); $this->assertTrue($isLockReleased); @@ -364,9 +345,9 @@ public function testItCannotReleaseLockIfNotAcquired(): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); + $postgresLockId = PostgresLockKey::create('test'); - $isLockReleased = $locker->releaseLock($dbConnection, $postgresLockId); + $isLockReleased = $locker->releaseSessionLevelLock($dbConnection, $postgresLockId); $this->assertFalse($isLockReleased); $this->assertPgAdvisoryLocksCount(0); @@ -377,14 +358,13 @@ public function testItCannotReleaseLockIfAcquiredInOtherConnection(): void $locker = $this->initLocker(); $dbConnection1 = $this->initPostgresPdoConnection(); $dbConnection2 = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); - $locker->acquireLock( + $postgresLockId = PostgresLockKey::create('test'); + $locker->acquireSessionLevelLock( $dbConnection1, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); - $isLockReleased = $locker->releaseLock($dbConnection2, $postgresLockId); + $isLockReleased = $locker->releaseSessionLevelLock($dbConnection2, $postgresLockId); $this->assertFalse($isLockReleased); $this->assertPgAdvisoryLocksCount(1); @@ -395,23 +375,20 @@ public function testItCanReleaseAllLocksInConnection(): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $locker->acquireLock( + $locker->acquireSessionLevelLock( $dbConnection, - PostgresLockId::fromKeyValue('test'), - PostgresAdvisoryLockScopeEnum::Session, + PostgresLockKey::create('test'), ); - $locker->acquireLock( + $locker->acquireSessionLevelLock( $dbConnection, - PostgresLockId::fromKeyValue('test'), - PostgresAdvisoryLockScopeEnum::Session, + PostgresLockKey::create('test'), ); - $locker->acquireLock( + $locker->acquireSessionLevelLock( $dbConnection, - PostgresLockId::fromKeyValue('test2'), - PostgresAdvisoryLockScopeEnum::Session, + PostgresLockKey::create('test2'), ); - $locker->releaseAllLocks($dbConnection); + $locker->releaseAllSessionLevelLocks($dbConnection); $this->assertPgAdvisoryLocksCount(0); } @@ -421,7 +398,7 @@ public function testItCanReleaseAllLocksInConnectionIfNoLocksWereAcquired(): voi $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $locker->releaseAllLocks($dbConnection); + $locker->releaseAllSessionLevelLocks($dbConnection); $this->assertPgAdvisoryLocksCount(0); } @@ -431,32 +408,28 @@ public function testItCanReleaseAllLocksInConnectionButKeepsOtherConnectionLocks $locker = $this->initLocker(); $dbConnection1 = $this->initPostgresPdoConnection(); $dbConnection2 = $this->initPostgresPdoConnection(); - $postgresLockId1 = PostgresLockId::fromKeyValue('test'); - $postgresLockId2 = PostgresLockId::fromKeyValue('test2'); - $postgresLockId3 = PostgresLockId::fromKeyValue('test3'); - $postgresLockId4 = PostgresLockId::fromKeyValue('test4'); - $locker->acquireLock( + $postgresLockId1 = PostgresLockKey::create('test'); + $postgresLockId2 = PostgresLockKey::create('test2'); + $postgresLockId3 = PostgresLockKey::create('test3'); + $postgresLockId4 = PostgresLockKey::create('test4'); + $locker->acquireSessionLevelLock( $dbConnection1, $postgresLockId1, - PostgresAdvisoryLockScopeEnum::Session, ); - $locker->acquireLock( + $locker->acquireSessionLevelLock( $dbConnection1, $postgresLockId2, - PostgresAdvisoryLockScopeEnum::Session, ); - $locker->acquireLock( + $locker->acquireSessionLevelLock( $dbConnection2, $postgresLockId3, - PostgresAdvisoryLockScopeEnum::Session, ); - $locker->acquireLock( + $locker->acquireSessionLevelLock( $dbConnection2, $postgresLockId4, - PostgresAdvisoryLockScopeEnum::Session, ); - $locker->releaseAllLocks($dbConnection1); + $locker->releaseAllSessionLevelLocks($dbConnection1); $this->assertPgAdvisoryLocksCount(2); $this->assertPgAdvisoryLockExistsInConnection($dbConnection2, $postgresLockId3); @@ -472,12 +445,11 @@ public function testItCannotAcquireLockWithinTransactionNotInTransaction(): void $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); + $postgresLockId = PostgresLockKey::create('test'); - $locker->acquireLock( + $locker->acquireTransactionLevelLock( $dbConnection, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Transaction, ); } @@ -486,18 +458,16 @@ public function testItCannotAcquireLockInSecondConnectionIfTakenWithinTransactio $locker = $this->initLocker(); $dbConnection1 = $this->initPostgresPdoConnection(); $dbConnection2 = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); + $postgresLockId = PostgresLockKey::create('test'); $dbConnection1->beginTransaction(); - $locker->acquireLock( + $locker->acquireSessionLevelLock( $dbConnection1, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); - $isLockAcquired = $locker->acquireLock( + $isLockAcquired = $locker->acquireSessionLevelLock( $dbConnection2, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Session, ); $this->assertFalse($isLockAcquired); @@ -509,12 +479,11 @@ public function testItCanAutoReleaseLockAcquiredWithinTransactionOnCommit(): voi { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); + $postgresLockId = PostgresLockKey::create('test'); $dbConnection->beginTransaction(); - $locker->acquireLock( + $locker->acquireTransactionLevelLock( $dbConnection, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Transaction, ); $dbConnection->commit(); @@ -527,12 +496,11 @@ public function testItCanAutoReleaseLockAcquiredWithinTransactionOnRollback(): v { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); + $postgresLockId = PostgresLockKey::create('test'); $dbConnection->beginTransaction(); - $locker->acquireLock( + $locker->acquireTransactionLevelLock( $dbConnection, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Transaction, ); $dbConnection->rollBack(); @@ -545,12 +513,11 @@ public function testItCanAutoReleaseLockAcquiredWithinTransactionOnConnectionKil { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); + $postgresLockId = PostgresLockKey::create('test'); $dbConnection->beginTransaction(); - $locker->acquireLock( + $locker->acquireTransactionLevelLock( $dbConnection, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Transaction, ); $dbConnection = null; @@ -562,15 +529,14 @@ public function testItCannotReleaseLockAcquiredWithinTransaction(): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId = PostgresLockId::fromKeyValue('test'); + $postgresLockId = PostgresLockKey::create('test'); $dbConnection->beginTransaction(); - $locker->acquireLock( + $locker->acquireTransactionLevelLock( $dbConnection, $postgresLockId, - PostgresAdvisoryLockScopeEnum::Transaction, ); - $isLockReleased = $locker->releaseLock($dbConnection, $postgresLockId); + $isLockReleased = $locker->releaseSessionLevelLock($dbConnection, $postgresLockId); $this->assertFalse($isLockReleased); $this->assertPgAdvisoryLocksCount(1); @@ -581,90 +547,25 @@ public function testItCannotReleaseAllLocksAcquiredWithinTransaction(): void { $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId1 = PostgresLockId::fromKeyValue('test'); - $postgresLockId2 = PostgresLockId::fromKeyValue('test2'); - $locker->acquireLock( + $postgresLockId1 = PostgresLockKey::create('test'); + $postgresLockId2 = PostgresLockKey::create('test2'); + $locker->acquireSessionLevelLock( $dbConnection, $postgresLockId1, - PostgresAdvisoryLockScopeEnum::Session, ); $dbConnection->beginTransaction(); - $locker->acquireLock( + $locker->acquireTransactionLevelLock( $dbConnection, $postgresLockId2, - PostgresAdvisoryLockScopeEnum::Transaction, ); - $locker->releaseAllLocks($dbConnection); + $locker->releaseAllSessionLevelLocks($dbConnection); $this->assertPgAdvisoryLocksCount(1); $this->assertPgAdvisoryLockMissingInConnection($dbConnection, $postgresLockId1); $this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId2); } - public function testItCannotReleaseLockWithTransactionScope(): void - { - $locker = $this->initLocker(); - $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId1 = PostgresLockId::fromKeyValue('test'); - $postgresLockId2 = PostgresLockId::fromKeyValue('test2'); - $locker->acquireLock( - $dbConnection, - $postgresLockId1, - PostgresAdvisoryLockScopeEnum::Session, - ); - $dbConnection->beginTransaction(); - $locker->acquireLock( - $dbConnection, - $postgresLockId2, - PostgresAdvisoryLockScopeEnum::Transaction, - ); - - try { - $locker->releaseLock( - $dbConnection, - $postgresLockId2, - PostgresAdvisoryLockScopeEnum::Transaction, - ); - } catch (\InvalidArgumentException $exception) { - $this->assertSame( - 'Transaction-level advisory lock cannot be released', - $exception->getMessage(), - ); - } - } - - public function testItCannotReleaseAllLocksWithTransactionScope(): void - { - $locker = $this->initLocker(); - $dbConnection = $this->initPostgresPdoConnection(); - $postgresLockId1 = PostgresLockId::fromKeyValue('test'); - $postgresLockId2 = PostgresLockId::fromKeyValue('test2'); - $locker->acquireLock( - $dbConnection, - $postgresLockId1, - PostgresAdvisoryLockScopeEnum::Session, - ); - $dbConnection->beginTransaction(); - $locker->acquireLock( - $dbConnection, - $postgresLockId2, - PostgresAdvisoryLockScopeEnum::Transaction, - ); - - try { - $locker->releaseAllLocks( - $dbConnection, - PostgresAdvisoryLockScopeEnum::Transaction, - ); - } catch (\InvalidArgumentException $exception) { - $this->assertSame( - 'Transaction-level advisory lock cannot be released', - $exception->getMessage(), - ); - } - } - private function initLocker(): PostgresAdvisoryLocker { return new PostgresAdvisoryLocker(); diff --git a/test/Unit/LockId/PostgresLockIdTest.php b/test/Unit/Postgres/PostgresLockKeyTest.php similarity index 89% rename from test/Unit/LockId/PostgresLockIdTest.php rename to test/Unit/Postgres/PostgresLockKeyTest.php index d8ff576..4adb626 100644 --- a/test/Unit/LockId/PostgresLockIdTest.php +++ b/test/Unit/Postgres/PostgresLockKeyTest.php @@ -11,13 +11,13 @@ declare(strict_types=1); -namespace Cog\Test\DbLocker\Unit\LockId; +namespace Cog\Test\DbLocker\Unit\Postgres; -use Cog\DbLocker\LockId\PostgresLockId; +use Cog\DbLocker\Postgres\PostgresLockKey; use Cog\Test\DbLocker\Unit\AbstractUnitTestCase; use PHPUnit\Framework\Attributes\DataProvider; -final class PostgresLockIdTest extends AbstractUnitTestCase +final class PostgresLockKeyTest extends AbstractUnitTestCase { private const DB_INT32_VALUE_MIN = -2_147_483_648; private const DB_INT32_VALUE_MAX = 2_147_483_647; @@ -29,7 +29,7 @@ public function testItCanCreatePostgresLockIdFromKeyValue( int $expectedClassId, int $expectedObjectId, ): void { - $postgresLockId = PostgresLockId::fromKeyValue($key, $value); + $postgresLockId = PostgresLockKey::create($key, $value); $this->assertSame($expectedClassId, $postgresLockId->classId); $this->assertSame($expectedObjectId, $postgresLockId->objectId); @@ -58,7 +58,7 @@ public function testItCanCreatePostgresLockIdFromIntKeys( int $classId, int $objectId, ): void { - $lockId = PostgresLockId::fromIntKeys($classId, $objectId); + $lockId = PostgresLockKey::createFromInternalIds($classId, $objectId); $this->assertSame($classId, $lockId->classId); $this->assertSame($objectId, $lockId->objectId); @@ -95,7 +95,7 @@ public function testItCanNotCreatePostgresLockIdFromOutOfRangeIntKeys( $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage($expectedExceptionMessage); - $lockId = PostgresLockId::fromIntKeys($classId, $objectId); + $lockId = PostgresLockKey::createFromInternalIds($classId, $objectId); $this->assertSame($classId, $lockId->classId); $this->assertSame($objectId, $lockId->objectId); @@ -107,22 +107,22 @@ public static function provideItCanCreatePostgresLockIdFromOutOfRangeIntKeysData 'min class_id' => [ self::DB_INT32_VALUE_MIN - 1, 0, - "Out of bound exception (classId=-2147483649 is too small)" + "Out of bound exception (classId=-2147483649 is too small)", ], 'max class_id' => [ self::DB_INT32_VALUE_MAX + 1, 0, - "Out of bound exception (classId=2147483648 is too big)" + "Out of bound exception (classId=2147483648 is too big)", ], 'min object_id' => [ 0, self::DB_INT32_VALUE_MIN - 1, - "Out of bound exception (objectId=-2147483649 is too small)" + "Out of bound exception (objectId=-2147483649 is too small)", ], 'max object_id' => [ 0, self::DB_INT32_VALUE_MAX + 1, - "Out of bound exception (objectId=2147483648 is too big)" + "Out of bound exception (objectId=2147483648 is too big)", ], ]; }