From 13f7da961fe019915a502629e6e2f62ee1dc2fed Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 2 Jun 2013 14:35:37 -0400 Subject: [PATCH] Add tests for index reflection Fix issues with primary key reflection and add missing tests for reflection of KEY statements. --- lib/Cake/Database/Schema/MysqlSchema.php | 4 ++-- .../Database/Schema/MysqlSchemaTest.php | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/Cake/Database/Schema/MysqlSchema.php b/lib/Cake/Database/Schema/MysqlSchema.php index 222ae3827c0..ec9f5db5c60 100644 --- a/lib/Cake/Database/Schema/MysqlSchema.php +++ b/lib/Cake/Database/Schema/MysqlSchema.php @@ -183,7 +183,7 @@ public function convertIndexDescription(Table $table, $row) { $type = strtolower($row['Index_type']); } elseif ($row['Non_unique'] == 0 && $type !== 'primary') { $type = 'unique'; - } else { + } elseif ($type !== 'primary') { $type = 'index'; } @@ -203,7 +203,7 @@ public function convertIndexDescription(Table $table, $row) { // MySQL multi column indexes come back // as multiple rows. if (!empty($existing)) { - $columns = array_merge($existing['columns'], $columns); + $columns = array_unique(array_merge($existing['columns'], $columns)); $length = array_merge($existing['length'], $length); } if ($isIndex) { diff --git a/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php b/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php index 1d8ef3f6c03..6c393ad7c5c 100644 --- a/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php +++ b/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php @@ -282,6 +282,8 @@ public function testDescribeTableIndexes() { $schema = new SchemaCollection($connection); $result = $schema->describe('articles'); $this->assertInstanceOf('Cake\Database\Schema\Table', $result); + + $this->assertCount(2, $result->constraints()); $expected = [ 'primary' => [ 'type' => 'primary', @@ -296,13 +298,16 @@ public function testDescribeTableIndexes() { ] ] ]; - foreach ($expected as $name => $props) { - $this->assertEquals( - $props, - $result->constraint($name), - 'Index definition does not match for ' . $name - ); - } + $this->assertEquals($expected['primary'], $result->constraint('primary')); + $this->assertEquals($expected['length_idx'], $result->constraint('length_idx')); + + $this->assertCount(1, $result->indexes()); + $expected = [ + 'type' => 'index', + 'columns' => ['author_id'], + 'length' => [] + ]; + $this->assertEquals($expected, $result->index('author_idx')); } /**