Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db64453

Browse files
author
Anton Komarev
committed
Migrate to PHP 8.1 and PHPUnit 10
1 parent 9d0ab85 commit db64453

File tree

11 files changed

+161
-204
lines changed

11 files changed

+161
-204
lines changed

‎.docker/php/Dockerfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ----------------------
22
# The FPM base container
33
# ----------------------
4-
FROM php:7.4-fpm-alpine AS dev
4+
FROM php:8.1-fpm-alpine AS dev
55

66
# Install build dependencies
77
RUN apk add --no-cache --virtual .build-deps \

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.docker-volume-postgres
22
/.idea
3+
/.phpunit.cache
34
/vendor
45
.DS_STORE
56
.phpunit.result.cache

‎composer.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
"docs": "https://github.com/cybercog/php-db-locker"
3131
},
3232
"require": {
33-
"php": "^7.4|^8.0",
33+
"php": "^8.1",
3434
"ext-pdo": "*"
3535
},
3636
"require-dev": {
37-
"phpunit/phpunit": "^9.5"
37+
"phpunit/phpunit": "^10.0"
3838
},
3939
"autoload": {
4040
"psr-4": {

‎phpunit.xml.dist‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
stopOnFailure="false"
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
stopOnFailure="false"
8+
cacheDirectory=".phpunit.cache"
79
>
8-
<coverage processUncoveredFiles="true">
9-
<include>
10-
<directory suffix=".php">./src</directory>
11-
</include>
12-
</coverage>
10+
<coverage/>
1311
<testsuites>
1412
<testsuite name="Unit">
1513
<directory suffix="Test.php">./test</directory>
@@ -23,4 +21,9 @@
2321
<env name="DB_POSTGRES_PASSWORD" value="secret"/>
2422
<env name="DB_POSTGRES_DATABASE" value="php_db_locker"/>
2523
</php>
24+
<source>
25+
<include>
26+
<directory suffix=".php">./src</directory>
27+
</include>
28+
</source>
2629
</phpunit>

‎src/LockId/LockId.php‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,10 @@
1717

1818
final class LockId
1919
{
20-
private string $key;
21-
22-
private string $value;
23-
2420
public function __construct(
25-
string $key,
26-
string $value = ''
21+
publicreadonlystring $key,
22+
publicreadonlystring $value = '',
2723
) {
28-
$this->key = $key;
29-
$this->value = $value;
30-
3124
if ($key === '') {
3225
throw new InvalidArgumentException('LockId key is empty');
3326
}

‎src/LockId/PostgresLockId.php‎

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,36 @@
1717

1818
final class PostgresLockId
1919
{
20-
private const MIN_DB_INT_VALUE = 0;
21-
private const MAX_DB_INT_VALUE = 9223372036854775807;
22-
23-
private int $id;
24-
25-
private string $humanReadableValue;
20+
private const DB_INT_VALUE_MIN = 0;
21+
private const DB_INT_VALUE_MAX = 9223372036854775807;
22+
private const DB_INT32_VALUE_MAX = 2147483647;
2623

2724
public function __construct(
28-
int $id,
29-
string $humanReadableValue = ''
25+
publicreadonlyint $id,
26+
publicreadonlystring $humanReadableValue = '',
3027
) {
31-
$this->id = $id;
32-
$this->humanReadableValue = $humanReadableValue;
33-
34-
if ($id < self::MIN_DB_INT_VALUE) {
28+
if ($id < self::DB_INT_VALUE_MIN) {
3529
throw new InvalidArgumentException('Out of bound exception (id is too small)');
3630
}
37-
if ($id > self::MAX_DB_INT_VALUE) {
31+
if ($id > self::DB_INT_VALUE_MAX) {
3832
throw new InvalidArgumentException('Out of bound exception (id is too big)');
3933
}
4034
}
4135

42-
public function id(): int
43-
{
44-
return $this->id;
45-
}
46-
47-
public function humanReadableValue(): string
48-
{
49-
return $this->humanReadableValue;
50-
}
51-
5236
public static function fromLockId(
5337
LockId $lockId
5438
): self {
5539
$humanReadableValue = (string)$lockId;
5640

5741
return new self(
58-
crc32($humanReadableValue),
42+
self::generateIdFromString($humanReadableValue),
5943
$humanReadableValue
6044
);
6145
}
46+
47+
private static function generateIdFromString(
48+
string $string,
49+
): int {
50+
return crc32($string);
51+
}
6252
}

‎src/Locker/PostgresAdvisoryLocker.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class PostgresAdvisoryLocker
2121
{
2222
public function acquireLock(
2323
PDO $dbConnection,
24-
PostgresLockId $postgresLockId
24+
PostgresLockId $postgresLockId,
2525
): bool {
2626
$statement = $dbConnection->prepare(
2727
<<<SQL
@@ -30,7 +30,7 @@ public function acquireLock(
3030
);
3131
$statement->execute(
3232
[
33-
'lock_id' => $postgresLockId->id(),
33+
'lock_id' => $postgresLockId->id,
3434
]
3535
);
3636

@@ -39,10 +39,10 @@ public function acquireLock(
3939

4040
public function acquireLockWithinTransaction(
4141
PDO $dbConnection,
42-
PostgresLockId $postgresLockId
42+
PostgresLockId $postgresLockId,
4343
): bool {
4444
if ($dbConnection->inTransaction() === false) {
45-
$lockId = $postgresLockId->humanReadableValue();
45+
$lockId = $postgresLockId->humanReadableValue;
4646

4747
throw new LogicException(
4848
"Transaction-level advisory lock `$lockId` cannot be acquired outside of transaction"
@@ -56,7 +56,7 @@ public function acquireLockWithinTransaction(
5656
);
5757
$statement->execute(
5858
[
59-
'lock_id' => $postgresLockId->id(),
59+
'lock_id' => $postgresLockId->id,
6060
]
6161
);
6262

@@ -65,7 +65,7 @@ public function acquireLockWithinTransaction(
6565

6666
public function releaseLock(
6767
PDO $dbConnection,
68-
PostgresLockId $postgresLockId
68+
PostgresLockId $postgresLockId,
6969
): bool {
7070
$statement = $dbConnection->prepare(
7171
<<<SQL
@@ -74,15 +74,15 @@ public function releaseLock(
7474
);
7575
$statement->execute(
7676
[
77-
'lock_id' => $postgresLockId->id(),
77+
'lock_id' => $postgresLockId->id,
7878
]
7979
);
8080

8181
return $statement->fetchColumn(0);
8282
}
8383

8484
public function releaseAllLocks(
85-
PDO $dbConnection
85+
PDO $dbConnection,
8686
): void {
8787
$statement = $dbConnection->prepare(
8888
<<<SQL

‎test/Integration/AbstractIntegrationTestCase.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function tearDown(): void
3131
parent::tearDown();
3232
}
3333

34-
protected function createPostgresPdoConnection(): PDO
34+
protected function initPostgresPdoConnection(): PDO
3535
{
3636
$dsn = implode(';', [
3737
'dbname=' . getenv('DB_POSTGRES_DATABASE'),
@@ -52,7 +52,7 @@ protected function assertPgAdvisoryLockExistsInConnection(
5252
): void {
5353
$row = $this->findPostgresAdvisoryLockInConnection($dbConnection, $postgresLockId);
5454

55-
$lockIdString = $postgresLockId->humanReadableValue();
55+
$lockIdString = $postgresLockId->humanReadableValue;
5656

5757
$this->assertTrue(
5858
$row !== null,
@@ -66,7 +66,7 @@ protected function assertPgAdvisoryLockMissingInConnection(
6666
): void {
6767
$row = $this->findPostgresAdvisoryLockInConnection($dbConnection, $postgresLockId);
6868

69-
$lockIdString = $postgresLockId->humanReadableValue();
69+
$lockIdString = $postgresLockId->humanReadableValue;
7070

7171
$this->assertTrue(
7272
$row === null,
@@ -91,7 +91,7 @@ private function findPostgresAdvisoryLockInConnection(
9191
PDO $dbConnection,
9292
PostgresLockId $postgresLockId
9393
): ?object {
94-
$id = $postgresLockId->id();
94+
$id = $postgresLockId->id;
9595

9696
$lockObjectId = $id % self::POSTGRES_BLOCK_SIZE;
9797
$lockCatalogId = ($id - $lockObjectId) / self::POSTGRES_BLOCK_SIZE;
@@ -127,7 +127,7 @@ private function findPostgresAdvisoryLockInConnection(
127127

128128
private function findAllPostgresAdvisoryLocks(): array
129129
{
130-
$dbConnection = $this->createPostgresPdoConnection();
130+
$dbConnection = $this->initPostgresPdoConnection();
131131

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

149149
private function closeAllPostgresPdoConnections(): void
150150
{
151-
$this->createPostgresPdoConnection()->query(
151+
$this->initPostgresPdoConnection()->query(
152152
<<<SQL
153153
SELECT pg_terminate_backend(pid)
154154
FROM pg_stat_activity

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /