Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes duplicate keys in normalizedColumnNameMap #1726

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fixes duplicate keys in normalizedColumnNameMap
  • Loading branch information
hsegnitz committed May 5, 2021
commit aa311340066d77f507d73546011c40d68da869c6
67 changes: 25 additions & 42 deletions src/Propel/Generator/Builder/Om/TableMapBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,55 +453,38 @@ protected function addFieldsAttributes()
*/
protected function addNormalizedColumnNameMap(&$script): void
{
$script .= '
/**
* Holds a list of column names and their normalized version.
*
* @var string[]
*/
protected $normalizedColumnNameMap = [' . PHP_EOL;

$table = $this->getTable();
$tableColumns = $table->getColumns();

foreach ($tableColumns as $num => $column) {
$normalizedName = strtoupper($column->getName());

// ColumnName => COLUMN_NAME
$script .= '
\'' . $column->getPhpName() . '\' => \'' . $normalizedName . '\',';

// TableName.ColumnName => COLUMN_NAME
$script .= '
\'' . $table->getPhpName() . '.' . $column->getPhpName() . '\' => \'' . $normalizedName . '\',';

// columnName => COLUMN_NAME
$script .= '
\'' . $column->getCamelCaseName() . '\' => \'' . $normalizedName . '\',';
$arrayString = '';
foreach ($tableColumns as $column) {
$variants = [
$column->getPhpName(), // ColumnName => COLUMN_NAME
$table->getPhpName() . '.' . $column->getPhpName(), // TableName.ColumnName => COLUMN_NAME
$column->getCamelCaseName(), // columnName => COLUMN_NAME
$table->getCamelCaseName() . '.' . $column->getCamelCaseName(), // tableName.columnName => COLUMN_NAME
$this->getColumnConstant($column, $this->getTableMapClass()), // TableNameTableMap::COL_COLUMN_NAME => COLUMN_NAME
$column->getConstantName(), // COL_COLUMN_NAME => COLUMN_NAME
$column->getName(), // column_name => COLUMN_NAME
$table->getName() . '.' . $column->getName(), // table_name.column_name => COLUMN_NAME
];

$variants = array_unique($variants);

// tableName.columnName => COLUMN_NAME
$script .= '
\'' . $table->getCamelCaseName() . '.' . $column->getCamelCaseName() . '\' => \'' . $normalizedName . '\',';

// TableNameTableMap::COL_COLUMN_NAME => COLUMN_NAME
$script .= '
\'' . $this->getColumnConstant($column, $this->getTableMapClass()) . '\' => \'' . $normalizedName . '\',';

// COL_COLUMN_NAME => COLUMN_NAME
$script .= '
\'' . $column->getConstantName() . '\' => \'' . $normalizedName . '\',';

// column_name => COLUMN_NAME
$script .= '
\'' . $column->getName() . '\' => \'' . $normalizedName . '\',';

// table_name.column_name => COLUMN_NAME
$script .= '
\'' . $table->getName() . '.' . $column->getName() . '\' => \'' . $normalizedName . '\',';
$normalizedName = strtoupper($column->getName());
array_walk($variants, static function ($variant) use (&$arrayString, $normalizedName) {
$arrayString .= PHP_EOL . " '{$variant}' => '{$normalizedName}',";
});
}

$script .= '
];' . PHP_EOL;
/**
* Holds a list of column names and their normalized version.
*
* @var string[]
*/
protected $normalizedColumnNameMap = [' . $arrayString . PHP_EOL
. ' ];' . PHP_EOL;
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Propel/Tests/Generator/Builder/Om/TableMapBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Propel\Tests\Generator\Builder\Om;

use Propel\Generator\Builder\Om\TableMapBuilder;
use Propel\Generator\Builder\Util\SchemaReader;
use Propel\Generator\Config\QuickGeneratorConfig;
use Propel\Runtime\Map\RelationMap;
use Propel\Runtime\Propel;
use Propel\Tests\Bookstore\Behavior\Map\Table1TableMap;
Expand Down Expand Up @@ -357,4 +360,43 @@ public function testIsCrossRef()
$BookListRelTable = $this->databaseMap->getTableByPhpName('Propel\Tests\Bookstore\BookListRel');
$this->assertTrue($BookListRelTable->isCrossRef(), 'The map builder add isCrossRef information "true"');
}

/**
* @return void
*/
public function testNormalizedColumnMapHasOnlyUniqueKeys()
{
$databaseXml = '
<database>
<table name="email">
<column name="id" type="integer"/>
<column name="email_address" type="varchar"/>
</table>
</database>
';
$reader = new SchemaReader();
$schema = $reader->parseString($databaseXml);
$table = $schema->getDatabase()->getTable('email');

$tableMapBuilder = new class($table) extends TableMapBuilder {
public function getNormalizedColumnNameMapDefinition(): string
{
$script = '';
$this->addNormalizedColumnNameMap($script);
return $script;
}
};
$tableMapBuilder->setGeneratorConfig(new QuickGeneratorConfig());
$normalizedColumnMapDefinition = $tableMapBuilder->getNormalizedColumnNameMapDefinition();

// extract inner part of the array
$this->assertEquals(1, preg_match('/= \[\n(.*)\n\s*\]/ms', $normalizedColumnMapDefinition, $matches));

// split, then check for number of lines -- so that we are sure nothing vanishes
$list = explode(PHP_EOL, $matches[1]);
$this->assertCount(14, $list);

// check for uniqueness -> unique list has to be the same size
$this->assertCount(14, array_unique($list));
}
}