From a48e8de380c428fbcd20021e75c3690b00e0e22a Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Thu, 18 Apr 2019 16:17:39 +0200 Subject: [PATCH] Moved subscribing to events away from DbPlatform constructor (#5) * Added addEventSubscribers(EventManager) method to DbPlatform interface * Implemented addEventSubscribers on existing DbPlatform implementations * Aligned tests with changes --- src/lib/Database/DbPlatform/DbPlatform.php | 9 +++++++++ src/lib/Database/DbPlatform/PostgreSqlDbPlatform.php | 8 ++++++++ src/lib/Database/DbPlatform/SqliteDbPlatform.php | 7 ++++--- tests/lib/Database/Builder/SqliteTestDatabaseBuilder.php | 4 +++- tests/lib/Database/DbPlatform/SqliteDbPlatformTest.php | 3 +-- tests/lib/Exporter/SchemaExporterTest.php | 3 +-- 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/lib/Database/DbPlatform/DbPlatform.php b/src/lib/Database/DbPlatform/DbPlatform.php index 9f2304f..7c75e3f 100644 --- a/src/lib/Database/DbPlatform/DbPlatform.php +++ b/src/lib/Database/DbPlatform/DbPlatform.php @@ -8,6 +8,8 @@ namespace EzSystems\DoctrineSchema\Database\DbPlatform; +use Doctrine\Common\EventManager; + interface DbPlatform { /** @@ -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; } diff --git a/src/lib/Database/DbPlatform/PostgreSqlDbPlatform.php b/src/lib/Database/DbPlatform/PostgreSqlDbPlatform.php index fcfa32f..fcf64a8 100644 --- a/src/lib/Database/DbPlatform/PostgreSqlDbPlatform.php +++ b/src/lib/Database/DbPlatform/PostgreSqlDbPlatform.php @@ -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; @@ -16,6 +17,13 @@ class PostgreSqlDbPlatform extends PostgreSQL100Platform implements DbPlatform { + /** + * {@inheritdoc} + */ + public function addEventSubscribers(EventManager $eventManager): void + { + } + /** * {@inheritdoc} */ diff --git a/src/lib/Database/DbPlatform/SqliteDbPlatform.php b/src/lib/Database/DbPlatform/SqliteDbPlatform.php index fef1707..c81543b 100644 --- a/src/lib/Database/DbPlatform/SqliteDbPlatform.php +++ b/src/lib/Database/DbPlatform/SqliteDbPlatform.php @@ -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')); } diff --git a/tests/lib/Database/Builder/SqliteTestDatabaseBuilder.php b/tests/lib/Database/Builder/SqliteTestDatabaseBuilder.php index 7b17a42..386d5a6 100644 --- a/tests/lib/Database/Builder/SqliteTestDatabaseBuilder.php +++ b/tests/lib/Database/Builder/SqliteTestDatabaseBuilder.php @@ -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 diff --git a/tests/lib/Database/DbPlatform/SqliteDbPlatformTest.php b/tests/lib/Database/DbPlatform/SqliteDbPlatformTest.php index 4639521..41e177d 100644 --- a/tests/lib/Database/DbPlatform/SqliteDbPlatformTest.php +++ b/tests/lib/Database/DbPlatform/SqliteDbPlatformTest.php @@ -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; @@ -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(); } diff --git a/tests/lib/Exporter/SchemaExporterTest.php b/tests/lib/Exporter/SchemaExporterTest.php index e6ba438..e356f04 100644 --- a/tests/lib/Exporter/SchemaExporterTest.php +++ b/tests/lib/Exporter/SchemaExporterTest.php @@ -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; @@ -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');