Skip to content

Commit

Permalink
Moved subscribing to events away from DbPlatform constructor (#5)
Browse files Browse the repository at this point in the history
* Added addEventSubscribers(EventManager) method to DbPlatform interface

* Implemented addEventSubscribers on existing DbPlatform implementations

* Aligned tests with changes
  • Loading branch information
alongosz authored Apr 18, 2019
1 parent e3f98f8 commit a48e8de
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/lib/Database/DbPlatform/DbPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace EzSystems\DoctrineSchema\Database\DbPlatform;

use Doctrine\Common\EventManager;

interface DbPlatform
{
/**
Expand All @@ -21,4 +23,11 @@ interface DbPlatform
* @return string
*/
public function getDriverName(): string;

/**
* Add event subscribers predefined and required by an implementation.
*
* @param \Doctrine\Common\EventManager $eventManager
*/
public function addEventSubscribers(EventManager $eventManager): void;
}
8 changes: 8 additions & 0 deletions src/lib/Database/DbPlatform/PostgreSqlDbPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace EzSystems\DoctrineSchema\Database\DbPlatform;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Event\SchemaDropTableEventArgs;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
Expand All @@ -16,6 +17,13 @@

class PostgreSqlDbPlatform extends PostgreSQL100Platform implements DbPlatform
{
/**
* {@inheritdoc}
*/
public function addEventSubscribers(EventManager $eventManager): void
{
}

/**
* {@inheritdoc}
*/
Expand Down
7 changes: 4 additions & 3 deletions src/lib/Database/DbPlatform/SqliteDbPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

class SqliteDbPlatform extends SqlitePlatform implements DbPlatform
{
public function __construct(EventManager $eventManager)
/**
* {@inheritdoc}
*/
public function addEventSubscribers(EventManager $eventManager): void
{
parent::__construct();

$eventManager->addEventSubscriber(new SQLSessionInit('PRAGMA FOREIGN_KEYS = ON'));
}

Expand Down
4 changes: 3 additions & 1 deletion tests/lib/Database/Builder/SqliteTestDatabaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ class SqliteTestDatabaseBuilder implements TestDatabaseBuilder
*/
public function buildDatabase(): Connection
{
$dbPlatform = new SqliteDbPlatform();
$eventManager = new EventManager();
$dbPlatform->addEventSubscribers($eventManager);

return DriverManager::getConnection(
[
'url' => 'sqlite:///:memory:',
'platform' => new SqliteDbPlatform($eventManager),
'platform' => $dbPlatform,
],
new Configuration(),
$eventManager
Expand Down
3 changes: 1 addition & 2 deletions tests/lib/Database/DbPlatform/SqliteDbPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace EzSystems\Tests\DoctrineSchema\Database\DbPlatform;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\ParameterType;
use EzSystems\DoctrineSchema\Database\DbPlatform\SqliteDbPlatform;
Expand All @@ -29,7 +28,7 @@ class SqliteDbPlatformTest extends TestCase

public function setUp(): void
{
$this->sqliteDbPlatform = new SqliteDbPlatform(new EventManager());
$this->sqliteDbPlatform = new SqliteDbPlatform();
$this->testDatabaseFactory = new TestDatabaseFactory();
}

Expand Down
3 changes: 1 addition & 2 deletions tests/lib/Exporter/SchemaExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace EzSystems\Tests\DoctrineSchema\Exporter;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
Expand Down Expand Up @@ -50,7 +49,7 @@ public function providerForTestExport(): array
{
$data = [];

$databasePlatforms = [new SqliteDbPlatform(new EventManager()), new MySqlPlatform()];
$databasePlatforms = [new SqliteDbPlatform(), new MySqlPlatform()];

// iterate over output files to avoid loading it for each platform
$directoryIterator = new \DirectoryIterator(__DIR__ . '/_fixtures/output');
Expand Down

0 comments on commit a48e8de

Please sign in to comment.