Skip to content

Commit

Permalink
Merge pull request #5320 from spideyfusion/fix-event-manager-for-cust…
Browse files Browse the repository at this point in the history
…om-platform

Inject EventManager when using a custom platform
  • Loading branch information
morozov authored Apr 16, 2022
2 parents 4e474b0 + 735ee90 commit 110ab13
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,6 @@ public function __construct(
$this->_driver = $driver;
$this->params = $params;

if (isset($params['platform'])) {
if (! $params['platform'] instanceof Platforms\AbstractPlatform) {
throw Exception::invalidPlatformType($params['platform']);
}

$this->platform = $params['platform'];
}

// Create default config and event manager if none given
if ($config === null) {
$config = new Configuration();
Expand All @@ -202,6 +194,15 @@ public function __construct(
$this->_config = $config;
$this->_eventManager = $eventManager;

if (isset($params['platform'])) {
if (! $params['platform'] instanceof Platforms\AbstractPlatform) {
throw Exception::invalidPlatformType($params['platform']);
}

$this->platform = $params['platform'];
$this->platform->setEventManager($this->_eventManager);
}

$this->_expr = $this->createExpressionBuilder();

$this->autoCommit = $config->getAutoCommit();
Expand Down
28 changes: 28 additions & 0 deletions tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Tests\Functional\Schema;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
Expand Down Expand Up @@ -378,6 +379,28 @@ public function testListTableIndexesDispatchEvent(): void
$this->schemaManager->listTableIndexes('list_table_indexes_test');
}

public function testDispatchEventWhenDatabasePlatformIsExplicitlyPassed(): void
{
$params = $this->connection->getParams();
$params['platform'] = $this->connection->getDriver()->getDatabasePlatform();

$listenerMock = $this->createMock(CreateTableDispatchEventListener::class);
$listenerMock
->expects(self::once())
->method('onSchemaCreateTable');

$eventManager = new EventManager();
$eventManager->addEventListener([Events::onSchemaCreateTable], $listenerMock);

// We need to work with a new connection because the shared one has an auto-detected platform already set.
$connection = DriverManager::getConnection($params, null, $eventManager);

$table = $this->getTestTable('explicit_db_platform_test');

$schemaManager = $connection->createSchemaManager();
$schemaManager->createTable($table);
}

/**
* @param callable(AbstractSchemaManager):Comparator $comparatorFactory
*
Expand Down Expand Up @@ -1628,3 +1651,8 @@ interface ListTableIndexesDispatchEventListener
{
public function onSchemaIndexDefinition(): void;
}

interface CreateTableDispatchEventListener
{
public function onSchemaCreateTable(): void;
}

0 comments on commit 110ab13

Please sign in to comment.