Skip to content

Commit 874d652

Browse files
committed
ping is deprecated - use DummySelectSQL from platform and execute them!
1 parent 579240b commit 874d652

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

Consumption/Extension/DoctrinePingConnectionExtension.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Enqueue\Bundle\Consumption\Extension;
44

55
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\Exception\ConnectionLost;
67
use Doctrine\Persistence\ManagerRegistry;
78
use Enqueue\Consumption\Context\MessageReceived;
89
use Enqueue\Consumption\MessageReceivedExtensionInterface;
10+
use ErrorException;
911

1012
class DoctrinePingConnectionExtension implements MessageReceivedExtensionInterface
1113
{
@@ -27,7 +29,7 @@ public function onMessageReceived(MessageReceived $context): void
2729
continue;
2830
}
2931

30-
if ($connection->ping()) {
32+
if ($this->ping($connection)) {
3133
continue;
3234
}
3335

@@ -43,4 +45,23 @@ public function onMessageReceived(MessageReceived $context): void
4345
);
4446
}
4547
}
48+
49+
private function ping(Connection $connection): bool
50+
{
51+
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
52+
throw new ErrorException($message, $severity, $severity, $file, $line);
53+
});
54+
55+
try {
56+
$dummySelectSQL = $connection->getDatabasePlatform()->getDummySelectSQL();
57+
58+
$connection->executeQuery($dummySelectSQL);
59+
60+
return true;
61+
} catch (ErrorException | ConnectionLost $exception) {
62+
return false;
63+
} finally {
64+
restore_error_handler();
65+
}
66+
}
4667
}

Tests/Unit/Consumption/Extension/DoctrinePingConnectionExtensionTest.php

+22-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Enqueue\Bundle\Tests\Unit\Consumption\Extension;
44

55
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\Driver\DriverException;
7+
use Doctrine\DBAL\Exception\ConnectionLost;
8+
use Doctrine\DBAL\Platforms\AbstractPlatform;
69
use Doctrine\Persistence\ManagerRegistry;
710
use Enqueue\Bundle\Consumption\Extension\DoctrinePingConnectionExtension;
811
use Enqueue\Consumption\Context\MessageReceived;
@@ -29,10 +32,17 @@ public function testShouldNotReconnectIfConnectionIsOK()
2932
->method('isConnected')
3033
->willReturn(true)
3134
;
35+
36+
$abstractPlatform = $this->createMock(AbstractPlatform::class);
37+
$abstractPlatform->expects($this->once())
38+
->method('getDummySelectSQL')
39+
->willReturn('dummy')
40+
;
41+
3242
$connection
3343
->expects($this->once())
34-
->method('ping')
35-
->willReturn(true)
44+
->method('getDatabasePlatform')
45+
->willReturn($abstractPlatform)
3646
;
3747
$connection
3848
->expects($this->never())
@@ -70,8 +80,8 @@ public function testShouldDoesReconnectIfConnectionFailed()
7080
;
7181
$connection
7282
->expects($this->once())
73-
->method('ping')
74-
->willReturn(false)
83+
->method('getDatabasePlatform')
84+
->willThrowException(new ConnectionLost('message', $this->createMock(DriverException::class)))
7585
;
7686
$connection
7787
->expects($this->once())
@@ -128,10 +138,16 @@ public function testShouldSkipIfConnectionWasNotOpened()
128138
->method('isConnected')
129139
->willReturn(true)
130140
;
141+
$abstractPlatform = $this->createMock(AbstractPlatform::class);
142+
$abstractPlatform->expects($this->once())
143+
->method('getDummySelectSQL')
144+
->willReturn('dummy')
145+
;
146+
131147
$connection2
132148
->expects($this->once())
133-
->method('ping')
134-
->willReturn(true)
149+
->method('getDatabasePlatform')
150+
->willReturn($abstractPlatform)
135151
;
136152

137153
$context = $this->createContext();

composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
"source": "https://github.com/php-enqueue/enqueue-dev",
2121
"docs": "https://github.com/php-enqueue/enqueue-dev/blob/master/docs/index.md"
2222
},
23+
"repositories": [
24+
{
25+
"type": "git",
26+
"url": "https://github.com/andrewmy/php-rabbitmq-management-api.git"
27+
}
28+
],
2329
"require-dev": {
2430
"phpunit/phpunit": "^9.5",
2531
"enqueue/stomp": "0.10.x-dev",

0 commit comments

Comments
 (0)