Skip to content

Code style #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
* text=auto

/.docker export-ignore
/.github export-ignore
/test export-ignore
/.docker/ export-ignore
/.github/ export-ignore
/test/ export-ignore

/.env export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/CODE_OF_CONDUCT.md export-ignore
/CONTRIBUTING.md export-ignore
/compose.yml export-ignore
/Makefile export-ignore
/phpunit.xml.dist export-ignore
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
uses: actions/checkout@v2

- name: Docker compose up
run: docker-compose up -d
run: docker compose up -d

- name: Validate composer.json and composer.lock
run: composer validate
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ setup-dev: ## Setup project for development
make composer-install

start: ## Start application silently
docker-compose up -d
docker compose up -d

stop: ## Stop application
docker-compose down
docker compose down

restart: ## Restart the application
make stop
Expand Down
File renamed without changes.
5 changes: 0 additions & 5 deletions src/LockId/LockId.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public function __construct(
}

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

private function compileId(): string
{
return $this->value !== ''
? $this->key . ':' . $this->value
Expand Down
6 changes: 3 additions & 3 deletions src/LockId/PostgresLockId.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public function __construct(
public static function fromLockId(
LockId $lockId,
): self {
$humanReadableValue = (string)$lockId;
$lockStringId = (string)$lockId;

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

Expand Down
30 changes: 15 additions & 15 deletions src/Locker/PostgresAdvisoryLocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public function tryAcquireLock(
PDO $dbConnection,
PostgresLockId $postgresLockId,
): bool {
// TODO: Need to cleanup humanReadableValue?
// TODO: Need to sanitize humanReadableValue?
$statement = $dbConnection->prepare(
<<<SQL
SELECT pg_try_advisory_lock(:lock_id); -- $postgresLockId->humanReadableValue
SQL
SELECT PG_TRY_ADVISORY_LOCK(:lock_id); -- $postgresLockId->humanReadableValue
SQL,
);
$statement->execute(
[
'lock_id' => $postgresLockId->id,
]
],
);

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

throw new LogicException(
"Transaction-level advisory lock `$lockId` cannot be acquired outside of transaction"
"Transaction-level advisory lock `$lockId` cannot be acquired outside of transaction",
);
}

// TODO: Need to cleanup humanReadableValue?
// TODO: Need to sanitize humanReadableValue?
$statement = $dbConnection->prepare(
<<<SQL
SELECT pg_try_advisory_xact_lock(:lock_id); -- $postgresLockId->humanReadableValue
SQL
SELECT PG_TRY_ADVISORY_XACT_LOCK(:lock_id); -- $postgresLockId->humanReadableValue
SQL,
);
$statement->execute(
[
'lock_id' => $postgresLockId->id,
]
],
);

return $statement->fetchColumn(0);
Expand All @@ -70,14 +70,14 @@ public function releaseLock(
PostgresLockId $postgresLockId,
): bool {
$statement = $dbConnection->prepare(
<<<'SQL'
SELECT pg_advisory_unlock(:lock_id);
SQL
<<<SQL
SELECT PG_ADVISORY_UNLOCK(:lock_id); -- $postgresLockId->humanReadableValue
SQL,
);
$statement->execute(
[
'lock_id' => $postgresLockId->id,
]
],
);

return $statement->fetchColumn(0);
Expand All @@ -88,8 +88,8 @@ public function releaseAllLocks(
): void {
$statement = $dbConnection->prepare(
<<<'SQL'
SELECT pg_advisory_unlock_all();
SQL
SELECT PG_ADVISORY_UNLOCK_ALL();
SQL,
);
$statement->execute();
}
Expand Down
42 changes: 21 additions & 21 deletions test/Integration/AbstractIntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function assertPgAdvisoryLockExistsInConnection(

$this->assertTrue(
$row !== null,
"Lock id `$lockIdString` does not exists"
"Lock id `$lockIdString` does not exists",
);
}

Expand All @@ -70,7 +70,7 @@ protected function assertPgAdvisoryLockMissingInConnection(

$this->assertTrue(
$row === null,
"Lock id `$lockIdString` is present"
"Lock id `$lockIdString` is present",
);
}

Expand All @@ -83,7 +83,7 @@ protected function assertPgAdvisoryLocksCount(
$this->assertSame(
$expectedCount,
$rowsCount,
"Failed asserting that advisory locks actual count $rowsCount matches expected count $expectedCount."
"Failed asserting that advisory locks actual count $rowsCount matches expected count $expectedCount.",
);
}

Expand All @@ -98,22 +98,22 @@ private function findPostgresAdvisoryLockInConnection(

$statement = $dbConnection->prepare(
<<<'SQL'
SELECT *
FROM pg_locks
WHERE locktype = 'advisory'
AND classid = :lock_catalog_id
AND objid = :lock_object_id
AND pid = :connection_pid
AND mode = :mode
SQL
SELECT *
FROM pg_locks
WHERE locktype = 'advisory'
AND classid = :lock_catalog_id
AND objid = :lock_object_id
AND pid = :connection_pid
AND mode = :mode
SQL,
);
$statement->execute(
[
'lock_catalog_id' => $lockCatalogId,
'lock_object_id' => $lockObjectId,
'connection_pid' => $dbConnection->pgsqlGetPid(),
'mode' => self::MODE_EXCLUSIVE,
]
],
);

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

$statement = $dbConnection->prepare(
<<<'SQL'
SELECT *
FROM pg_locks
WHERE locktype = 'advisory'
AND mode = :mode
SQL
SELECT *
FROM pg_locks
WHERE locktype = 'advisory'
AND mode = :mode
SQL,
);
$statement->execute(
[
'mode' => self::MODE_EXCLUSIVE,
]
],
);

return $statement->fetchAll(PDO::FETCH_OBJ);
Expand All @@ -150,10 +150,10 @@ private function closeAllPostgresPdoConnections(): void
{
$this->initPostgresPdoConnection()->query(
<<<'SQL'
SELECT pg_terminate_backend(pid)
SELECT PG_TERMINATE_BACKEND(pid)
FROM pg_stat_activity
WHERE pid <> pg_backend_pid()
SQL
WHERE pid <> PG_BACKEND_PID()
SQL,
);
}
}
2 changes: 1 addition & 1 deletion test/Integration/Locker/PostgresAdvisoryLockerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function test_it_cannot_acquire_lock_within_transaction_not_in_transactio
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage(
"Transaction-level advisory lock `test` cannot be acquired outside of transaction"
'Transaction-level advisory lock `test` cannot be acquired outside of transaction',
);

$locker = $this->initLocker();
Expand Down
25 changes: 23 additions & 2 deletions test/Unit/LockId/LockIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
namespace Cog\Test\DbLocker\Unit\LockId;

use Cog\DbLocker\LockId\LockId;
use Cog\Test\DbLocker\Unit\AbstractUnitTestCase;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class LockIdTest extends TestCase
final class LockIdTest extends AbstractUnitTestCase
{
public function test_it_can_create_lock_id(): void
{
Expand All @@ -26,13 +26,34 @@ public function test_it_can_create_lock_id(): void
$this->assertSame('test', (string)$lockId);
}

public function test_it_can_create_lock_id_with_space_key(): void
{
$lockId = new LockId(' ');

$this->assertSame(' ', (string)$lockId);
}

public function test_it_can_create_lock_id_with_spaced_key(): void
{
$lockId = new LockId(' test ');

$this->assertSame(' test ', (string)$lockId);
}

public function test_it_can_create_lock_id_with_value(): void
{
$lockId = new LockId('test', '1');

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

public function test_it_can_create_lock_id_with_value_and_spaced_key(): void
{
$lockId = new LockId(' test ', '1');

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

public function test_it_cannot_create_lock_id_with_empty_key(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
Loading