Skip to content

Commit

Permalink
Add additional validation when adding constraints & indexes.
Browse files Browse the repository at this point in the history
Add more extensive checks when adding indexes & constraints.
Allow columns key to be defined as a string. This is an easy to
correct error user error.
  • Loading branch information
markstory committed Jun 22, 2013
1 parent 804d99b commit f69bfd8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
8 changes: 8 additions & 0 deletions lib/Cake/Database/Schema/Table.php
Expand Up @@ -249,6 +249,10 @@ public function addIndex($name, $attrs) {
if (!in_array($attrs['type'], $this->_validIndexTypes, true)) {
throw new Exception(__d('cake_dev', 'Invalid index type "%s"', $attrs['type']));
}
if (empty($attrs['columns'])) {
throw new Exception(__d('cake_dev', 'Indexes must define columns.'));
}
$attrs['columns'] = (array)$attrs['columns'];
foreach ($attrs['columns'] as $field) {
if (empty($this->_columns[$field])) {
throw new Exception(__d('cake_dev', 'Columns used in indexes must already exist.'));
Expand Down Expand Up @@ -325,6 +329,10 @@ public function addConstraint($name, $attrs) {
if (!in_array($attrs['type'], $this->_validConstraintTypes, true)) {
throw new Exception(__d('cake_dev', 'Invalid constraint type "%s"', $attrs['type']));
}
if (empty($attrs['columns'])) {
throw new Exception(__d('cake_dev', 'Constraints must define columns.'));
}
$attrs['columns'] = (array)$attrs['columns'];
foreach ($attrs['columns'] as $field) {
if (empty($this->_columns[$field])) {
throw new Exception(__d('cake_dev', 'Columns used in constraints must already exist.'));
Expand Down
65 changes: 43 additions & 22 deletions lib/Cake/Test/TestCase/Database/Schema/TableTest.php
Expand Up @@ -122,17 +122,35 @@ public function testAddConstraint() {
}

/**
* Test that an exception is raised when constraintes
* Dataprovider for invalid addConstraint calls.
*
* @return array
*/
public static function addConstaintErrorProvider() {
return [
// No properties
[[]],
// Empty columns
[['columns' => '']],
[['columns' => []]],
// Missing column
[['columns' => ['derp']]],
// Invalid type
[['columns' => 'author_id', 'type' => 'derp']],
];
}
/**
* Test that an exception is raised when constraints
* are added for fields that do not exist.
*
* @dataProvider addConstaintErrorProvider
* @expectedException Cake\Database\Exception
* @return void
*/
public function testAddConstraintErrorWhenFieldIsMissing() {
public function testAddConstraintError($props) {
$table = new Table('articles');
$table->addConstraint('author_idx', [
'columns' => ['author_id']
]);
$table->addColumn('author_id', 'integer');
$table->addConstraint('author_idx', $props);
}

/**
Expand All @@ -154,33 +172,36 @@ public function testAddIndex() {
}

/**
* Test that an exception is raised when indexes
* are added for fields that do not exist.
* Dataprovider for invalid addIndex calls
*
* @expectedException Cake\Database\Exception
* @return void
* @return array
*/
public function testAddIndexErrorWhenFieldIsMissing() {
$table = new Table('articles');
$table->addIndex('author_idx', [
'columns' => ['author_id']
]);
public static function addIndexErrorProvider() {
return [
// Empty
[[]],
// No columns
[['columns' => '']],
[['columns' => []]],
// Missing column
[['columns' => ['not_there']]],
// Invalid type
[['columns' => 'author_id', 'type' => 'derp']],
];
}

/**
* Test that exceptions are raised when indexes
* are added with invalid types
* Test that an exception is raised when indexes
* are added for fields that do not exist.
*
* @dataProvider addIndexErrorProvider
* @expectedException Cake\Database\Exception
* @return void
*/
public function testAddIndexErrorWrongType() {
public function testAddIndexError($props) {
$table = new Table('articles');
$table->addColumn('author_id', 'integer')
->addIndex('author_idx', [
'type' => 'derp',
'columns' => ['author_id']
]);
$table->addColumn('author_id', 'integer');
$table->addIndex('author_idx', $props);
}

/**
Expand Down

0 comments on commit f69bfd8

Please sign in to comment.