diff --git a/src/Phinx/Db/Adapter/SqlServerAdapter.php b/src/Phinx/Db/Adapter/SqlServerAdapter.php index 6a36f81cd..41e3795c6 100644 --- a/src/Phinx/Db/Adapter/SqlServerAdapter.php +++ b/src/Phinx/Db/Adapter/SqlServerAdapter.php @@ -487,7 +487,7 @@ public function getColumns(string $tableName): array $column->setPrecision((int)$columnInfo['precision']); } - $columns[$columnInfo['name']] = $column; + $columns[] = $column; } return $columns; @@ -626,10 +626,10 @@ protected function getChangeDefault(string $tableName, Column $newColumn): Alter */ protected function getChangeColumnInstructions(string $tableName, string $columnName, Column $newColumn): AlterInstructions { - $columns = $this->getColumns($tableName); + $column = $this->getColumn($tableName, $columnName); $changeDefault = - $newColumn->getDefault() !== $columns[$columnName]->getDefault() || - $newColumn->getType() !== $columns[$columnName]->getType(); + $newColumn->getDefault() !== $column?->getDefault() || + $newColumn->getType() !== $column?->getType(); $instructions = new AlterInstructions(); @@ -1382,4 +1382,25 @@ public function getDecoratedConnection(): Connection return $this->decoratedConnection = $this->buildConnection(SqlServerDriver::class, $options); } + + /** + * Gets a table column + * + * @param string $tableName + * @param string $columnName + * @return \Phinx\Db\Table\Column|null + */ + private function getColumn(string $tableName, string $columnName): ?Column + { + $columns = $this->getColumns($tableName); + + $filteredColumns = array_filter( + $columns, + static function ($column) use ($columnName) { + return $column->getName() === $columnName; + }, + ); + + return array_pop($filteredColumns); + } } diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index a69fc96a1..d946677c1 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -479,7 +479,7 @@ public function testAddColumnWithDefaultNull() } } - public function testAddColumnWithNotNullableNoDefault() + public function testAddColumnWithNotNullableNoDefault(): void { $table = new Table('table1', [], $this->adapter); $table @@ -488,10 +488,12 @@ public function testAddColumnWithNotNullableNoDefault() $columns = $this->adapter->getColumns('table1'); $this->assertCount(2, $columns); - $this->assertArrayHasKey('id', $columns); - $this->assertArrayHasKey('col', $columns); - $this->assertFalse($columns['col']->isNull()); - $this->assertNull($columns['col']->getDefault()); + $firstColumn = $columns[0]; + $this->assertSame('id', $firstColumn->getName()); + $secondColumn = $columns[1]; + $this->assertSame('col', $secondColumn->getName()); + $this->assertFalse($secondColumn->isNull()); + $this->assertNull($secondColumn->getDefault()); } public function testAddColumnWithDefaultBool() @@ -522,7 +524,7 @@ public function testAddColumnWithLiteralTypeAndDefault() ->addColumn('checked', Literal::from('bit'), ['default' => 0]) ->save(); - $column = $this->adapter->getColumns('table1')['checked']; + $column = $this->adapter->getColumns('table1')[1]; $this->assertSame('checked', $column->getName()); $this->assertSame('boolean', $column->getType()); @@ -530,7 +532,7 @@ public function testAddColumnWithLiteralTypeAndDefault() $this->assertTrue($column->getNull()); } - public function testAddColumnWithCustomType() + public function testAddColumnWithCustomType(): void { $this->adapter->setDataDomain([ 'custom' => [ @@ -549,18 +551,17 @@ public function testAddColumnWithCustomType() $this->assertTrue($this->adapter->hasTable('table1')); $columns = $this->adapter->getColumns('table1'); - $this->assertArrayHasKey('custom', $columns); - $this->assertArrayHasKey('custom_ext', $columns); + $this->assertCount(3, $columns); - $column = $this->adapter->getColumns('table1')['custom']; - $this->assertSame('custom', $column->getName()); - $this->assertSame('geometry', (string)$column->getType()); - $this->assertTrue($column->getNull()); + $customColumn = $columns[1]; + $this->assertSame('custom', $customColumn->getName()); + $this->assertSame('geometry', (string)$customColumn->getType()); + $this->assertTrue($customColumn->getNull()); - $column = $this->adapter->getColumns('table1')['custom_ext']; - $this->assertSame('custom_ext', $column->getName()); - $this->assertSame('geometry', (string)$column->getType()); - $this->assertFalse($column->getNull()); + $customExtColumn = $columns[2]; + $this->assertSame('custom_ext', $customExtColumn->getName()); + $this->assertSame('geometry', (string)$customExtColumn->getType()); + $this->assertFalse($customExtColumn->getNull()); } public function testRenameColumn() @@ -632,7 +633,7 @@ public function testChangeColumnNameAndNull() } } - public function testChangeColumnDefaults() + public function testChangeColumnDefaults(): void { $table = new Table('t', [], $this->adapter); $table->addColumn('column1', 'string', ['default' => 'test']) @@ -640,7 +641,7 @@ public function testChangeColumnDefaults() $this->assertTrue($this->adapter->hasColumn('t', 'column1')); $columns = $this->adapter->getColumns('t'); - $this->assertSame('test', $columns['column1']->getDefault()); + $this->assertSame('test', $columns[1]->getDefault()); $newColumn1 = new Column(); $newColumn1 @@ -650,10 +651,10 @@ public function testChangeColumnDefaults() $this->assertTrue($this->adapter->hasColumn('t', 'column1')); $columns = $this->adapter->getColumns('t'); - $this->assertSame('another test', $columns['column1']->getDefault()); + $this->assertSame('another test', $columns[1]->getDefault()); } - public function testChangeColumnDefaultToNull() + public function testChangeColumnDefaultToNull(): void { $table = new Table('t', [], $this->adapter); $table->addColumn('column1', 'string', ['null' => true, 'default' => 'test']) @@ -664,10 +665,10 @@ public function testChangeColumnDefaultToNull() ->setDefault(null); $table->changeColumn('column1', $newColumn1)->save(); $columns = $this->adapter->getColumns('t'); - $this->assertNull($columns['column1']->getDefault()); + $this->assertNull($columns[1]->getDefault()); } - public function testChangeColumnDefaultToZero() + public function testChangeColumnDefaultToZero(): void { $table = new Table('t', [], $this->adapter); $table->addColumn('column1', 'integer') @@ -678,7 +679,7 @@ public function testChangeColumnDefaultToZero() ->setDefault(0); $table->changeColumn('column1', $newColumn1)->save(); $columns = $this->adapter->getColumns('t'); - $this->assertSame(0, $columns['column1']->getDefault()); + $this->assertSame(0, $columns[1]->getDefault()); } public function testDropColumn() @@ -691,7 +692,7 @@ public function testDropColumn() $this->assertFalse($this->adapter->hasColumn('t', 'column1')); } - public function columnsProvider() + public static function columnsProvider(): array { return [ ['column1', 'string', ['null' => true, 'default' => null]], @@ -718,7 +719,7 @@ public function columnsProvider() /** * @dataProvider columnsProvider */ - public function testGetColumns($colName, $type, $options) + public function testGetColumns($colName, $type, $options): void { $table = new Table('t', [], $this->adapter); $table @@ -727,19 +728,23 @@ public function testGetColumns($colName, $type, $options) $columns = $this->adapter->getColumns('t'); $this->assertCount(2, $columns); - $this->assertEquals($colName, $columns[$colName]->getName()); - $this->assertEquals($type, $columns[$colName]->getType()); + + $specificColumn = $this->getColumn('t', $colName); + $this->assertNotNull($specificColumn); + + $this->assertEquals($colName, $specificColumn->getName()); + $this->assertEquals($type, $specificColumn->getType()); if (isset($options['limit'])) { - $this->assertEquals($options['limit'], $columns[$colName]->getLimit()); + $this->assertEquals($options['limit'], $specificColumn->getLimit()); } if (isset($options['precision'])) { - $this->assertEquals($options['precision'], $columns[$colName]->getPrecision()); + $this->assertEquals($options['precision'], $specificColumn->getPrecision()); } if (isset($options['scale'])) { - $this->assertEquals($options['scale'], $columns[$colName]->getScale()); + $this->assertEquals($options['scale'], $specificColumn->getScale()); } } @@ -1663,4 +1668,18 @@ public function testInvalidPdoAttribute($attribute) $this->expectExceptionMessage('Invalid PDO attribute: ' . $attribute . ' (\PDO::' . strtoupper($attribute) . ')'); $adapter->connect(); } + + private function getColumn(string $tableName, string $columnName): ?Column + { + $columns = $this->adapter->getColumns($tableName); + + $filteredColumns = array_filter( + $columns, + static function ($column) use ($columnName) { + return $column->getName() === $columnName; + }, + ); + + return array_pop($filteredColumns); + } }