8000 [Messenger] Test generated SQL by derrabus · Pull Request #39214 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Test generated SQL #39214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Messenger] Test generated SQL
  • Loading branch information
derrabus committed Dec 5, 2020
commit 1f1b62afb64b2850f44a6c5eff93b602919747b4
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
namespace Symfony\Component\Messenger\Tests\Transport\Doctrine;

use Doctrine\DBAL\Abstraction\Result as AbstractionResult;
use Doctrine\DBAL\Connection as DBALConnection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Result as DriverResult;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
Expand Down Expand Up @@ -117,7 +122,7 @@ public function testItThrowsATransportExceptionIfItCannotRejectMessage()

private function getDBALConnectionMock()
{
$driverConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
$driverConnection = $this->createMock(DBALConnection::class);
$platform = $this->createMock(AbstractPlatform::class);
$platform->method('getWriteLockSQL')->willReturn('FOR UPDATE');
$configuration = $this->createMock(\Doctrine\DBAL\Configuration::class);
Expand Down Expand Up @@ -345,4 +350,53 @@ public function testFindAll()
$this->assertEquals('{"message":"Hi again"}', $doctrineEnvelopes[1]['body']);
$this->assertEquals(['type' => DummyMessage::class], $doctrineEnvelopes[1]['headers']);
}

/**
* @dataProvider providePlatformSql
*/
public function testGeneratedSql(AbstractPlatform $platform, string $expectedSql)
{
$driverConnection = $this->createMock(DBALConnection::class);
$driverConnection->method('getDatabasePlatform')->willReturn($platform);
$driverConnection->method('createQueryBuilder')->willReturnCallback(function () use ($driverConnection) {
return new QueryBuilder($driverConnection);
});

8000 if (interface_exists(DriverResult::class)) {
$result = $this->createMock(DriverResult::class);
$result->method('fetchAssociative')->willReturn(false);

if (class_exists(Result::class)) {
$result = new Result($result, $driverConnection);
}
} else {
$result = $this->createMock(ResultStatement::class);
$result->method('fetch')->willReturn(false);
}

$driverConnection->expects($this->once())->method('beginTransaction');
$driverConnection
->expects($this->once())
->method('executeQuery')
->with($expectedSql)
->willReturn($result)
;
$driverConnection->expects($this->once())->method('commit');

$connection = new Connection([], $driverConnection);
$connection->get();
}

public function providePlatformSql(): iterable
{
yield 'MySQL' => [
new MySQL57Platform(),
'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',
];

yield 'SQL Server' => [
new SQLServer2012Platform(),
'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 ',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it really generate white spaces at the end? 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I know where that happens and could apply a trim there, but it felt unnecessary. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes 👍🏻

];
}
}
0