Skip to content

Commit

Permalink
Migrate to PHP 8.1 and PHPUnit 10
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Komarev committed Aug 26, 2023
1 parent 9d0ab85 commit db64453
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 204 deletions.
2 changes: 1 addition & 1 deletion .docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ----------------------
# The FPM base container
# ----------------------
FROM php:7.4-fpm-alpine AS dev
FROM php:8.1-fpm-alpine AS dev

# Install build dependencies
RUN apk add --no-cache --virtual .build-deps \
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/.docker-volume-postgres
/.idea
/.phpunit.cache
/vendor
.DS_STORE
.phpunit.result.cache
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
"docs": "https://github.com/cybercog/php-db-locker"
},
"require": {
"php": "^7.4|^8.0",
"php": "^8.1",
"ext-pdo": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^10.0"
},
"autoload": {
"psr-4": {
Expand Down
23 changes: 13 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
stopOnFailure="false"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
stopOnFailure="false"
cacheDirectory=".phpunit.cache"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<coverage/>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./test</directory>
Expand All @@ -23,4 +21,9 @@
<env name="DB_POSTGRES_PASSWORD" value="secret"/>
<env name="DB_POSTGRES_DATABASE" value="php_db_locker"/>
</php>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
11 changes: 2 additions & 9 deletions src/LockId/LockId.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@

final class LockId
{
private string $key;

private string $value;

public function __construct(
string $key,
string $value = ''
public readonly string $key,
public readonly string $value = '',
) {
$this->key = $key;
$this->value = $value;

if ($key === '') {
throw new InvalidArgumentException('LockId key is empty');
}
Expand Down
38 changes: 14 additions & 24 deletions src/LockId/PostgresLockId.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,36 @@

final class PostgresLockId
{
private const MIN_DB_INT_VALUE = 0;
private const MAX_DB_INT_VALUE = 9223372036854775807;

private int $id;

private string $humanReadableValue;
private const DB_INT_VALUE_MIN = 0;
private const DB_INT_VALUE_MAX = 9223372036854775807;
private const DB_INT32_VALUE_MAX = 2147483647;

public function __construct(
int $id,
string $humanReadableValue = ''
public readonly int $id,
public readonly string $humanReadableValue = '',
) {
$this->id = $id;
$this->humanReadableValue = $humanReadableValue;

if ($id < self::MIN_DB_INT_VALUE) {
if ($id < self::DB_INT_VALUE_MIN) {
throw new InvalidArgumentException('Out of bound exception (id is too small)');
}
if ($id > self::MAX_DB_INT_VALUE) {
if ($id > self::DB_INT_VALUE_MAX) {
throw new InvalidArgumentException('Out of bound exception (id is too big)');
}
}

public function id(): int
{
return $this->id;
}

public function humanReadableValue(): string
{
return $this->humanReadableValue;
}

public static function fromLockId(
LockId $lockId
): self {
$humanReadableValue = (string)$lockId;

return new self(
crc32($humanReadableValue),
self::generateIdFromString($humanReadableValue),
$humanReadableValue
);
}

private static function generateIdFromString(
string $string,
): int {
return crc32($string);
}
}
16 changes: 8 additions & 8 deletions src/Locker/PostgresAdvisoryLocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class PostgresAdvisoryLocker
{
public function acquireLock(
PDO $dbConnection,
PostgresLockId $postgresLockId
PostgresLockId $postgresLockId,
): bool {
$statement = $dbConnection->prepare(
<<<SQL
Expand All @@ -30,7 +30,7 @@ public function acquireLock(
);
$statement->execute(
[
'lock_id' => $postgresLockId->id(),
'lock_id' => $postgresLockId->id,
]
);

Expand All @@ -39,10 +39,10 @@ public function acquireLock(

public function acquireLockWithinTransaction(
PDO $dbConnection,
PostgresLockId $postgresLockId
PostgresLockId $postgresLockId,
): bool {
if ($dbConnection->inTransaction() === false) {
$lockId = $postgresLockId->humanReadableValue();
$lockId = $postgresLockId->humanReadableValue;

throw new LogicException(
"Transaction-level advisory lock `$lockId` cannot be acquired outside of transaction"
Expand All @@ -56,7 +56,7 @@ public function acquireLockWithinTransaction(
);
$statement->execute(
[
'lock_id' => $postgresLockId->id(),
'lock_id' => $postgresLockId->id,
]
);

Expand All @@ -65,7 +65,7 @@ public function acquireLockWithinTransaction(

public function releaseLock(
PDO $dbConnection,
PostgresLockId $postgresLockId
PostgresLockId $postgresLockId,
): bool {
$statement = $dbConnection->prepare(
<<<SQL
Expand All @@ -74,15 +74,15 @@ public function releaseLock(
);
$statement->execute(
[
'lock_id' => $postgresLockId->id(),
'lock_id' => $postgresLockId->id,
]
);

return $statement->fetchColumn(0);
}

public function releaseAllLocks(
PDO $dbConnection
PDO $dbConnection,
): void {
$statement = $dbConnection->prepare(
<<<SQL
Expand Down
12 changes: 6 additions & 6 deletions test/Integration/AbstractIntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function tearDown(): void
parent::tearDown();
}

protected function createPostgresPdoConnection(): PDO
protected function initPostgresPdoConnection(): PDO
{
$dsn = implode(';', [
'dbname=' . getenv('DB_POSTGRES_DATABASE'),
Expand All @@ -52,7 +52,7 @@ protected function assertPgAdvisoryLockExistsInConnection(
): void {
$row = $this->findPostgresAdvisoryLockInConnection($dbConnection, $postgresLockId);

$lockIdString = $postgresLockId->humanReadableValue();
$lockIdString = $postgresLockId->humanReadableValue;

$this->assertTrue(
$row !== null,
Expand All @@ -66,7 +66,7 @@ protected function assertPgAdvisoryLockMissingInConnection(
): void {
$row = $this->findPostgresAdvisoryLockInConnection($dbConnection, $postgresLockId);

$lockIdString = $postgresLockId->humanReadableValue();
$lockIdString = $postgresLockId->humanReadableValue;

$this->assertTrue(
$row === null,
Expand All @@ -91,7 +91,7 @@ private function findPostgresAdvisoryLockInConnection(
PDO $dbConnection,
PostgresLockId $postgresLockId
): ?object {
$id = $postgresLockId->id();
$id = $postgresLockId->id;

$lockObjectId = $id % self::POSTGRES_BLOCK_SIZE;
$lockCatalogId = ($id - $lockObjectId) / self::POSTGRES_BLOCK_SIZE;
Expand Down Expand Up @@ -127,7 +127,7 @@ private function findPostgresAdvisoryLockInConnection(

private function findAllPostgresAdvisoryLocks(): array
{
$dbConnection = $this->createPostgresPdoConnection();
$dbConnection = $this->initPostgresPdoConnection();

$statement = $dbConnection->prepare(
<<<SQL
Expand All @@ -148,7 +148,7 @@ private function findAllPostgresAdvisoryLocks(): array

private function closeAllPostgresPdoConnections(): void
{
$this->createPostgresPdoConnection()->query(
$this->initPostgresPdoConnection()->query(
<<<SQL
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
Expand Down
Loading

0 comments on commit db64453

Please sign in to comment.