Skip to content

Commit 464ed7e

Browse files
authored
Refactor test of columns (#399)
1 parent df759ea commit 464ed7e

File tree

6 files changed

+412
-754
lines changed

6 files changed

+412
-754
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
- Enh #353: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov)
1212
- Enh #354: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov)
1313
- Enh #356: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
14-
- New #355, #368, #370: Implement `ColumnFactory` class (@Tigrov)
14+
- New #355, #368, #370, #399: Implement `ColumnFactory` class (@Tigrov)
1515
- Enh #359: Separate column type constants (@Tigrov)
1616
- Enh #359: Remove `Schema::TYPE_ARRAY` and `Schema::TYPE_STRUCTURED` constants (@Tigrov)
1717
- New #360: Realize `ColumnBuilder` class (@Tigrov)

src/Column/ColumnFactory.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,7 @@ public function fromType(string $type, array $info = []): ColumnInterface
124124
$column = parent::fromType($type, $info);
125125

126126
if ($column instanceof StructuredColumn) {
127-
/** @psalm-var array|null $defaultValue */
128-
$defaultValue = $column->getDefaultValue();
129-
130-
if (is_array($defaultValue)) {
131-
foreach ($column->getColumns() as $structuredColumnName => $structuredColumn) {
132-
if (isset($defaultValue[$structuredColumnName])) {
133-
$structuredColumn->defaultValue($defaultValue[$structuredColumnName]);
134-
}
135-
}
136-
}
127+
$this->initializeStructuredDefaultValue($column);
137128
}
138129

139130
return $column;
@@ -179,4 +170,25 @@ protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnInte
179170

180171
return $value;
181172
}
173+
174+
/**
175+
* Initializes the default value for structured columns.
176+
*/
177+
private function initializeStructuredDefaultValue(StructuredColumn $column): void
178+
{
179+
/** @psalm-var array|null $defaultValue */
180+
$defaultValue = $column->getDefaultValue();
181+
182+
if (is_array($defaultValue)) {
183+
foreach ($column->getColumns() as $structuredColumnName => $structuredColumn) {
184+
if (isset($defaultValue[$structuredColumnName])) {
185+
$structuredColumn->defaultValue($defaultValue[$structuredColumnName]);
186+
187+
if ($structuredColumn instanceof StructuredColumn) {
188+
$this->initializeStructuredDefaultValue($structuredColumn);
189+
}
190+
}
191+
}
192+
}
193+
}
182194
}

tests/ColumnTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,11 @@ public function testStructuredType(): void
277277
name: 'price_array2',
278278
notNull: false,
279279
columns: [
280-
'value' => new DoubleColumn(ColumnType::DECIMAL, dbType: 'numeric', name: 'value', notNull: false, scale: 2, size: 10),
281-
'currency_code' => new StringColumn(ColumnType::CHAR, dbType: 'bpchar', name: 'currency_code', notNull: false, size: 3),
280+
'value' => new DoubleColumn(ColumnType::DECIMAL, dbType: 'numeric', name: 'value', notNull: false, scale: 2, size: 10, defaultValue: null),
281+
'currency_code' => new StringColumn(ColumnType::CHAR, dbType: 'bpchar', name: 'currency_code', notNull: false, size: 3, defaultValue: null),
282282
],
283283
),
284+
defaultValue: null,
284285
),
285286
),
286287
$priceArray2->dbTypecast([null, null]),

0 commit comments

Comments
 (0)