Skip to content

Commit

Permalink
Handle fetch mode deprecation of DBAL 2.11.
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed May 28, 2020
1 parent b8c48d6 commit a97df38
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
21 changes: 15 additions & 6 deletions Tests/Transport/Doctrine/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions Tests/Transport/Doctrine/DoctrineIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
17 changes: 10 additions & 7 deletions Transport/Doctrine/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit a97df38

Please sign in to comment.