Skip to content

Commit 1d7c0ef

Browse files
authored
Code style (#4)
1 parent a44fcb6 commit 1d7c0ef

File tree

10 files changed

+72
-55
lines changed

10 files changed

+72
-55
lines changed

.gitattributes

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
* text=auto
22

3-
/.docker export-ignore
4-
/.github export-ignore
5-
/test export-ignore
3+
/.docker/ export-ignore
4+
/.github/ export-ignore
5+
/test/ export-ignore
6+
67
/.env export-ignore
78
/.gitattributes export-ignore
89
/.gitignore export-ignore
9-
/CODE_OF_CONDUCT.md export-ignore
10-
/CONTRIBUTING.md export-ignore
10+
/compose.yml export-ignore
11+
/Makefile export-ignore
1112
/phpunit.xml.dist export-ignore

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
uses: actions/checkout@v2
1212

1313
- name: Docker compose up
14-
run: docker-compose up -d
14+
run: docker compose up -d
1515

1616
- name: Validate composer.json and composer.lock
1717
run: composer validate

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ setup-dev: ## Setup project for development
88
make composer-install
99

1010
start: ## Start application silently
11-
docker-compose up -d
11+
docker compose up -d
1212

1313
stop: ## Stop application
14-
docker-compose down
14+
docker compose down
1515

1616
restart: ## Restart the application
1717
make stop
File renamed without changes.

src/LockId/LockId.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ public function __construct(
2727
}
2828

2929
public function __toString(): string
30-
{
31-
return $this->compileId();
32-
}
33-
34-
private function compileId(): string
3530
{
3631
return $this->value !== ''
3732
? $this->key . ':' . $this->value

src/LockId/PostgresLockId.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public function __construct(
3535
public static function fromLockId(
3636
LockId $lockId,
3737
): self {
38-
$humanReadableValue = (string)$lockId;
38+
$lockStringId = (string)$lockId;
3939

4040
return new self(
41-
self::generateIdFromString($humanReadableValue),
42-
$humanReadableValue
41+
id: self::generateIdFromString($lockStringId),
42+
humanReadableValue: $lockStringId,
4343
);
4444
}
4545

src/Locker/PostgresAdvisoryLocker.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ public function tryAcquireLock(
2323
PDO $dbConnection,
2424
PostgresLockId $postgresLockId,
2525
): bool {
26-
// TODO: Need to cleanup humanReadableValue?
26+
// TODO: Need to sanitize humanReadableValue?
2727
$statement = $dbConnection->prepare(
2828
<<<SQL
29-
SELECT pg_try_advisory_lock(:lock_id); -- $postgresLockId->humanReadableValue
30-
SQL
29+
SELECT PG_TRY_ADVISORY_LOCK(:lock_id); -- $postgresLockId->humanReadableValue
30+
SQL,
3131
);
3232
$statement->execute(
3333
[
3434
'lock_id' => $postgresLockId->id,
35-
]
35+
],
3636
);
3737

3838
return $statement->fetchColumn(0);
@@ -46,20 +46,20 @@ public function tryAcquireLockWithinTransaction(
4646
$lockId = $postgresLockId->humanReadableValue;
4747

4848
throw new LogicException(
49-
"Transaction-level advisory lock `$lockId` cannot be acquired outside of transaction"
49+
"Transaction-level advisory lock `$lockId` cannot be acquired outside of transaction",
5050
);
5151
}
5252

53-
// TODO: Need to cleanup humanReadableValue?
53+
// TODO: Need to sanitize humanReadableValue?
5454
$statement = $dbConnection->prepare(
5555
<<<SQL
56-
SELECT pg_try_advisory_xact_lock(:lock_id); -- $postgresLockId->humanReadableValue
57-
SQL
56+
SELECT PG_TRY_ADVISORY_XACT_LOCK(:lock_id); -- $postgresLockId->humanReadableValue
57+
SQL,
5858
);
5959
$statement->execute(
6060
[
6161
'lock_id' => $postgresLockId->id,
62-
]
62+
],
6363
);
6464

6565
return $statement->fetchColumn(0);
@@ -70,14 +70,14 @@ public function releaseLock(
7070
PostgresLockId $postgresLockId,
7171
): bool {
7272
$statement = $dbConnection->prepare(
73-
<<<'SQL'
74-
SELECT pg_advisory_unlock(:lock_id);
75-
SQL
73+
<<<SQL
74+
SELECT PG_ADVISORY_UNLOCK(:lock_id); -- $postgresLockId->humanReadableValue
75+
SQL,
7676
);
7777
$statement->execute(
7878
[
7979
'lock_id' => $postgresLockId->id,
80-
]
80+
],
8181
);
8282

8383
return $statement->fetchColumn(0);
@@ -88,8 +88,8 @@ public function releaseAllLocks(
8888
): void {
8989
$statement = $dbConnection->prepare(
9090
<<<'SQL'
91-
SELECT pg_advisory_unlock_all();
92-
SQL
91+
SELECT PG_ADVISORY_UNLOCK_ALL();
92+
SQL,
9393
);
9494
$statement->execute();
9595
}

test/Integration/AbstractIntegrationTestCase.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function assertPgAdvisoryLockExistsInConnection(
5656

5757
$this->assertTrue(
5858
$row !== null,
59-
"Lock id `$lockIdString` does not exists"
59+
"Lock id `$lockIdString` does not exists",
6060
);
6161
}
6262

@@ -70,7 +70,7 @@ protected function assertPgAdvisoryLockMissingInConnection(
7070

7171
$this->assertTrue(
7272
$row === null,
73-
"Lock id `$lockIdString` is present"
73+
"Lock id `$lockIdString` is present",
7474
);
7575
}
7676

@@ -83,7 +83,7 @@ protected function assertPgAdvisoryLocksCount(
8383
$this->assertSame(
8484
$expectedCount,
8585
$rowsCount,
86-
"Failed asserting that advisory locks actual count $rowsCount matches expected count $expectedCount."
86+
"Failed asserting that advisory locks actual count $rowsCount matches expected count $expectedCount.",
8787
);
8888
}
8989

@@ -98,22 +98,22 @@ private function findPostgresAdvisoryLockInConnection(
9898

9999
$statement = $dbConnection->prepare(
100100
<<<'SQL'
101-
SELECT *
102-
FROM pg_locks
103-
WHERE locktype = 'advisory'
104-
AND classid = :lock_catalog_id
105-
AND objid = :lock_object_id
106-
AND pid = :connection_pid
107-
AND mode = :mode
108-
SQL
101+
SELECT *
102+
FROM pg_locks
103+
WHERE locktype = 'advisory'
104+
AND classid = :lock_catalog_id
105+
AND objid = :lock_object_id
106+
AND pid = :connection_pid
107+
AND mode = :mode
108+
SQL,
109109
);
110110
$statement->execute(
111111
[
112112
'lock_catalog_id' => $lockCatalogId,
113113
'lock_object_id' => $lockObjectId,
114114
'connection_pid' => $dbConnection->pgsqlGetPid(),
115115
'mode' => self::MODE_EXCLUSIVE,
116-
]
116+
],
117117
);
118118

119119
$result = $statement->fetchObject();
@@ -131,16 +131,16 @@ private function findAllPostgresAdvisoryLocks(): array
131131

132132
$statement = $dbConnection->prepare(
133133
<<<'SQL'
134-
SELECT *
135-
FROM pg_locks
136-
WHERE locktype = 'advisory'
137-
AND mode = :mode
138-
SQL
134+
SELECT *
135+
FROM pg_locks
136+
WHERE locktype = 'advisory'
137+
AND mode = :mode
138+
SQL,
139139
);
140140
$statement->execute(
141141
[
142142
'mode' => self::MODE_EXCLUSIVE,
143-
]
143+
],
144144
);
145145

146146
return $statement->fetchAll(PDO::FETCH_OBJ);
@@ -150,10 +150,10 @@ private function closeAllPostgresPdoConnections(): void
150150
{
151151
$this->initPostgresPdoConnection()->query(
152152
<<<'SQL'
153-
SELECT pg_terminate_backend(pid)
153+
SELECT PG_TERMINATE_BACKEND(pid)
154154
FROM pg_stat_activity
155-
WHERE pid <> pg_backend_pid()
156-
SQL
155+
WHERE pid <> PG_BACKEND_PID()
156+
SQL,
157157
);
158158
}
159159
}

test/Integration/Locker/PostgresAdvisoryLockerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public function test_it_cannot_acquire_lock_within_transaction_not_in_transactio
264264
{
265265
$this->expectException(LogicException::class);
266266
$this->expectExceptionMessage(
267-
"Transaction-level advisory lock `test` cannot be acquired outside of transaction"
267+
'Transaction-level advisory lock `test` cannot be acquired outside of transaction',
268268
);
269269

270270
$locker = $this->initLocker();

test/Unit/LockId/LockIdTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
namespace Cog\Test\DbLocker\Unit\LockId;
1515

1616
use Cog\DbLocker\LockId\LockId;
17+
use Cog\Test\DbLocker\Unit\AbstractUnitTestCase;
1718
use InvalidArgumentException;
18-
use PHPUnit\Framework\TestCase;
1919

20-
final class LockIdTest extends TestCase
20+
final class LockIdTest extends AbstractUnitTestCase
2121
{
2222
public function test_it_can_create_lock_id(): void
2323
{
@@ -26,13 +26,34 @@ public function test_it_can_create_lock_id(): void
2626
$this->assertSame('test', (string)$lockId);
2727
}
2828

29+
public function test_it_can_create_lock_id_with_space_key(): void
30+
{
31+
$lockId = new LockId(' ');
32+
33+
$this->assertSame(' ', (string)$lockId);
34+
}
35+
36+
public function test_it_can_create_lock_id_with_spaced_key(): void
37+
{
38+
$lockId = new LockId(' test ');
39+
40+
$this->assertSame(' test ', (string)$lockId);
41+
}
42+
2943
public function test_it_can_create_lock_id_with_value(): void
3044
{
3145
$lockId = new LockId('test', '1');
3246

3347
$this->assertSame('test:1', (string)$lockId);
3448
}
3549

50+
public function test_it_can_create_lock_id_with_value_and_spaced_key(): void
51+
{
52+
$lockId = new LockId(' test ', '1');
53+
54+
$this->assertSame(' test :1', (string)$lockId);
55+
}
56+
3657
public function test_it_cannot_create_lock_id_with_empty_key(): void
3758
{
3859
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)