Skip to content

Commit

Permalink
Add additional ETL DB Model error checking (#1237)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpalmer authored Feb 27, 2020
1 parent 572b831 commit fefdbc5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions classes/ETL/DbModel/ForeignKeyConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function __construct(
public function initialize(stdClass $config)
{
if (!isset($config->name)) {
if (!isset($config->columns) || !is_array($config->columns)) {
$this->logAndThrowException('"columns" must be an array');
}
$config->name = $this->generateForeignKeyConstraintName($config->columns);
}

Expand Down
3 changes: 3 additions & 0 deletions classes/ETL/DbModel/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function initialize(stdClass $config)
// Local verifications

if ( ! isset($config->name) ) {
if (!isset($config->columns) || !is_array($config->columns)) {
$this->logAndThrowException('"columns" must be an array');
}
$config->name = $this->generateIndexName($config->columns);
}

Expand Down
68 changes: 68 additions & 0 deletions tests/unit/lib/ETL/DbModel/DbModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,74 @@ public function testTableSchema()
$this->assertEquals($expected, $generated);
}

/**
* Test index initialization error.
*
* @expectedException Exception
* @expectedExceptionMessage "columns" must be an array
*/
public function testIndexInitializationError()
{
$config = (object) [
'name' => 'initialize_error',
'columns' => [
(object) [
'name' => 'column1',
'type' => 'int(11)',
'nullable' => false,
'default' => 0,
'comment' => 'This is my comment'
]
],
'indexes' => [
(object) [
'type' => 'PRIMARY'
]
]
];

$table = new Table($config);
$table->verify();
}

/**
* Test foreign key constraint initialization error.
*
* @expectedException Exception
* @expectedExceptionMessage "columns" must be an array
*/
public function testForeignKeyConstraintInitializationError()
{
$config = (object) [
'name' => 'initialize_error',
'columns' => [
(object) [
'name' => 'column1',
'type' => 'int(11)',
'nullable' => false
]
],
'indexes' => [
(object) [
'columns' => [
'column1'
]
]
],
'foreign_key_constraints' => [
(object) [
'referenced_table' => 'other_table',
'referenced_columns' => [
'id'
]
]
]
];

$table = new Table($config);
$table->verify();
}

/**
* Test table verification error
*
Expand Down

0 comments on commit fefdbc5

Please sign in to comment.