Skip to content

Commit

Permalink
Merge branch '4.4' into 5.0
Browse files Browse the repository at this point in the history
* 4.4:
  Correctly use doctrine/dbal v3+
  Correctly use doctrine/dbal v3+
  • Loading branch information
nicolas-grekas committed Jun 9, 2020
2 parents 6afb50a + 8fb7091 commit 1e52e0a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
17 changes: 9 additions & 8 deletions Tests/Transport/Doctrine/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Messenger\Tests\Transport\Doctrine;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
Expand All @@ -29,7 +30,7 @@ public function testGetAMessageWillChangeItsStatus()
$queryBuilder = $this->getQueryBuilderMock();
$driverConnection = $this->getDBALConnectionMock();
$schemaSynchronizer = $this->getSchemaSynchronizerMock();
$stmt = $this->getStatementMock([
$stmt = $this->getResultMock([
'id' => 1,
'body' => '{"message":"Hi"}',
'headers' => json_encode(['type' => DummyMessage::class]),
Expand Down Expand Up @@ -63,7 +64,7 @@ public function testGetWithNoPendingMessageWillReturnNull()
$queryBuilder = $this->getQueryBuilderMock();
$driverConnection = $this->getDBALConnectionMock();
$schemaSynchronizer = $this->getSchemaSynchronizerMock();
$stmt = $this->getStatementMock(false);
$stmt = $this->getResultMock(false);

$queryBuilder
->method('getParameters')
Expand Down Expand Up @@ -142,12 +143,12 @@ private function getQueryBuilderMock()
return $queryBuilder;
}

private function getStatementMock($expectedResult)
private function getResultMock($expectedResult)
{
$stmt = $this->createMock(Statement::class);
$stmt = $this->createMock(interface_exists(Result::class) ? Result::class : Statement::class);

$stmt->expects($this->once())
->method(method_exists(Statement::class, 'fetchAssociative') ? 'fetchAssociative' : 'fetch')
->method(interface_exists(Result::class) ? 'fetchAssociative' : 'fetch')
->willReturn($expectedResult);

return $stmt;
Expand Down Expand Up @@ -262,7 +263,7 @@ public function testFind()
$driverConnection = $this->getDBALConnectionMock();
$schemaSynchronizer = $this->getSchemaSynchronizerMock();
$id = 1;
$stmt = $this->getStatementMock([
$stmt = $this->getResultMock([
'id' => $id,
'body' => '{"message":"Hi"}',
'headers' => json_encode(['type' => DummyMessage::class]),
Expand Down Expand Up @@ -307,9 +308,9 @@ public function testFindAll()
'headers' => json_encode(['type' => DummyMessage::class]),
];

$stmt = $this->createMock(Statement::class);
$stmt = $this->createMock(interface_exists(Result::class) ? Result::class : Statement::class);
$stmt->expects($this->once())
->method(method_exists(Statement::class, 'fetchAllAssociative') ? 'fetchAllAssociative' : 'fetchAll')
->method(interface_exists(Result::class) ? 'fetchAllAssociative' : 'fetchAll')
->willReturn([$message1, $message2]);

$driverConnection
Expand Down
3 changes: 2 additions & 1 deletion Tests/Transport/Doctrine/DoctrineIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger\Tests\Transport\Doctrine;

use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Version;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -71,7 +72,7 @@ public function testSendWithDelay()
->setParameter(':body', '{"message": "Hi i am delayed"}')
->execute();

$available_at = new \DateTime(method_exists($stmt, 'fetchOne') ? $stmt->fetchOne() : $stmt->fetchColumn());
$available_at = new \DateTime($stmt instanceof Result ? $stmt->fetchOne() : $stmt->fetchColumn());

$now = new \DateTime();
$now->modify('+60 seconds');
Expand Down
9 changes: 5 additions & 4 deletions Transport/Doctrine/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\Connection as DBALConnection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Schema\Schema;
Expand Down Expand Up @@ -163,7 +164,7 @@ public function get(): ?array
$query->getParameters(),
$query->getParameterTypes()
);
$doctrineEnvelope = method_exists($stmt, 'fetchAssociative') ? $stmt->fetchAssociative() : $stmt->fetch();
$doctrineEnvelope = $stmt instanceof Result ? $stmt->fetchAssociative() : $stmt->fetch();

if (false === $doctrineEnvelope) {
$this->driverConnection->commit();
Expand Down Expand Up @@ -251,7 +252,7 @@ public function getMessageCount(): int

$stmt = $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters(), $queryBuilder->getParameterTypes());

return method_exists($stmt, 'fetchOne') ? $stmt->fetchOne() : $stmt->fetchColumn();
return $stmt instanceof Result ? $stmt->fetchOne() : $stmt->fetchColumn();
}

public function findAll(int $limit = null): array
Expand All @@ -262,7 +263,7 @@ public function findAll(int $limit = null): array
}

$stmt = $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters(), $queryBuilder->getParameterTypes());
$data = method_exists($stmt, 'fetchAllAssociative') ? $stmt->fetchAllAssociative() : $stmt->fetchAll();
$data = $stmt instanceof Result ? $stmt->fetchAllAssociative() : $stmt->fetchAll();

return array_map(function ($doctrineEnvelope) {
return $this->decodeEnvelopeHeaders($doctrineEnvelope);
Expand All @@ -275,7 +276,7 @@ public function find($id): ?array
->where('m.id = ?');

$stmt = $this->executeQuery($queryBuilder->getSQL(), [$id]);
$data = method_exists($stmt, 'fetchAssociative') ? $stmt->fetchAssociative() : $stmt->fetch();
$data = $stmt instanceof Result ? $stmt->fetchAssociative() : $stmt->fetch();

return false === $data ? null : $this->decodeEnvelopeHeaders($data);
}
Expand Down

0 comments on commit 1e52e0a

Please sign in to comment.