Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf07627

Browse files
committed
minor #39214 [Messenger] Test generated SQL (derrabus)
This PR was merged into the 4.4 branch. Discussion ---------- [Messenger] Test generated SQL | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A Follow-up to #39166. This is an attempt to the SQL generated by the `Symfony\Component\Messenger\Transport\Doctrine\Connection::get()` method. The challange was to create a locking SELECT statement, taking into account the different ways to create a lock in different SQL dialects. This test verifies if the correct lock statements are generated for MySQL and SQL Server. Commits ------- 1f1b62a [Messenger] Test generated SQL
2 parents fd3c60b + 1f1b62a commit cf07627

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
namespace Symfony\Component\Messenger\Tests\Transport\Doctrine;
1313

1414
use Doctrine\DBAL\Abstraction\Result as AbstractionResult;
15+
use Doctrine\DBAL\Connection as DBALConnection;
1516
use Doctrine\DBAL\DBALException;
17+
use Doctrine\DBAL\Driver\Result as DriverResult;
18+
use Doctrine\DBAL\Driver\ResultStatement;
1619
use Doctrine\DBAL\Exception;
1720
use Doctrine\DBAL\Platforms\AbstractPlatform;
21+
use Doctrine\DBAL\Platforms\MySQL57Platform;
22+
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
1823
use Doctrine\DBAL\Query\QueryBuilder;
1924
use Doctrine\DBAL\Result;
2025
use Doctrine\DBAL\Schema\AbstractSchemaManager;
@@ -117,7 +122,7 @@ public function testItThrowsATransportExceptionIfItCannotRejectMessage()
117122

118123
private function getDBALConnectionMock()
119124
{
120-
$driverConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
125+
$driverConnection = $this->createMock(DBALConnection::class);
121126
$platform = $this->createMock(AbstractPlatform::class);
122127
$platform->method('getWriteLockSQL')->willReturn('FOR UPDATE');
123128
$configuration = $this->createMock(\Doctrine\DBAL\Configuration::class);
@@ -345,4 +350,53 @@ public function testFindAll()
345350
$this->assertEquals('{"message":"Hi again"}', $doctrineEnvelopes[1]['body']);
346351
$this->assertEquals(['type' => DummyMessage::class], $doctrineEnvelopes[1]['headers']);
347352
}
353+
354+
/**
355+
* @dataProvider providePlatformSql
356+
*/
357+
public function testGeneratedSql(AbstractPlatform $platform, string $expectedSql)
358+
{
359+
$driverConnection = $this->createMock(DBALConnection::class);
360+
$driverConnection->method('getDatabasePlatform')->willReturn($platform);
361+
$driverConnection->method('createQueryBuilder')->willReturnCallback(function () use ($driverConnection) {
362+
return new QueryBuilder($driverConnection);
363+
});
364+
365+
if (interface_exists(DriverResult::class)) {
366+
$result = $this->createMock(DriverResult::class);
367+
$result->method('fetchAssociative')->willReturn(false);
368+
369+
if (class_exists(Result::class)) {
370+
$result = new Result($result, $driverConnection);
371+
}
372+
} else {
373+
$result = $this->createMock(ResultStatement::class);
374+
$result->method('fetch')->willReturn(false);
375+
}
376+
377+
$driverConnection->expects($this->once())->method('beginTransaction');
378+
$driverConnection
379+
->expects($this->once())
380+
->method('executeQuery')
381+
->with($expectedSql)
382+
->willReturn($result)
383+
;
384+
$driverConnection->expects($this->once())->method('commit');
385+
386+
$connection = new Connection([], $driverConnection);
387+
$connection->get();
388+
}
389+
390+
public function providePlatformSql(): iterable
391+
{
392+
yield 'MySQL' => [
393+
new MySQL57Platform(),
394+
'SELECT m.* FROM messenger_messages m WHERE (m.delivered_at is null OR m.delivered_at < ?) AND (m.available_at <= ?) AND (m.queue_name = ?) ORDER BY available_at ASC LIMIT 1 FOR UPDATE',
395+
];
396+
397+
yield 'SQL Server' => [
398+
new SQLServer2012Platform(),
399+
'SELECT m.* FROM messenger_messages m WITH (UPDLOCK, ROWLOCK) WHERE (m.delivered_at is null OR m.delivered_at < ?) AND (m.available_at <= ?) AND (m.queue_name = ?) ORDER BY available_at ASC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY ',
400+
];
401+
}
348402
}

0 commit comments

Comments
 (0)