Skip to content

Commit

Permalink
Rework Doctrine Bulk tests to not use deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Nov 28, 2023
1 parent 2f360d6 commit 5a1cc53
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
Expand All @@ -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;
}
Expand All @@ -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(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Flow\Doctrine\Bulk\Tests\Context;

use Psr\Log\AbstractLogger;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;

final class ProxyLogger extends AbstractLogger implements LoggerAwareInterface
{
use LoggerAwareTrait;

public int $count = 0;

public function __construct()
{
$this->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++;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace Flow\Doctrine\Bulk\Tests;

use Flow\Doctrine\Bulk\Tests\Context\DatabaseContext;
use Flow\Doctrine\Bulk\Tests\Context\ProxyLogger;
use PHPUnit\Framework\TestCase;

abstract class IntegrationTestCase extends TestCase
{
protected DatabaseContext $databaseContext;

protected readonly ProxyLogger $logger;

public function __construct(string $name)
{
$this->logger = new ProxyLogger();

parent::__construct($name);
}

protected function tearDown() : void
{
$this->databaseContext->dropAllTables();
}

public function executedQueriesCount() : int
{
return $this->logger->count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
)
);
}
}

0 comments on commit 5a1cc53

Please sign in to comment.