Skip to content

Commit bd2356d

Browse files
committed
bug #31966 [Messenger] Doctrine Connection find and findAll now correctly decode headers (TimoBakx)
This PR was merged into the 4.3 branch. Discussion ---------- [Messenger] Doctrine Connection find and findAll now correctly decode headers | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #31916 | License | MIT | Doc PR | N/A The Doctrine transport `Connection` class methods `find` and `findAll` did not JSON-decode the headers of the envelope after retrieving it from Doctrine. The `get` method, however, did this correctly. I added two tests to verify the results of `find` and `findAll` and then added the JSON-decoding to the `Connection` class. I moved the JSON-decoding to a separate method to avoid duplicate code. Commits ------- 3aec2ac [Messenger] Doctrine Connection find and findAll now correctly decode headers
2 parents 0219834 + 3aec2ac commit bd2356d

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

src/Symfony/Component/Messenger/Tests/Transport/Doctrine/ConnectionTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,90 @@ public function testItThrowsAnExceptionIfAnExtraOptionsInDefinedInDSN()
230230
{
231231
Connection::buildConfiguration('doctrine://default?new_option=woops');
232232
}
233+
234+
public function testFind()
235+
{
236+
$queryBuilder = $this->getQueryBuilderMock();
237+
$driverConnection = $this->getDBALConnectionMock();
238+
$schemaSynchronizer = $this->getSchemaSynchronizerMock();
239+
$id = 1;
240+
$stmt = $this->getStatementMock([
241+
'id' => $id,
242+
'body' => '{"message":"Hi"}',
243+
'headers' => \json_encode(['type' => DummyMessage::class]),
244+
]);
245+
246+
$driverConnection
247+
->method('createQueryBuilder')
248+
->willReturn($queryBuilder);
249+
$queryBuilder
250+
->method('where')
251+
->willReturn($queryBuilder);
252+
$queryBuilder
253+
->method('getSQL')
254+
->willReturn('');
255+
$queryBuilder
256+
->method('getParameters')
257+
->willReturn([]);
258+
$driverConnection
259+
->method('prepare')
260+
->willReturn($stmt);
261+
262+
$connection = new Connection([], $driverConnection, $schemaSynchronizer);
263+
$doctrineEnvelope = $connection->find($id);
264+
$this->assertEquals(1, $doctrineEnvelope['id']);
265+
$this->assertEquals('{"message":"Hi"}', $doctrineEnvelope['body']);
266+
$this->assertEquals(['type' => DummyMessage::class], $doctrineEnvelope['headers']);
267+
}
268+
269+
public function testFindAll()
270+
{
271+
$queryBuilder = $this->getQueryBuilderMock();
272+
$driverConnection = $this->getDBALConnectionMock();
273+
$schemaSynchronizer = $this->getSchemaSynchronizerMock();
274+
$message1 = [
275+
'id' => 1,
276+
'body' => '{"message":"Hi"}',
277+
'headers' => \json_encode(['type' => DummyMessage::class]),
278+
];
279+
$message2 = [
280+
'id' => 2,
281+
'body' => '{"message":"Hi again"}',
282+
'headers' => \json_encode(['type' => DummyMessage::class]),
283+
];
284+
285+
$stmt = $this->getMockBuilder(Statement::class)
286+
->disableOriginalConstructor()
287+
->getMock();
288+
$stmt->expects($this->once())
289+
->method('fetchAll')
290+
->willReturn([$message1, $message2]);
291+
292+
$driverConnection
293+
->method('createQueryBuilder')
294+
->willReturn($queryBuilder);
295+
$queryBuilder
296+
->method('where')
297+
->willReturn($queryBuilder);
298+
$queryBuilder
299+
->method('getSQL')
300+
->willReturn('');
301+
$queryBuilder
302+
->method('getParameters')
303+
->willReturn([]);
304+
$driverConnection
305+
->method('prepare')
306+
->willReturn($stmt);
307+
308+
$connection = new Connection([], $driverConnection, $schemaSynchronizer);
309+
$doctrineEnvelopes = $connection->findAll();
310+
311+
$this->assertEquals(1, $doctrineEnvelopes[0]['id']);
312+
$this->assertEquals('{"message":"Hi"}', $doctrineEnvelopes[0]['body']);
313+
$this->assertEquals(['type' => DummyMessage::class], $doctrineEnvelopes[0]['headers']);
314+
315+
$this->assertEquals(2, $doctrineEnvelopes[1]['id']);
316+
$this->assertEquals('{"message":"Hi again"}', $doctrineEnvelopes[1]['body']);
317+
$this->assertEquals(['type' => DummyMessage::class], $doctrineEnvelopes[1]['headers']);
318+
}
233319
}

src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function get(): ?array
155155
return null;
156156
}
157157

158-
$doctrineEnvelope['headers'] = json_decode($doctrineEnvelope['headers'], true);
158+
$doctrineEnvelope = $this->decodeEnvelopeHeaders($doctrineEnvelope);
159159

160160
$queryBuilder = $this->driverConnection->createQueryBuilder()
161161
->update($this->configuration['table_name'])
@@ -238,7 +238,11 @@ public function findAll(int $limit = null): array
238238
$queryBuilder->setMaxResults($limit);
239239
}
240240

241-
return $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters())->fetchAll();
241+
$data = $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters())->fetchAll();
242+
243+
return \array_map(function ($doctrineEnvelope) {
244+
return $this->decodeEnvelopeHeaders($doctrineEnvelope);
245+
}, $data);
242246
}
243247

244248
public function find($id): ?array
@@ -254,7 +258,7 @@ public function find($id): ?array
254258
'id' => $id,
255259
])->fetch();
256260

257-
return false === $data ? null : $data;
261+
return false === $data ? null : $this->decodeEnvelopeHeaders($data);
258262
}
259263

260264
private function createAvailableMessagesQueryBuilder(): QueryBuilder
@@ -332,4 +336,11 @@ public static function formatDateTime(\DateTimeInterface $dateTime)
332336
{
333337
return $dateTime->format('Y-m-d\TH:i:s');
334338
}
339+
340+
private function decodeEnvelopeHeaders(array $doctrineEnvelope): array
341+
{
342+
$doctrineEnvelope['headers'] = \json_decode($doctrineEnvelope['headers'], true);
343+
344+
return $doctrineEnvelope;
345+
}
335346
}

0 commit comments

Comments
 (0)