Skip to content

Commit b9e5750

Browse files
committed
Juggle connect()/getServerVersion()
1 parent f4ab0d6 commit b9e5750

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

pkg/dbal/ManagerRegistryConnectionFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ private function establishConnection(): Connection
6161
{
6262
/** @var Connection $connection */
6363
$connection = $this->registry->getConnection($this->config['connection_name']);
64-
$connection->getServerVersion(); // calls connect() internally
64+
if (method_exists($connection, 'connect')) {
65+
// DBAL < 4
66+
$connection->connect();
67+
} else {
68+
// DBAL >= 4, calls connect() internally
69+
$connection->getServerVersion();
70+
}
6571

6672
return $connection;
6773
}

pkg/enqueue-bundle/Consumption/Extension/DoctrinePingConnectionExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ public function onMessageReceived(MessageReceived $context): void
3636
);
3737

3838
$connection->close();
39-
$connection->getServerVersion(); // calls connect() internally
39+
if (method_exists($connection, 'connect')) {
40+
// DBAL < 4
41+
$connection->connect();
42+
} else {
43+
// DBAL >= 4, calls connect() internally
44+
$connection->getServerVersion();
45+
}
4046

4147
$context->getLogger()->debug(
4248
'[DoctrinePingConnectionExtension] Connection is active now.'

pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/DoctrinePingConnectionExtensionTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,16 @@ public function testShouldNotReconnectIfConnectionIsOK()
4141
->expects($this->never())
4242
->method('close')
4343
;
44-
$connection
45-
->expects($this->never())
46-
->method('getServerVersion')
47-
;
44+
if (method_exists($connection, 'connect')) {
45+
// DBAL < 4
46+
$connection->expects($this->never())
47+
->method('connect');
48+
} else {
49+
// DBAL >= 4, calls connect() internally
50+
$connection
51+
->expects($this->never())
52+
->method('getServerVersion');
53+
}
4854

4955
$context = $this->createContext();
5056

@@ -81,10 +87,15 @@ public function testShouldDoesReconnectIfConnectionFailed()
8187
->expects($this->once())
8288
->method('close')
8389
;
84-
$connection
85-
->expects($this->once())
86-
->method('getServerVersion')
87-
;
90+
if (method_exists($connection, 'connect')) {
91+
// DBAL < 4
92+
$connection->expects($this->once())
93+
->method('connect');
94+
} else {
95+
// DBAL >= 4, calls connect() internally
96+
$connection->expects($this->once())
97+
->method('getServerVersion');
98+
}
8899

89100
$context = $this->createContext();
90101

0 commit comments

Comments
 (0)