Skip to content

Commit

Permalink
bug #32641 [Messenger] Retrieve table default options from the Schema…
Browse files Browse the repository at this point in the history
…Manager (vincenttouzet)

This PR was squashed before being merged into the 4.3 branch (closes #32641).

Discussion
----------

[Messenger] Retrieve table default options from the SchemaManager

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #32321    <!-- #-prefixed issue number(s), if any -->
| License       | MIT

This PR modify the Connection of the Doctrine Transport. It is now possible to specify a `SchemaConfig` that will be used when generating tables.

If Doctrine is configured as the following :
```yml
# config/packages/doctrine.yaml
doctrine:
    dbal:
        default_table_options:
            charset: 'utf8mb4'
            collate: 'utf8mb4_unicode_ci'
```

Then those options are used to create the table.

ping @weaverryan 😉

Commits
-------

93d0dc8 [Messenger] Retrieve table default options from the SchemaManager
  • Loading branch information
Tobion committed Jul 28, 2019
2 parents a3cfa36 + 93d0dc8 commit 6003608
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\SchemaConfig;
use Doctrine\DBAL\Schema\Synchronizer\SchemaSynchronizer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
Expand Down Expand Up @@ -102,25 +104,26 @@ public function testItThrowsATransportExceptionIfItCannotRejectMessage()

private function getDBALConnectionMock()
{
$driverConnection = $this->getMockBuilder(\Doctrine\DBAL\Connection::class)
->disableOriginalConstructor()
->getMock();
$platform = $this->getMockBuilder(AbstractPlatform::class)
->getMock();
$driverConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
$platform = $this->createMock(AbstractPlatform::class);
$platform->method('getWriteLockSQL')->willReturn('FOR UPDATE');
$configuration = $this->getMockBuilder(\Doctrine\DBAL\Configuration::class)
->getMock();
$configuration = $this->createMock(\Doctrine\DBAL\Configuration::class);
$driverConnection->method('getDatabasePlatform')->willReturn($platform);
$driverConnection->method('getConfiguration')->willReturn($configuration);

$schemaManager = $this->createMock(AbstractSchemaManager::class);
$schemaConfig = $this->createMock(SchemaConfig::class);
$schemaConfig->method('getMaxIdentifierLength')->willReturn(63);
$schemaConfig->method('getDefaultTableOptions')->willReturn([]);
$schemaManager->method('createSchemaConfig')->willReturn($schemaConfig);
$driverConnection->method('getSchemaManager')->willReturn($schemaManager);

return $driverConnection;
}

private function getQueryBuilderMock()
{
$queryBuilder = $this->getMockBuilder(QueryBuilder::class)
->disableOriginalConstructor()
->getMock();
$queryBuilder = $this->createMock(QueryBuilder::class);

$queryBuilder->method('select')->willReturn($queryBuilder);
$queryBuilder->method('update')->willReturn($queryBuilder);
Expand All @@ -138,9 +141,7 @@ private function getQueryBuilderMock()

private function getStatementMock($expectedResult)
{
$stmt = $this->getMockBuilder(Statement::class)
->disableOriginalConstructor()
->getMock();
$stmt = $this->createMock(Statement::class);
$stmt->expects($this->once())
->method('fetch')
->willReturn($expectedResult);
Expand All @@ -150,8 +151,7 @@ private function getStatementMock($expectedResult)

private function getSchemaSynchronizerMock()
{
return $this->getMockBuilder(SchemaSynchronizer::class)
->getMock();
return $this->createMock(SchemaSynchronizer::class);
}

/**
Expand Down Expand Up @@ -307,9 +307,7 @@ public function testFindAll()
'headers' => json_encode(['type' => DummyMessage::class]),
];

$stmt = $this->getMockBuilder(Statement::class)
->disableOriginalConstructor()
->getMock();
$stmt = $this->createMock(Statement::class);
$stmt->expects($this->once())
->method('fetchAll')
->willReturn([$message1, $message2]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testItReturnsTheDecodedMessageToTheHandler()
$serializer = $this->createSerializer();

$doctrineEnvelope = $this->createDoctrineEnvelope();
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
$connection = $this->createMock(Connection::class);
$connection->method('get')->willReturn($doctrineEnvelope);

$receiver = new DoctrineReceiver($connection, $serializer);
Expand Down Expand Up @@ -62,7 +62,7 @@ public function testItRejectTheMessageIfThereIsAMessageDecodingFailedException()
$serializer->method('decode')->willThrowException(new MessageDecodingFailedException());

$doctrineEnvelop = $this->createDoctrineEnvelope();
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
$connection = $this->createMock(Connection::class);
$connection->method('get')->willReturn($doctrineEnvelop);
$connection->expects($this->once())->method('reject');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ public function testSend()
$envelope = new Envelope(new DummyMessage('Oy'));
$encoded = ['body' => '...', 'headers' => ['type' => DummyMessage::class]];

$connection = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->getMock();
$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('send')->with($encoded['body'], $encoded['headers'])->willReturn(15);

$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
$serializer = $this->createMock(SerializerInterface::class);
$serializer->method('encode')->with($envelope)->willReturnOnConsecutiveCalls($encoded);

$sender = new DoctrineSender($connection, $serializer);
Expand All @@ -49,12 +47,10 @@ public function testSendWithDelay()
$envelope = (new Envelope(new DummyMessage('Oy')))->with(new DelayStamp(500));
$encoded = ['body' => '...', 'headers' => ['type' => DummyMessage::class]];

$connection = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->getMock();
$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('send')->with($encoded['body'], $encoded['headers'], 500);

$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
$serializer = $this->createMock(SerializerInterface::class);
$serializer->method('encode')->with($envelope)->willReturnOnConsecutiveCalls($encoded);

$sender = new DoctrineSender($connection, $serializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Messenger\Tests\Transport\Doctrine;

use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\SchemaConfig;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Messenger\Transport\Doctrine\Connection;
Expand All @@ -23,7 +25,7 @@ class DoctrineTransportFactoryTest extends TestCase
public function testSupports()
{
$factory = new DoctrineTransportFactory(
$this->getMockBuilder(RegistryInterface::class)->getMock()
$this->createMock(RegistryInterface::class)
);

$this->assertTrue($factory->supports('doctrine://default', []));
Expand All @@ -32,19 +34,21 @@ public function testSupports()

public function testCreateTransport()
{
$connection = $this->getMockBuilder(\Doctrine\DBAL\Connection::class)
->disableOriginalConstructor()
->getMock();
$registry = $this->getMockBuilder(RegistryInterface::class)->getMock();
$driverConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
$schemaManager = $this->createMock(AbstractSchemaManager::class);
$schemaConfig = $this->createMock(SchemaConfig::class);
$schemaManager->method('createSchemaConfig')->willReturn($schemaConfig);
$driverConnection->method('getSchemaManager')->willReturn($schemaManager);
$registry = $this->createMock(RegistryInterface::class);
$registry->expects($this->once())
->method('getConnection')
->willReturn($connection);
->willReturn($driverConnection);

$factory = new DoctrineTransportFactory($registry);
$serializer = $this->createMock(SerializerInterface::class);

$this->assertEquals(
new DoctrineTransport(new Connection(Connection::buildConfiguration('doctrine://default'), $connection), $serializer),
new DoctrineTransport(new Connection(Connection::buildConfiguration('doctrine://default'), $driverConnection), $serializer),
$factory->createTransport('doctrine://default', [], $serializer)
);
}
Expand All @@ -55,7 +59,7 @@ public function testCreateTransport()
*/
public function testCreateTransportMustThrowAnExceptionIfManagerIsNotFound()
{
$registry = $this->getMockBuilder(RegistryInterface::class)->getMock();
$registry = $this->createMock(RegistryInterface::class);
$registry->expects($this->once())
->method('getConnection')
->willReturnCallback(function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function testItIsATransport()
public function testReceivesMessages()
{
$transport = $this->getTransport(
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock(),
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock()
$serializer = $this->createMock(SerializerInterface::class),
$connection = $this->createMock(Connection::class)
);

$decodedMessage = new DummyMessage('Decoded.');
Expand All @@ -52,8 +52,8 @@ public function testReceivesMessages()

private function getTransport(SerializerInterface $serializer = null, Connection $connection = null)
{
$serializer = $serializer ?: $this->getMockBuilder(SerializerInterface::class)->getMock();
$connection = $connection ?: $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
$serializer = $serializer ?: $this->createMock(SerializerInterface::class);
$connection = $connection ?: $this->createMock(Connection::class);

return new DoctrineTransport($connection, $serializer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private function executeQuery(string $sql, array $parameters = [])

private function getSchema(): Schema
{
$schema = new Schema();
$schema = new Schema([], [], $this->driverConnection->getSchemaManager()->createSchemaConfig());
$table = $schema->createTable($this->configuration['table_name']);
$table->addColumn('id', Type::BIGINT)
->setAutoincrement(true)
Expand Down

0 comments on commit 6003608

Please sign in to comment.