Skip to content

Commit

Permalink
Merge pull request #5232 from morozov/issues/5231
Browse files Browse the repository at this point in the history
Consider CASE_LOWER being equal to 0 in portability middleware
  • Loading branch information
morozov authored Feb 2, 2022
2 parents d9918cf + 4714b59 commit 0dc504e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/Portability/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function connect(array $params)
$this->mode
);

$case = 0;
$case = null;

if ($this->case !== 0 && ($portability & Connection::PORTABILITY_FIX_CASE) !== 0) {
$nativeConnection = null;
Expand All @@ -63,7 +63,7 @@ public function connect(array $params)
$convertEmptyStringToNull = ($portability & Connection::PORTABILITY_EMPTY_TO_NULL) !== 0;
$rightTrimString = ($portability & Connection::PORTABILITY_RTRIM) !== 0;

if (! $convertEmptyStringToNull && ! $rightTrimString && $case === 0) {
if (! $convertEmptyStringToNull && ! $rightTrimString && $case === null) {
return $connection;
}

Expand Down
118 changes: 68 additions & 50 deletions tests/Functional/PortabilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,13 @@
use Doctrine\DBAL\Portability\Middleware;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Throwable;

use function array_keys;
use function array_merge;
use function strlen;

class PortabilityTest extends FunctionalTestCase
{
protected function setUp(): void
{
$configuration = $this->connection->getConfiguration();
$configuration->setMiddlewares(
array_merge(
$configuration->getMiddlewares(),
[new Middleware(Connection::PORTABILITY_ALL, ColumnCase::LOWER)]
)
);

$this->connection = DriverManager::getConnection($this->connection->getParams(), $configuration);

try {
$table = new Table('portability_table');
$table->addColumn('Test_Int', 'integer');
$table->addColumn('Test_String', 'string', ['fixed' => true, 'length' => 32]);
$table->addColumn('Test_Null', 'string', ['notnull' => false]);
$table->setPrimaryKey(['Test_Int']);

$sm = $this->connection->getSchemaManager();
$sm->createTable($table);

$this->connection->insert('portability_table', [
'Test_Int' => 1,
'Test_String' => 'foo',
'Test_Null' => '',
]);

$this->connection->insert('portability_table', [
'Test_Int' => 2,
'Test_String' => 'foo ',
'Test_Null' => null,
]);
} catch (Throwable $e) {
}
}

protected function tearDown(): void
{
// the connection that overrides the shared one has to be manually closed prior to 4.0.0 to prevent leak
Expand All @@ -61,6 +24,9 @@ protected function tearDown(): void

public function testFullFetchMode(): void
{
$this->connectWithPortability(Connection::PORTABILITY_ALL, ColumnCase::LOWER);
$this->createTable();

$rows = $this->connection->fetchAllAssociative('SELECT * FROM portability_table');
$this->assertFetchResultRows($rows);

Expand All @@ -79,22 +45,29 @@ public function testFullFetchMode(): void
}
}

public function testConnFetchMode(): void
/**
* @param array{int, list<string>} $expected
*
* @dataProvider caseProvider
*/
public function testCaseConversion(int $case, array $expected): void
{
$rows = $this->connection->fetchAllAssociative('SELECT * FROM portability_table');
$this->assertFetchResultRows($rows);
$this->connectWithPortability(Connection::PORTABILITY_FIX_CASE, $case);
$this->createTable();

$result = $this->connection->executeQuery('SELECT * FROM portability_table');
while (($row = $result->fetchAssociative())) {
$this->assertFetchResultRow($row);
}
$row = $this->connection->fetchAssociative('SELECT * FROM portability_table');

$result = $this->connection->prepare('SELECT * FROM portability_table')
->execute();
self::assertNotFalse($row);
self::assertSame($expected, array_keys($row));
}

while (($row = $result->fetchAssociative())) {
$this->assertFetchResultRow($row);
}
/**
* @return iterable<string, array{int, list<string>}>
*/
public static function caseProvider(): iterable
{
yield 'lower' => [ColumnCase::LOWER, ['test_int', 'test_string', 'test_null']];
yield 'upper' => [ColumnCase::UPPER, ['TEST_INT', 'TEST_STRING', 'TEST_NULL']];
}

/**
Expand Down Expand Up @@ -131,6 +104,9 @@ public function assertFetchResultRow(array $row): void
*/
public function testFetchColumn(string $column, array $expected): void
{
$this->connectWithPortability(Connection::PORTABILITY_RTRIM, 0);
$this->createTable();

$result = $this->connection->executeQuery('SELECT ' . $column . ' FROM portability_table');

self::assertEquals($expected, $result->fetchFirstColumn());
Expand All @@ -155,8 +131,50 @@ public static function fetchColumnProvider(): iterable

public function testFetchAllNullColumn(): void
{
$this->connectWithPortability(Connection::PORTABILITY_EMPTY_TO_NULL, 0);
$this->createTable();

$column = $this->connection->fetchFirstColumn('SELECT Test_Null FROM portability_table');

self::assertSame([null, null], $column);
}

private function connectWithPortability(int $mode, int $case): void
{
// closing the default connection prior to 4.0.0 to prevent connection leak
$this->connection->close();

$configuration = $this->connection->getConfiguration();
$configuration->setMiddlewares(
array_merge(
$configuration->getMiddlewares(),
[new Middleware($mode, $case)]
)
);

$this->connection = DriverManager::getConnection($this->connection->getParams(), $configuration);
}

private function createTable(): void
{
$table = new Table('portability_table');
$table->addColumn('Test_Int', 'integer');
$table->addColumn('Test_String', 'string', ['fixed' => true, 'length' => 32]);
$table->addColumn('Test_Null', 'string', ['notnull' => false]);
$table->setPrimaryKey(['Test_Int']);

$this->dropAndCreateTable($table);

$this->connection->insert('portability_table', [
'Test_Int' => 1,
'Test_String' => 'foo',
'Test_Null' => '',
]);

$this->connection->insert('portability_table', [
'Test_Int' => 2,
'Test_String' => 'foo ',
'Test_Null' => null,
]);
}
}

0 comments on commit 0dc504e

Please sign in to comment.