Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb7275d0

Browse files
author
Florian Pondepeyre
committed
[DoctrineBridge] Allow doctrine_ping_connection to ping all connections
1 parente880e53 commitb7275d0

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

‎src/Symfony/Bridge/Doctrine/Messenger/DoctrinePingConnectionMiddleware.php‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,28 @@ class DoctrinePingConnectionMiddleware extends AbstractDoctrineMiddleware
2828
protectedfunctionhandleForManager(EntityManagerInterface$entityManager,Envelope$envelope,StackInterface$stack):Envelope
2929
{
3030
if (null !==$envelope->last(ConsumedByWorkerStamp::class)) {
31-
$this->pingConnection($entityManager);
31+
$entityManagers =null ===$this->entityManagerName ?array_filter(
32+
$this->managerRegistry->getManagers(),
33+
staticfn ($manager) =>$managerinstanceof EntityManagerInterface
34+
) : [$this->entityManagerName =>$entityManager];
35+
36+
$this->pingConnections($entityManagers);
3237
}
3338

3439
return$stack->next()->handle($envelope,$stack);
3540
}
3641

37-
privatefunctionpingConnection(EntityManagerInterface$entityManager):void
42+
/**
43+
* @param array<string|int, EntityManagerInterface> $entityManagers
44+
*/
45+
privatefunctionpingConnections(array$entityManagers):void
46+
{
47+
foreach ($entityManagersas$name =>$entityManager) {
48+
$this->pingConnection($entityManager,\is_string($name) ?$name :null);
49+
}
50+
}
51+
52+
privatefunctionpingConnection(EntityManagerInterface$entityManager, ?string$entityManagerName =null):void
3853
{
3954
$connection =$entityManager->getConnection();
4055

@@ -47,7 +62,7 @@ private function pingConnection(EntityManagerInterface $entityManager): void
4762
}
4863

4964
if (!$entityManager->isOpen()) {
50-
$this->managerRegistry->resetManager($this->entityManagerName);
65+
$this->managerRegistry->resetManager($entityManagerName ??$this->entityManagerName);
5166
}
5267
}
5368

‎src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrinePingConnectionMiddlewareTest.php‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,76 @@ public function testMiddlewareNoPingInNonWorkerContext()
120120
$this->middleware->handle($envelope,$this->getStackMock());
121121
}
122122

123+
publicfunctiontestMiddlewarePingsAllConnectionsWhenEntityManagerNameIsNull()
124+
{
125+
$firstConnection =$this->createMock(Connection::class);
126+
$firstConnection->method('getDatabasePlatform')->willReturn($this->mockPlatform());
127+
$firstConnection->expects($this->once())->method('executeQuery');
128+
129+
$secondConnection =$this->createMock(Connection::class);
130+
$secondConnection->method('getDatabasePlatform')->willReturn($this->mockPlatform());
131+
$secondConnection->expects($this->once())->method('executeQuery');
132+
133+
$firstManager =$this->createMock(EntityManagerInterface::class);
134+
$firstManager->method('getConnection')->willReturn($firstConnection);
135+
136+
$secondManager =$this->createMock(EntityManagerInterface::class);
137+
$secondManager->method('getConnection')->willReturn($secondConnection);
138+
139+
$managerRegistry =$this->createMock(ManagerRegistry::class);
140+
$managerRegistry->method('getManager')->with(null)->willReturn($firstManager);
141+
$managerRegistry->method('getManagers')->willReturn([
142+
'first' =>$firstManager,
143+
'second' =>$secondManager,
144+
]);
145+
146+
$middleware =newDoctrinePingConnectionMiddleware($managerRegistry);
147+
148+
$envelope =newEnvelope(new \stdClass(), [
149+
newConsumedByWorkerStamp(),
150+
]);
151+
$middleware->handle($envelope,$this->getStackMock());
152+
}
153+
154+
publicfunctiontestMiddlewareResetsClosedManagersWhenEntityManagerNameIsNull()
155+
{
156+
$platform =$this->mockPlatform();
157+
158+
$openConnection =$this->createMock(Connection::class);
159+
$openConnection->method('getDatabasePlatform')->willReturn($platform);
160+
$openConnection->expects($this->once())->method('executeQuery');
161+
162+
$closedConnection =$this->createMock(Connection::class);
163+
$closedConnection->method('getDatabasePlatform')->willReturn($platform);
164+
$closedConnection->expects($this->once())->method('executeQuery');
165+
166+
$openManager =$this->createMock(EntityManagerInterface::class);
167+
$openManager->method('getConnection')->willReturn($openConnection);
168+
$openManager->expects($this->once())->method('isOpen')->willReturn(true);
169+
170+
$closedManager =$this->createMock(EntityManagerInterface::class);
171+
$closedManager->method('getConnection')->willReturn($closedConnection);
172+
$closedManager->expects($this->once())->method('isOpen')->willReturn(false);
173+
174+
$managerRegistry =$this->createMock(ManagerRegistry::class);
175+
$managerRegistry->method('getManager')->with(null)->willReturn($openManager);
176+
$managerRegistry->method('getManagers')->willReturn([
177+
'open' =>$openManager,
178+
'closed' =>$closedManager,
179+
]);
180+
$managerRegistry->expects($this->once())
181+
->method('resetManager')
182+
->with('closed')
183+
;
184+
185+
$middleware =newDoctrinePingConnectionMiddleware($managerRegistry);
186+
187+
$envelope =newEnvelope(new \stdClass(), [
188+
newConsumedByWorkerStamp(),
189+
]);
190+
$middleware->handle($envelope,$this->getStackMock());
191+
}
192+
123193
privatefunctionmockPlatform():AbstractPlatform&MockObject
124194
{
125195
$platform =$this->createMock(AbstractPlatform::class);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp