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

Commitd90cdf0

Browse files
committed
[DoctrineBridge] Allow doctrine_ping_connection to ping all connections
1 parente880e53 commitd90cdf0

File tree

2 files changed

+152
-19
lines changed

2 files changed

+152
-19
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,35 @@ 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+
foreach ($this->getTargetEntityManagers($entityManager)as$name =>$targetEntityManager) {
32+
$this->pingConnection($targetEntityManager,$name);
33+
}
3234
}
3335

3436
return$stack->next()->handle($envelope,$stack);
3537
}
3638

37-
privatefunctionpingConnection(EntityManagerInterface$entityManager):void
39+
/**
40+
* @return iterable<string|null, EntityManagerInterface>
41+
*/
42+
privatefunctiongetTargetEntityManagers(EntityManagerInterface$entityManager):iterable
43+
{
44+
if (null !==$this->entityManagerName) {
45+
yield$this->entityManagerName =>$entityManager;
46+
47+
return;
48+
}
49+
50+
foreach ($this->managerRegistry->getManagerNames()as$name =>$serviceId) {
51+
$manager =$this->managerRegistry->getManager($name);
52+
53+
if ($managerinstanceof EntityManagerInterface) {
54+
yield$name =>$manager;
55+
}
56+
}
57+
}
58+
59+
privatefunctionpingConnection(EntityManagerInterface$entityManager, ?string$entityManagerName =null):void
3860
{
3961
$connection =$entityManager->getConnection();
4062

@@ -47,7 +69,7 @@ private function pingConnection(EntityManagerInterface $entityManager): void
4769
}
4870

4971
if (!$entityManager->isOpen()) {
50-
$this->managerRegistry->resetManager($this->entityManagerName);
72+
$this->managerRegistry->resetManager($entityManagerName ??$this->entityManagerName);
5173
}
5274
}
5375

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

Lines changed: 127 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
useDoctrine\DBAL\Result;
1818
useDoctrine\ORM\EntityManagerInterface;
1919
useDoctrine\Persistence\ManagerRegistry;
20-
usePHPUnit\Framework\MockObject\MockObject;
2120
useSymfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware;
2221
useSymfony\Component\Messenger\Envelope;
2322
useSymfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
@@ -26,20 +25,20 @@
2625

2726
class DoctrinePingConnectionMiddlewareTestextends MiddlewareTestCase
2827
{
29-
privateConnection&MockObject$connection;
30-
privateEntityManagerInterface&MockObject$entityManager;
31-
privateManagerRegistry&MockObject$managerRegistry;
28+
privateConnection$connection;
29+
privateEntityManagerInterface$entityManager;
30+
privateManagerRegistry$managerRegistry;
3231
privateDoctrinePingConnectionMiddleware$middleware;
3332
privatestring$entityManagerName ='default';
3433

3534
protectedfunctionsetUp():void
3635
{
37-
$this->connection =$this->createMock(Connection::class);
36+
$this->connection =$this->createStub(Connection::class);
3837

39-
$this->entityManager =$this->createMock(EntityManagerInterface::class);
38+
$this->entityManager =$this->createStub(EntityManagerInterface::class);
4039
$this->entityManager->method('getConnection')->willReturn($this->connection);
4140

42-
$this->managerRegistry =$this->createMock(ManagerRegistry::class);
41+
$this->managerRegistry =$this->createStub(ManagerRegistry::class);
4342
$this->managerRegistry->method('getManager')->willReturn($this->entityManager);
4443

4544
$this->middleware =newDoctrinePingConnectionMiddleware(
@@ -50,6 +49,14 @@ protected function setUp(): void
5049

5150
publicfunctiontestMiddlewarePingOk()
5251
{
52+
$this->connection =$this->createMock(Connection::class);
53+
$this->entityManager =$this->createStub(EntityManagerInterface::class);
54+
$this->entityManager->method('getConnection')->willReturn($this->connection);
55+
$this->managerRegistry =$this->createStub(ManagerRegistry::class);
56+
$this->managerRegistry->method('getManager')->willReturn($this->entityManager);
57+
58+
$middleware =newDoctrinePingConnectionMiddleware($this->managerRegistry,$this->entityManagerName);
59+
5360
$this->connection->method('getDatabasePlatform')
5461
->willReturn($this->mockPlatform());
5562

@@ -59,10 +66,10 @@ public function testMiddlewarePingOk()
5966
static$counter =0;
6067

6168
if (1 === ++$counter) {
62-
throw$this->createMock(DBALException::class);
69+
throw$this->createStub(DBALException::class);
6370
}
6471

65-
return$this->createMock(Result::class);
72+
return$this->createStub(Result::class);
6673
});
6774

6875
$this->connection->expects($this->once())
@@ -72,13 +79,25 @@ public function testMiddlewarePingOk()
7279
$envelope =newEnvelope(new \stdClass(), [
7380
newConsumedByWorkerStamp(),
7481
]);
75-
$this->middleware->handle($envelope,$this->getStackMock());
82+
$middleware->handle($envelope,$this->getStackMock());
7683
}
7784

7885
publicfunctiontestMiddlewarePingResetEntityManager()
7986
{
87+
$this->connection =$this->createStub(Connection::class);
88+
$this->entityManager =$this->createMock(EntityManagerInterface::class);
89+
$this->entityManager->method('getConnection')->willReturn($this->connection);
90+
$this->managerRegistry =$this->createMock(ManagerRegistry::class);
91+
$this->managerRegistry->method('getManager')->willReturn($this->entityManager);
92+
93+
$middleware =newDoctrinePingConnectionMiddleware(
94+
$this->managerRegistry,
95+
$this->entityManagerName
96+
);
97+
8098
$this->connection->method('getDatabasePlatform')
8199
->willReturn($this->mockPlatform());
100+
$this->connection->method('executeQuery')->willReturn($this->createStub(Result::class));
82101

83102
$this->entityManager->expects($this->once())
84103
->method('isOpen')
@@ -92,15 +111,14 @@ public function testMiddlewarePingResetEntityManager()
92111
$envelope =newEnvelope(new \stdClass(), [
93112
newConsumedByWorkerStamp(),
94113
]);
95-
$this->middleware->handle($envelope,$this->getStackMock());
114+
$middleware->handle($envelope,$this->getStackMock());
96115
}
97116

98117
publicfunctiontestInvalidEntityManagerThrowsException()
99118
{
100-
$managerRegistry =$this->createMock(ManagerRegistry::class);
119+
$managerRegistry =$this->createStub(ManagerRegistry::class);
101120
$managerRegistry
102121
->method('getManager')
103-
->with('unknown_manager')
104122
->willThrowException(new \InvalidArgumentException());
105123

106124
$middleware =newDoctrinePingConnectionMiddleware($managerRegistry,'unknown_manager');
@@ -112,19 +130,112 @@ public function testInvalidEntityManagerThrowsException()
112130

113131
publicfunctiontestMiddlewareNoPingInNonWorkerContext()
114132
{
133+
$this->connection =$this->createMock(Connection::class);
134+
$this->entityManager =$this->createStub(EntityManagerInterface::class);
135+
$this->entityManager->method('getConnection')->willReturn($this->connection);
136+
$this->managerRegistry =$this->createStub(ManagerRegistry::class);
137+
$this->managerRegistry->method('getManager')->willReturn($this->entityManager);
138+
139+
$middleware =newDoctrinePingConnectionMiddleware(
140+
$this->managerRegistry,
141+
$this->entityManagerName
142+
);
143+
115144
$this->connection->expects($this->never())
116145
->method('close')
117146
;
118147

119148
$envelope =newEnvelope(new \stdClass());
120-
$this->middleware->handle($envelope,$this->getStackMock());
149+
$middleware->handle($envelope,$this->getStackMock());
150+
}
151+
152+
publicfunctiontestMiddlewarePingsAllConnectionsWhenEntityManagerNameIsNull()
153+
{
154+
$firstConnection =$this->connectionExpectingOnePing();
155+
$secondConnection =$this->connectionExpectingOnePing();
156+
157+
$registry =$this->createRegistryForManagers([
158+
'first' =>$this->createManagerWithConnection($firstConnection),
159+
'second' =>$this->createManagerWithConnection($secondConnection),
160+
]);
161+
162+
$middleware =newDoctrinePingConnectionMiddleware($registry);
163+
164+
$envelope =newEnvelope(new \stdClass(), [
165+
newConsumedByWorkerStamp(),
166+
]);
167+
$middleware->handle($envelope,$this->getStackMock());
168+
}
169+
170+
publicfunctiontestMiddlewareResetsClosedManagersWhenEntityManagerNameIsNull()
171+
{
172+
$registry =$this->createRegistryForManagers([
173+
'open' =>$this->createManagerWithConnection($this->connectionExpectingOnePing(),true),
174+
'closed' =>$this->createManagerWithConnection($this->connectionExpectingOnePing(),false),
175+
],true);
176+
$registry->expects($this->once())
177+
->method('resetManager')
178+
->with('closed')
179+
;
180+
181+
$middleware =newDoctrinePingConnectionMiddleware($registry);
182+
183+
$envelope =newEnvelope(new \stdClass(), [
184+
newConsumedByWorkerStamp(),
185+
]);
186+
$middleware->handle($envelope,$this->getStackMock());
121187
}
122188

123-
privatefunctionmockPlatform():AbstractPlatform&MockObject
189+
privatefunctionmockPlatform():AbstractPlatform
124190
{
125-
$platform =$this->createMock(AbstractPlatform::class);
191+
$platform =$this->createStub(AbstractPlatform::class);
126192
$platform->method('getDummySelectSQL')->willReturn('SELECT 1');
127193

128194
return$platform;
129195
}
196+
197+
privatefunctionconnectionExpectingOnePing():Connection
198+
{
199+
$connection =$this->createMock(Connection::class);
200+
$connection->method('getDatabasePlatform')->willReturn($this->mockPlatform());
201+
$connection->expects($this->once())->method('executeQuery');
202+
203+
return$connection;
204+
}
205+
206+
privatefunctioncreateManagerWithConnection(Connection$connection, ?bool$isOpen =null):EntityManagerInterface
207+
{
208+
$manager =null ===$isOpen ?$this->createStub(EntityManagerInterface::class) :$this->createMock(EntityManagerInterface::class);
209+
$manager->method('getConnection')->willReturn($connection);
210+
211+
if (null !==$isOpen) {
212+
$manager->expects($this->once())->method('isOpen')->willReturn($isOpen);
213+
}
214+
215+
return$manager;
216+
}
217+
218+
/**
219+
* @param array<string, EntityManagerInterface> $managers
220+
*/
221+
privatefunctioncreateRegistryForManagers(array$managers,bool$withExpectations =false):ManagerRegistry
222+
{
223+
$defaultName =array_key_first($managers);
224+
225+
$registry =$withExpectations ?$this->createMock(ManagerRegistry::class) :$this->createStub(ManagerRegistry::class);
226+
227+
if ($withExpectations) {
228+
$registry->expects($this->any())->method('getManagerNames')->willReturn(array_combine(array_keys($managers),array_keys($managers)));
229+
$registry->expects($this->any())->method('getManager')->willReturnCallback(staticfunction (?string$name)use ($managers,$defaultName): ?EntityManagerInterface {
230+
return$managers[$name ??$defaultName] ??null;
231+
});
232+
}else {
233+
$registry->method('getManagerNames')->willReturn(array_combine(array_keys($managers),array_keys($managers)));
234+
$registry->method('getManager')->willReturnCallback(staticfunction (?string$name)use ($managers,$defaultName): ?EntityManagerInterface {
235+
return$managers[$name ??$defaultName] ??null;
236+
});
237+
}
238+
239+
return$registry;
240+
}
130241
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp