From a03885168e05253073809d93fa8a3ba24f14032d Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 23 Sep 2020 20:53:58 +0200 Subject: [PATCH] fix compatibility with Doctrine DBAL 3 --- src/Symfony/Component/Cache/Traits/PdoTrait.php | 4 +++- src/Symfony/Component/Lock/Store/PdoStore.php | 4 +++- .../Tests/Transport/Doctrine/ConnectionTest.php | 15 +++++++++++++-- .../Messenger/Transport/Doctrine/Connection.php | 6 ++++-- .../Transport/Doctrine/DoctrineReceiver.php | 13 +++++++------ .../Transport/Doctrine/DoctrineSender.php | 3 ++- 6 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Cache/Traits/PdoTrait.php b/src/Symfony/Component/Cache/Traits/PdoTrait.php index 51761af4c060b..02a3e20c79a03 100644 --- a/src/Symfony/Component/Cache/Traits/PdoTrait.php +++ b/src/Symfony/Component/Cache/Traits/PdoTrait.php @@ -17,6 +17,7 @@ use Doctrine\DBAL\Driver\Result as DriverResult; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception\TableNotFoundException; use Doctrine\DBAL\Schema\Schema; use Symfony\Component\Cache\Exception\InvalidArgumentException; @@ -85,6 +86,7 @@ private function init($connOrDsn, string $namespace, int $defaultLifetime, array * * @throws \PDOException When the table already exists * @throws DBALException When the table already exists + * @throws Exception When the table already exists * @throws \DomainException When an unsupported PDO driver is used */ public function createTable() @@ -392,7 +394,7 @@ protected function doSave(array $values, int $lifetime) if (null === $driver && !($result instanceof DriverResult ? $result : $stmt)->rowCount()) { try { $insertStmt->execute(); - } catch (DBALException $e) { + } catch (DBALException | Exception $e) { } catch (\PDOException $e) { // A concurrent write won, let it be } diff --git a/src/Symfony/Component/Lock/Store/PdoStore.php b/src/Symfony/Component/Lock/Store/PdoStore.php index 9d01d6dcea1a0..e1ebcada991bf 100644 --- a/src/Symfony/Component/Lock/Store/PdoStore.php +++ b/src/Symfony/Component/Lock/Store/PdoStore.php @@ -15,6 +15,7 @@ use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver\Result; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Schema\Schema; use Symfony\Component\Lock\Exception\InvalidArgumentException; use Symfony\Component\Lock\Exception\InvalidTtlException; @@ -127,7 +128,7 @@ public function save(Key $key) try { $stmt->execute(); - } catch (DBALException $e) { + } catch (DBALException | Exception $e) { // the lock is already acquired. It could be us. Let's try to put off. $this->putOffExpiration($key, $this->initialTtl); } catch (\PDOException $e) { @@ -250,6 +251,7 @@ private function getConnection() * * @throws \PDOException When the table already exists * @throws DBALException When the table already exists + * @throws Exception When the table already exists * @throws \DomainException When an unsupported PDO driver is used */ public function createTable(): void diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Doctrine/ConnectionTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Doctrine/ConnectionTest.php index 8d76d1c468534..66353af62653d 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Doctrine/ConnectionTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Doctrine/ConnectionTest.php @@ -13,6 +13,7 @@ use Doctrine\DBAL\Abstraction\Result; use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Query\QueryBuilder; use Doctrine\DBAL\Schema\AbstractSchemaManager; @@ -87,7 +88,12 @@ public function testItThrowsATransportExceptionIfItCannotAcknowledgeMessage() { $this->expectException('Symfony\Component\Messenger\Exception\TransportException'); $driverConnection = $this->getDBALConnectionMock(); - $driverConnection->method('delete')->willThrowException(new DBALException()); + + if (class_exists(Exception::class)) { + $driverConnection->method('delete')->willThrowException(new Exception()); + } else { + $driverConnection->method('delete')->willThrowException(new DBALException()); + } $connection = new Connection([], $driverConnection); $connection->ack('dummy_id'); @@ -97,7 +103,12 @@ public function testItThrowsATransportExceptionIfItCannotRejectMessage() { $this->expectException('Symfony\Component\Messenger\Exception\TransportException'); $driverConnection = $this->getDBALConnectionMock(); - $driverConnection->method('delete')->willThrowException(new DBALException()); + + if (class_exists(Exception::class)) { + $driverConnection->method('delete')->willThrowException(new Exception()); + } else { + $driverConnection->method('delete')->willThrowException(new DBALException()); + } $connection = new Connection([], $driverConnection); $connection->reject('dummy_id'); diff --git a/src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php b/src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php index dbaee871289c5..bdd7245cc995e 100644 --- a/src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php +++ b/src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php @@ -14,6 +14,7 @@ use Doctrine\DBAL\Connection as DBALConnection; use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver\Result; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception\TableNotFoundException; use Doctrine\DBAL\Query\QueryBuilder; use Doctrine\DBAL\Schema\Comparator; @@ -110,6 +111,7 @@ public static function buildConfiguration(string $dsn, array $options = []): arr * @return string The inserted id * * @throws \Doctrine\DBAL\DBALException + * @throws \Doctrine\DBAL\Exception */ public function send(string $body, array $headers, int $delay = 0): string { @@ -205,7 +207,7 @@ public function ack(string $id): bool { try { return $this->driverConnection->delete($this->configuration['table_name'], ['id' => $id]) > 0; - } catch (DBALException $exception) { + } catch (DBALException | Exception $exception) { throw new TransportException($exception->getMessage(), 0, $exception); } } @@ -214,7 +216,7 @@ public function reject(string $id): bool { try { return $this->driverConnection->delete($this->configuration['table_name'], ['id' => $id]) > 0; - } catch (DBALException $exception) { + } catch (DBALException | Exception $exception) { throw new TransportException($exception->getMessage(), 0, $exception); } } diff --git a/src/Symfony/Component/Messenger/Transport/Doctrine/DoctrineReceiver.php b/src/Symfony/Component/Messenger/Transport/Doctrine/DoctrineReceiver.php index 071cf2812acc6..2845943c1f035 100644 --- a/src/Symfony/Component/Messenger/Transport/Doctrine/DoctrineReceiver.php +++ b/src/Symfony/Component/Messenger/Transport/Doctrine/DoctrineReceiver.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Messenger\Transport\Doctrine; use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception\RetryableException; use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Exception\LogicException; @@ -58,7 +59,7 @@ public function get(): iterable } return []; - } catch (DBALException $exception) { + } catch (DBALException | Exception $exception) { throw new TransportException($exception->getMessage(), 0, $exception); } @@ -76,7 +77,7 @@ public function ack(Envelope $envelope): void { try { $this->connection->ack($this->findDoctrineReceivedStamp($envelope)->getId()); - } catch (DBALException $exception) { + } catch (DBALException | Exception $exception) { throw new TransportException($exception->getMessage(), 0, $exception); } } @@ -88,7 +89,7 @@ public function reject(Envelope $envelope): void { try { $this->connection->reject($this->findDoctrineReceivedStamp($envelope)->getId()); - } catch (DBALException $exception) { + } catch (DBALException | Exception $exception) { throw new TransportException($exception->getMessage(), 0, $exception); } } @@ -100,7 +101,7 @@ public function getMessageCount(): int { try { return $this->connection->getMessageCount(); - } catch (DBALException $exception) { + } catch (DBALException | Exception $exception) { throw new TransportException($exception->getMessage(), 0, $exception); } } @@ -112,7 +113,7 @@ public function all(int $limit = null): iterable { try { $doctrineEnvelopes = $this->connection->findAll($limit); - } catch (DBALException $exception) { + } catch (DBALException | Exception $exception) { throw new TransportException($exception->getMessage(), 0, $exception); } @@ -128,7 +129,7 @@ public function find($id): ?Envelope { try { $doctrineEnvelope = $this->connection->find($id); - } catch (DBALException $exception) { + } catch (DBALException | Exception $exception) { throw new TransportException($exception->getMessage(), 0, $exception); } diff --git a/src/Symfony/Component/Messenger/Transport/Doctrine/DoctrineSender.php b/src/Symfony/Component/Messenger/Transport/Doctrine/DoctrineSender.php index ecfb5113e0624..95f886c12e6a0 100644 --- a/src/Symfony/Component/Messenger/Transport/Doctrine/DoctrineSender.php +++ b/src/Symfony/Component/Messenger/Transport/Doctrine/DoctrineSender.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Messenger\Transport\Doctrine; use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Exception; use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Exception\TransportException; use Symfony\Component\Messenger\Stamp\DelayStamp; @@ -47,7 +48,7 @@ public function send(Envelope $envelope): Envelope try { $id = $this->connection->send($encodedMessage['body'], $encodedMessage['headers'] ?? [], $delay); - } catch (DBALException $exception) { + } catch (DBALException | Exception $exception) { throw new TransportException($exception->getMessage(), 0, $exception); }