Skip to content

Commit

Permalink
Inject EventManager when using a custom platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Petar Obradović committed Mar 11, 2022
1 parent 82331b8 commit f41b9cb
Show file tree
Hide file tree
Showing 2 changed files with 23 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
14 changes: 14 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ public function testConnectDispatchEvent(): void
$conn->connect();
}

public function testEventDispatcherIsSetForCustomPlatform(): void
{
$eventManager = new EventManager();

$driverMock = $this->createMock(Driver::class);

$platformMock = $this->createMock(AbstractPlatform::class);
$platformMock->expects(self::once())
->method('setEventManager')
->with($eventManager);

new Connection(['platform' => $platformMock], $driverMock, new Configuration(), $eventManager);
}

public function testTransactionBeginDispatchEvent(): void
{
$eventManager = new EventManager();
Expand Down

0 comments on commit f41b9cb

Please sign in to comment.