Skip to content

Commit

Permalink
IBX-1817: Ensured that extra schema keys throw exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p authored Jan 3, 2022
2 parents 88cbefd + 6e627c5 commit acfebf7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/lib/Importer/SchemaImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Ibexa\Contracts\DoctrineSchema\Exception\InvalidConfigurationException;
use Ibexa\Contracts\DoctrineSchema\SchemaImporterInterface as APISchemaImporter;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -70,6 +71,14 @@ private function importSchemaTable(
): void {
$table = $schema->createTable($tableName);

$this->ensureNoExtraKeys($tableConfiguration, $tableName, [
'id',
'fields',
'foreignKeys',
'indexes',
'uniqueConstraints',
]);

if (isset($tableConfiguration['id'])) {
$this->addSchemaTableColumns($table, $tableConfiguration['id']);
$table->setPrimaryKey(array_keys($tableConfiguration['id']));
Expand Down Expand Up @@ -121,6 +130,16 @@ private function importSchemaTable(
private function addSchemaTableColumns(Table $table, array $columnList): void
{
foreach ($columnList as $columnName => $columnConfiguration) {
$location = sprintf('%s.fields.%s', $table->getName(), $columnName);
$this->ensureNoExtraKeys($columnConfiguration, $location, [
'length',
'scale',
'precision',
'type',
'nullable',
'options',
]);

if (isset($columnConfiguration['length'])) {
$columnConfiguration['options']['length'] = $columnConfiguration['length'];
}
Expand All @@ -144,6 +163,19 @@ private function addSchemaTableColumns(Table $table, array $columnList): void
}
}
}

private function ensureNoExtraKeys(array $tableConfiguration, string $location, array $allowedKeys): void
{
$diff = array_diff(array_keys($tableConfiguration), $allowedKeys);
if (!empty($diff)) {
throw new InvalidConfigurationException(sprintf(
'Unhandled property in schema configuration for "%s". "%s" keys are not allowed. Allowed keys: "%s".',
$location,
implode('", "', $diff),
implode('", "', $allowedKeys),
));
}
}
}

class_alias(SchemaImporter::class, 'EzSystems\DoctrineSchema\Importer\SchemaImporter');
25 changes: 25 additions & 0 deletions tests/lib/Importer/SchemaImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Ibexa\Contracts\DoctrineSchema\Exception\InvalidConfigurationException;
use Ibexa\DoctrineSchema\Importer\SchemaImporter;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -218,6 +219,30 @@ public function testImportFromFile(
"Yaml schema definition {$yamlSchemaDefinitionFile} produced unexpected Schema object"
);
}

public function testTableImportFailsIfUnhandledKeys(): void
{
$importer = new SchemaImporter();

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage(
'Unhandled property in schema configuration for "my_table". "foo" keys are not allowed. Allowed keys:'
. ' "id", "fields", "foreignKeys", "indexes", "uniqueConstraints".'
);
$importer->importFromFile(__DIR__ . '/_fixtures/failing-import.yaml');
}

public function testColumnImportFailsIfUnhandledKeys(): void
{
$importer = new SchemaImporter();

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage(
'Unhandled property in schema configuration for "my_table.fields.foo". "bar" keys are not allowed. Allowed keys:'
. ' "length", "scale", "precision", "type", "nullable", "options".'
);
$importer->importFromFile(__DIR__ . '/_fixtures/failing-import-column.yaml');
}
}

class_alias(SchemaImporterTest::class, 'EzSystems\Tests\DoctrineSchema\Importer\SchemaImporterTest');
5 changes: 5 additions & 0 deletions tests/lib/Importer/_fixtures/failing-import-column.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tables:
my_table:
fields:
foo:
bar: ~
3 changes: 3 additions & 0 deletions tests/lib/Importer/_fixtures/failing-import.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tables:
my_table:
foo: bar

0 comments on commit acfebf7

Please sign in to comment.