diff --git a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Context/DatabaseContext.php b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Context/DatabaseContext.php index c131bf5f9..946f38f85 100644 --- a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Context/DatabaseContext.php +++ b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Context/DatabaseContext.php @@ -9,13 +9,8 @@ final class DatabaseContext { - private readonly InsertQueryCounter $sqlLogger; - public function __construct(private readonly Connection $connection) { - $this->sqlLogger = new InsertQueryCounter(); - - $this->connection->getConfiguration()->setSQLLogger($this->sqlLogger); } public function connection() : Connection @@ -25,9 +20,7 @@ public function connection() : Connection public function createTable(Table $table) : void { - $schemaManager = $this - ->connection - ->getSchemaManager(); + $schemaManager = $this->connection->createSchemaManager(); if ($schemaManager->tablesExist($table->getName())) { $schemaManager->dropTable($table->getName()); @@ -38,7 +31,7 @@ public function createTable(Table $table) : void public function dropAllTables() : void { - foreach ($this->connection->getSchemaManager()->listTables() as $table) { + foreach ($this->connection->createSchemaManager()->listTables() as $table) { if (\str_contains($table->getName(), 'innodb')) { continue; } @@ -47,15 +40,10 @@ public function dropAllTables() : void continue; } - $this->connection->getSchemaManager()->dropTable($table->getName()); + $this->connection->createSchemaManager()->dropTable($table->getName()); } } - public function numberOfExecutedInsertQueries() : int - { - return $this->sqlLogger->count; - } - public function selectAll(string $tableName) : array { return $this->connection->fetchAllAssociative( diff --git a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Context/InsertQueryCounter.php b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Context/InsertQueryCounter.php deleted file mode 100644 index 838adaf55..000000000 --- a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Context/InsertQueryCounter.php +++ /dev/null @@ -1,23 +0,0 @@ -count++; - } - } - - public function stopQuery() : void - { - } -} diff --git a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Context/ProxyLogger.php b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Context/ProxyLogger.php new file mode 100644 index 000000000..b8a83de77 --- /dev/null +++ b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Context/ProxyLogger.php @@ -0,0 +1,33 @@ +logger = new NullLogger(); + } + + public function log($level, $message, array $context = []) : void + { + if (!isset($context['sql'])) { + return; + } + + if (\str_starts_with(\trim($context['sql']), 'INSERT')) { + $this->count++; + } + } +} diff --git a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/MySqlBulkInsertTest.php b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/MySqlBulkInsertTest.php index 136ee1b3d..23bb37690 100644 --- a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/MySqlBulkInsertTest.php +++ b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/MySqlBulkInsertTest.php @@ -43,7 +43,7 @@ public function test_inserts_multiple_rows_at_once() : void ); $this->assertEquals(3, $this->databaseContext->tableCount($table)); - $this->assertEquals(1, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(1, $this->executedQueriesCount()); $this->assertEquals( [ @@ -94,7 +94,7 @@ public function test_inserts_new_rows_and_skip_already_existed() : void ); $this->assertEquals(4, $this->databaseContext->tableCount($table)); - $this->assertEquals(2, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(2, $this->executedQueriesCount()); $this->assertEquals( [ ['id' => 1, 'name' => 'Name One', 'description' => 'Description One', 'active' => true], @@ -145,7 +145,7 @@ public function test_inserts_new_rows_and_update_already_existed() : void ); $this->assertEquals(4, $this->databaseContext->tableCount($table)); - $this->assertEquals(2, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(2, $this->executedQueriesCount()); $this->assertEquals( [ ['id' => 1, 'name' => 'Name One', 'description' => 'Description One', 'active' => true], @@ -197,7 +197,7 @@ public function test_inserts_new_rows_and_update_selected_columns_only_of_alread ); $this->assertEquals(4, $this->databaseContext->tableCount($table)); - $this->assertEquals(2, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(2, $this->executedQueriesCount()); $this->assertEquals( [ ['id' => 1, 'name' => 'Name One', 'description' => 'Description One', 'active' => true], diff --git a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/PostgreSqlBulkInsertTest.php b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/PostgreSqlBulkInsertTest.php index f08d032da..6429f94c9 100644 --- a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/PostgreSqlBulkInsertTest.php +++ b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/PostgreSqlBulkInsertTest.php @@ -43,7 +43,7 @@ public function test_inserts_multiple_rows_at_once() : void ); $this->assertEquals(3, $this->databaseContext->tableCount($table)); - $this->assertEquals(1, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(1, $this->executedQueriesCount()); $this->assertSame( [ @@ -94,7 +94,7 @@ public function test_inserts_new_rows_and_skip_already_existed() : void ); $this->assertEquals(4, $this->databaseContext->tableCount($table)); - $this->assertEquals(2, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(2, $this->executedQueriesCount()); $this->assertEquals( [ ['id' => 1, 'name' => 'Name One', 'description' => 'Description One', 'active' => true], @@ -144,7 +144,7 @@ public function test_inserts_new_rows_or_updates_already_existed_based_on_column ); $this->assertEquals(4, $this->databaseContext->tableCount($table)); - $this->assertEquals(2, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(2, $this->executedQueriesCount()); $this->assertEquals( [ ['id' => 1, 'name' => 'Name One', 'description' => 'Description One', 'active' => true], @@ -193,7 +193,7 @@ public function test_inserts_new_rows_or_updates_already_existed_based_on_column ); $this->assertEquals(3, $this->databaseContext->tableCount($table)); - $this->assertEquals(2, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(2, $this->executedQueriesCount()); $this->assertEquals( [ ['id' => 1, 'name' => 'Name One', 'description' => 'Description One', 'active' => true], @@ -242,7 +242,7 @@ public function test_inserts_new_rows_or_updates_already_existed_based_on_primar ); $this->assertEquals(4, $this->databaseContext->tableCount($table)); - $this->assertEquals(2, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(2, $this->executedQueriesCount()); $this->assertEquals( [ ['id' => 1, 'name' => 'Name One', 'description' => 'Description One', 'active' => true], diff --git a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/SqliteBulkInsertTest.php b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/SqliteBulkInsertTest.php index 9347a0388..6edf283c1 100644 --- a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/SqliteBulkInsertTest.php +++ b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/Integration/SqliteBulkInsertTest.php @@ -43,7 +43,7 @@ public function test_inserts_multiple_rows_at_once() : void ); $this->assertEquals(3, $this->databaseContext->tableCount($table)); - $this->assertEquals(1, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(1, $this->executedQueriesCount()); $this->assertEquals( [ @@ -94,7 +94,7 @@ public function test_inserts_new_rows_and_skip_already_existed() : void ); $this->assertEquals(4, $this->databaseContext->tableCount($table)); - $this->assertEquals(2, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(2, $this->executedQueriesCount()); $this->assertEquals( [ ['id' => 1, 'name' => 'Name One', 'description' => 'Description One', 'active' => true], @@ -144,7 +144,7 @@ public function test_inserts_new_rows_or_updates_already_existed_based_on_column ); $this->assertEquals(4, $this->databaseContext->tableCount($table)); - $this->assertEquals(2, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(2, $this->executedQueriesCount()); $this->assertEquals( [ ['id' => 1, 'name' => 'Name One', 'description' => 'Description One', 'active' => true], @@ -193,7 +193,7 @@ public function test_inserts_new_rows_or_updates_already_existed_based_on_column ); $this->assertEquals(3, $this->databaseContext->tableCount($table)); - $this->assertEquals(2, $this->databaseContext->numberOfExecutedInsertQueries()); + $this->assertEquals(2, $this->executedQueriesCount()); $this->assertEquals( [ ['id' => 1, 'name' => 'Name One', 'description' => 'Description One', 'active' => true], diff --git a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/IntegrationTestCase.php b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/IntegrationTestCase.php new file mode 100644 index 000000000..be1312ab0 --- /dev/null +++ b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/IntegrationTestCase.php @@ -0,0 +1,32 @@ +logger = new ProxyLogger(); + + parent::__construct($name); + } + + protected function tearDown() : void + { + $this->databaseContext->dropAllTables(); + } + + public function executedQueriesCount() : int + { + return $this->logger->count; + } +} diff --git a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/MysqlIntegrationTestCase.php b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/MysqlIntegrationTestCase.php index cfe0611b9..34c43b1f7 100644 --- a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/MysqlIntegrationTestCase.php +++ b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/MysqlIntegrationTestCase.php @@ -4,22 +4,21 @@ namespace Flow\Doctrine\Bulk\Tests; +use Doctrine\DBAL\Configuration; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Logging\Middleware; use Flow\Doctrine\Bulk\Tests\Context\DatabaseContext; -use PHPUnit\Framework\TestCase; -abstract class MysqlIntegrationTestCase extends TestCase +abstract class MysqlIntegrationTestCase extends IntegrationTestCase { - protected DatabaseContext $databaseContext; - protected function setUp() : void { - $this->databaseContext = new DatabaseContext(DriverManager::getConnection(['url' => \getenv('MYSQL_DATABASE_URL')])); + $this->databaseContext = new DatabaseContext( + DriverManager::getConnection( + ['url' => \getenv('MYSQL_DATABASE_URL')], + (new Configuration())->setMiddlewares([new Middleware($this->logger)]) + ) + ); $this->databaseContext->connection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); } - - protected function tearDown() : void - { - $this->databaseContext->dropAllTables(); - } } diff --git a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/PostgreSqlIntegrationTestCase.php b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/PostgreSqlIntegrationTestCase.php index da91614eb..bb1974740 100644 --- a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/PostgreSqlIntegrationTestCase.php +++ b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/PostgreSqlIntegrationTestCase.php @@ -4,21 +4,20 @@ namespace Flow\Doctrine\Bulk\Tests; +use Doctrine\DBAL\Configuration; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Logging\Middleware; use Flow\Doctrine\Bulk\Tests\Context\DatabaseContext; -use PHPUnit\Framework\TestCase; -abstract class PostgreSqlIntegrationTestCase extends TestCase +abstract class PostgreSqlIntegrationTestCase extends IntegrationTestCase { - protected DatabaseContext $databaseContext; - protected function setUp() : void { - $this->databaseContext = new DatabaseContext(DriverManager::getConnection(['url' => \getenv('PGSQL_DATABASE_URL')])); - } - - protected function tearDown() : void - { - $this->databaseContext->dropAllTables(); + $this->databaseContext = new DatabaseContext( + DriverManager::getConnection( + ['url' => \getenv('PGSQL_DATABASE_URL')], + (new Configuration())->setMiddlewares([new Middleware($this->logger)]) + ) + ); } } diff --git a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/SqliteIntegrationTestCase.php b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/SqliteIntegrationTestCase.php index c4c41004f..e85e380cc 100644 --- a/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/SqliteIntegrationTestCase.php +++ b/src/lib/doctrine-dbal-bulk/tests/Flow/Doctrine/Bulk/Tests/SqliteIntegrationTestCase.php @@ -4,21 +4,20 @@ namespace Flow\Doctrine\Bulk\Tests; +use Doctrine\DBAL\Configuration; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Logging\Middleware; use Flow\Doctrine\Bulk\Tests\Context\DatabaseContext; -use PHPUnit\Framework\TestCase; -abstract class SqliteIntegrationTestCase extends TestCase +abstract class SqliteIntegrationTestCase extends IntegrationTestCase { - protected DatabaseContext $databaseContext; - protected function setUp() : void { - $this->databaseContext = new DatabaseContext(DriverManager::getConnection(['url' => \getenv('SQLITE_DATABASE_URL')])); - } - - protected function tearDown() : void - { - $this->databaseContext->dropAllTables(); + $this->databaseContext = new DatabaseContext( + DriverManager::getConnection( + ['url' => \getenv('SQLITE_DATABASE_URL')], + (new Configuration())->setMiddlewares([new Middleware($this->logger)]) + ) + ); } }