diff --git a/Tests/Transport/Doctrine/ConnectionTest.php b/Tests/Transport/Doctrine/ConnectionTest.php index bd7fff76..d34e45d2 100644 --- a/Tests/Transport/Doctrine/ConnectionTest.php +++ b/Tests/Transport/Doctrine/ConnectionTest.php @@ -12,7 +12,8 @@ namespace Symfony\Component\Messenger\Tests\Transport\Doctrine; use Doctrine\DBAL\DBALException; -use Doctrine\DBAL\Driver\Statement; +use Doctrine\DBAL\Driver\ResultStatement; +use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatibleResultStatement; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Query\QueryBuilder; use Doctrine\DBAL\Schema\AbstractSchemaManager; @@ -142,11 +143,16 @@ private function getQueryBuilderMock() return $queryBuilder; } - private function getStatementMock($expectedResult): Statement + private function getStatementMock($expectedResult): ResultStatement { - $stmt = $this->createMock(Statement::class); + $mockedInterface = interface_exists(ForwardCompatibleResultStatement::class) + ? ForwardCompatibleResultStatement::class + : ResultStatement::class; + + $stmt = $this->createMock($mockedInterface); + $stmt->expects($this->once()) - ->method('fetch') + ->method(method_exists($mockedInterface, 'fetchAssociative') ? 'fetchAssociative' : 'fetch') ->willReturn($expectedResult); return $stmt; @@ -306,9 +312,12 @@ public function testFindAll() 'headers' => json_encode(['type' => DummyMessage::class]), ]; - $stmt = $this->createMock(Statement::class); + $mockedInterface = interface_exists(ForwardCompatibleResultStatement::class) + ? ForwardCompatibleResultStatement::class + : ResultStatement::class; + $stmt = $this->createMock($mockedInterface); $stmt->expects($this->once()) - ->method('fetchAll') + ->method(method_exists($mockedInterface, 'fetchAllAssociative') ? 'fetchAllAssociative' : 'fetchAll') ->willReturn([$message1, $message2]); $driverConnection diff --git a/Tests/Transport/Doctrine/DoctrineIntegrationTest.php b/Tests/Transport/Doctrine/DoctrineIntegrationTest.php index f417283a..45ca64b7 100644 --- a/Tests/Transport/Doctrine/DoctrineIntegrationTest.php +++ b/Tests/Transport/Doctrine/DoctrineIntegrationTest.php @@ -64,15 +64,14 @@ public function testSendWithDelay() { $this->connection->send('{"message": "Hi i am delayed"}', ['type' => DummyMessage::class], 600000); - $available_at = $this->driverConnection->createQueryBuilder() + $stmt = $this->driverConnection->createQueryBuilder() ->select('m.available_at') ->from('messenger_messages', 'm') ->where('m.body = :body') ->setParameter(':body', '{"message": "Hi i am delayed"}') - ->execute() - ->fetchColumn(); + ->execute(); - $available_at = new \DateTime($available_at); + $available_at = new \DateTime(method_exists($stmt, 'fetchOne') ? $stmt->fetchOne() : $stmt->fetchColumn()); $now = new \DateTime(); $now->modify('+60 seconds'); diff --git a/Transport/Doctrine/Connection.php b/Transport/Doctrine/Connection.php index 6004bf36..4f5b43c4 100644 --- a/Transport/Doctrine/Connection.php +++ b/Transport/Doctrine/Connection.php @@ -159,11 +159,12 @@ public function get(): ?array ->setMaxResults(1); // use SELECT ... FOR UPDATE to lock table - $doctrineEnvelope = $this->executeQuery( + $stmt = $this->executeQuery( $query->getSQL().' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(), $query->getParameters(), $query->getParameterTypes() - )->fetch(); + ); + $doctrineEnvelope = method_exists($stmt, 'fetchAssociative') ? $stmt->fetchAssociative() : $stmt->fetch(); if (false === $doctrineEnvelope) { $this->driverConnection->commit(); @@ -249,7 +250,9 @@ public function getMessageCount(): int ->select('COUNT(m.id) as message_count') ->setMaxResults(1); - return $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters(), $queryBuilder->getParameterTypes())->fetchColumn(); + $stmt = $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters(), $queryBuilder->getParameterTypes()); + + return method_exists($stmt, 'fetchOne') ? $stmt->fetchOne() : $stmt->fetchColumn(); } public function findAll(int $limit = null): array @@ -259,7 +262,8 @@ public function findAll(int $limit = null): array $queryBuilder->setMaxResults($limit); } - $data = $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters(), $queryBuilder->getParameterTypes())->fetchAll(); + $stmt = $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters(), $queryBuilder->getParameterTypes()); + $data = method_exists($stmt, 'fetchAllAssociative') ? $stmt->fetchAllAssociative() : $stmt->fetchAll(); return array_map(function ($doctrineEnvelope) { return $this->decodeEnvelopeHeaders($doctrineEnvelope); @@ -271,9 +275,8 @@ public function find($id): ?array $queryBuilder = $this->createQueryBuilder() ->where('m.id = ?'); - $data = $this->executeQuery($queryBuilder->getSQL(), [ - $id, - ])->fetch(); + $stmt = $this->executeQuery($queryBuilder->getSQL(), [$id]); + $data = method_exists($stmt, 'fetchAssociative') ? $stmt->fetchAssociative() : $stmt->fetch(); return false === $data ? null : $this->decodeEnvelopeHeaders($data); }