Skip to content

Commit a0e4d4e

Browse files
committed
Use Pdo\Pgsql::getNotify if PHP version is >= 8.4 instead of PDO::pgsqlGetNotify.
1 parent a5a01b0 commit a0e4d4e

4 files changed

Lines changed: 95 additions & 8 deletions

File tree

src/Store/DoctrineDbalStore.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Patchlevel\EventSourcing\Store\Criteria\ToIndexCriterion;
3535
use Patchlevel\EventSourcing\Store\Header\IndexHeader;
3636
use PDO;
37+
use Pdo\Pgsql;
3738

3839
use function array_fill;
3940
use function array_filter;
@@ -49,6 +50,8 @@
4950
use function is_string;
5051
use function sprintf;
5152

53+
use const PHP_VERSION_ID;
54+
5255
final class DoctrineDbalStore implements Store, SubscriptionStore, DoctrineSchemaConfigurator
5356
{
5457
/**
@@ -386,10 +389,15 @@ public function wait(int $timeoutMilliseconds): void
386389

387390
$this->connection->executeStatement(sprintf('LISTEN "%s"', $this->config['table_name']));
388391

389-
/** @var PDO $nativeConnection */
390-
$nativeConnection = $this->connection->getNativeConnection();
391-
392-
$nativeConnection->pgsqlGetNotify(PDO::FETCH_ASSOC, $timeoutMilliseconds);
392+
if (PHP_VERSION_ID >= 80400) {
393+
/** @var Pgsql $nativeConnection */
394+
$nativeConnection = $this->connection->getNativeConnection();
395+
$nativeConnection->getNotify(PDO::FETCH_ASSOC, $timeoutMilliseconds);
396+
} else {
397+
/** @var PDO $nativeConnection */
398+
$nativeConnection = $this->connection->getNativeConnection();
399+
$nativeConnection->pgsqlGetNotify(PDO::FETCH_ASSOC, $timeoutMilliseconds);
400+
}
393401
}
394402

395403
public function setupSubscription(): void

src/Store/StreamDoctrineDbalStore.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Patchlevel\EventSourcing\Store\Header\RecordedOnHeader;
4040
use Patchlevel\EventSourcing\Store\Header\StreamNameHeader;
4141
use PDO;
42+
use Pdo\Pgsql;
4243
use Psr\Clock\ClockInterface;
4344
use Ramsey\Uuid\Uuid;
4445

@@ -58,6 +59,8 @@
5859
use function str_contains;
5960
use function str_replace;
6061

62+
use const PHP_VERSION_ID;
63+
6164
final class StreamDoctrineDbalStore implements StreamStore, SubscriptionStore, DoctrineSchemaConfigurator
6265
{
6366
/**
@@ -465,10 +468,15 @@ public function wait(int $timeoutMilliseconds): void
465468

466469
$this->connection->executeStatement(sprintf('LISTEN "%s"', $this->config['table_name']));
467470

468-
/** @var PDO $nativeConnection */
469-
$nativeConnection = $this->connection->getNativeConnection();
470-
471-
$nativeConnection->pgsqlGetNotify(PDO::FETCH_ASSOC, $timeoutMilliseconds);
471+
if (PHP_VERSION_ID >= 80400) {
472+
/** @var Pgsql $nativeConnection */
473+
$nativeConnection = $this->connection->getNativeConnection();
474+
$nativeConnection->getNotify(PDO::FETCH_ASSOC, $timeoutMilliseconds);
475+
} else {
476+
/** @var PDO $nativeConnection */
477+
$nativeConnection = $this->connection->getNativeConnection();
478+
$nativeConnection->pgsqlGetNotify(PDO::FETCH_ASSOC, $timeoutMilliseconds);
479+
}
472480
}
473481

474482
public function setupSubscription(): void

tests/Unit/Store/DoctrineDbalStoreTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileEmailChanged;
4242
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
4343
use PDO;
44+
use Pdo\Pgsql;
4445
use PHPUnit\Framework\Attributes\CoversClass;
4546
use PHPUnit\Framework\Attributes\RequiresPhp;
4647
use PHPUnit\Framework\TestCase;
@@ -1343,7 +1344,39 @@ public function testSetupSubscriptionNotPostgres(): void
13431344
$doctrineDbalStore->setupSubscription();
13441345
}
13451346

1347+
#[RequiresPhp('>= 8.4')]
13461348
public function testWait(): void
1349+
{
1350+
$nativeConnection = $this->createMock(Pgsql::class);
1351+
$nativeConnection
1352+
->expects($this->once())
1353+
->method('getNotify')
1354+
->with(PDO::FETCH_ASSOC, 100)
1355+
->willReturn([]);
1356+
1357+
$connection = $this->createMock(Connection::class);
1358+
$connection->expects($this->once())->method('executeStatement')->with('LISTEN "eventstore"')
1359+
->willReturn(1);
1360+
$connection->expects($this->once())->method('getNativeConnection')
1361+
->willReturn($nativeConnection);
1362+
1363+
$abstractPlatform = $this->createMock(PostgreSQLPlatform::class);
1364+
$connection->expects($this->once())->method('getDatabasePlatform')->willReturn($abstractPlatform);
1365+
1366+
$eventSerializer = $this->createMock(EventSerializer::class);
1367+
$headersSerializer = $this->createMock(HeadersSerializer::class);
1368+
1369+
$doctrineDbalStore = new DoctrineDbalStore(
1370+
$connection,
1371+
$eventSerializer,
1372+
$headersSerializer,
1373+
);
1374+
1375+
$doctrineDbalStore->wait(100);
1376+
}
1377+
1378+
#[RequiresPhp('< 8.4')]
1379+
public function testWaitDeprecatedFunction(): void
13471380
{
13481381
$nativeConnection = $this->getMockBuilder(PDO::class)
13491382
->disableOriginalConstructor()

tests/Unit/Store/StreamDoctrineDbalStoreTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileEmailChanged;
4545
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
4646
use PDO;
47+
use Pdo\Pgsql;
4748
use PHPUnit\Framework\Attributes\CoversClass;
49+
use PHPUnit\Framework\Attributes\RequiresPhp;
4850
use PHPUnit\Framework\TestCase;
4951
use Psr\Clock\ClockInterface;
5052
use RuntimeException;
@@ -1367,7 +1369,43 @@ public function testSetupSubscriptionNotPostgres(): void
13671369
$doctrineDbalStore->setupSubscription();
13681370
}
13691371

1372+
#[RequiresPhp('>= 8.4')]
13701373
public function testWait(): void
1374+
{
1375+
$nativeConnection = $this->createMock(Pgsql::class);
1376+
$nativeConnection
1377+
->expects($this->once())
1378+
->method('getNotify')
1379+
->with(PDO::FETCH_ASSOC, 100)
1380+
->willReturn([]);
1381+
1382+
$connection = $this->createMock(Connection::class);
1383+
$connection
1384+
->expects($this->once())
1385+
->method('executeStatement')
1386+
->with('LISTEN "event_store"')
1387+
->willReturn(1);
1388+
$connection
1389+
->expects($this->once())
1390+
->method('getNativeConnection')
1391+
->willReturn($nativeConnection);
1392+
1393+
$abstractPlatform = $this->createMock(PostgreSQLPlatform::class);
1394+
$connection->method('getDatabasePlatform')->willReturn($abstractPlatform);
1395+
1396+
$eventSerializer = $this->createMock(EventSerializer::class);
1397+
$headersSerializer = $this->createMock(HeadersSerializer::class);
1398+
1399+
$doctrineDbalStore = new StreamDoctrineDbalStore(
1400+
$connection,
1401+
$eventSerializer,
1402+
$headersSerializer,
1403+
);
1404+
$doctrineDbalStore->wait(100);
1405+
}
1406+
1407+
#[RequiresPhp('< 8.4')]
1408+
public function testWaitDeprecatedFunction(): void
13711409
{
13721410
$nativeConnection = $this->getMockBuilder(PDO::class)
13731411
->disableOriginalConstructor()

0 commit comments

Comments
 (0)