Skip to content

Commit df547c4

Browse files
committed
Tests: refactored formatting of schema
1 parent 987991d commit df547c4

4 files changed

Lines changed: 138 additions & 86 deletions

File tree

tests/SqlSchema/Index.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test(function () {
1515
Assert::same('PRIMARY', $index->getType());
1616
Assert::same([
1717
'id ASC'
18-
], format($index->getColumns()));
18+
], formatIndexColumns($index->getColumns()));
1919
});
2020

2121

tests/SqlSchema/Table.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test(function () {
4040

4141
Assert::same([
4242
'CONSTRAINT FOREIGN KEY (author_id) REFERENCES author (id)',
43-
], format($table->getForeignKeys()));
43+
], formatForeignKeys($table->getForeignKeys()));
4444
});
4545

4646

tests/SqlSchema/readme.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ test(function () {
2828
'PRIMARY (id ASC)',
2929
'UNIQUE name_author_id (name ASC, author_id ASC)',
3030
],
31-
], format($schema));
31+
], formatSchema($schema));
3232
});

tests/bootstrap.php

Lines changed: 135 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -19,117 +19,169 @@ function test(callable $cb)
1919

2020

2121
/**
22-
* @param object|object[] $obj
23-
* @return mixed
22+
* @return array<string, array<string>>
2423
*/
25-
function format($obj)
24+
function formatSchema(SqlSchema\Schema $obj): array
2625
{
27-
if (is_array($obj)) {
28-
$output = [];
26+
$output = [];
2927

30-
foreach ($obj as $item) {
31-
$output[] = format($item);
32-
}
28+
foreach ($obj->getTables() as $table) {
29+
$output[$table->getName()] = formatTable($table);
30+
}
31+
32+
return $output;
33+
}
34+
35+
36+
/**
37+
* @return array<string>
38+
*/
39+
function formatTable(SqlSchema\Table $obj): array
40+
{
41+
$output = [];
3342

34-
return $output;
43+
foreach ($obj->getColumns() as $column) {
44+
$output[] = formatColumn($column);
3545
}
3646

37-
if ($obj instanceof SqlSchema\Schema) {
38-
$output = [];
47+
foreach ($obj->getIndexes() as $index) {
48+
$output[] = formatIndex($index);
49+
}
3950

40-
foreach ($obj->getTables() as $table) {
41-
$output[$table->getName()] = format($table);
42-
}
51+
return $output;
52+
}
4353

44-
return $output;
4554

46-
} elseif ($obj instanceof SqlSchema\Table) {
47-
$output = [];
55+
function formatColumn(SqlSchema\Column $obj): string
56+
{
57+
$output = $obj->getName();
58+
$output .= ' ' . $obj->getType();
59+
$parameters = $obj->getParameters();
4860

49-
foreach ($obj->getColumns() as $column) {
50-
$output[] = format($column);
51-
}
61+
if (!empty($parameters)) {
62+
$output .= '(' . implode(', ', $parameters) . ')';
63+
}
64+
65+
foreach ($obj->getOptions() as $option => $value) {
66+
$output .= ' ' . $option;
5267

53-
foreach ($obj->getIndexes() as $index) {
54-
$output[] = format($index);
68+
if (isset($value)) {
69+
$output .= ' = ' . $value;
5570
}
71+
}
5672

57-
return $output;
73+
$output .= $obj->isNullable() ? ' NULL' : ' NOT NULL';
74+
$output .= $obj->isAutoIncrement() ? ' AUTO_INCREMENT' : '';
5875

59-
} elseif ($obj instanceof SqlSchema\Column) {
60-
$output = $obj->getName();
61-
$output .= ' ' . $obj->getType();
62-
$parameters = $obj->getParameters();
76+
$comment = $obj->getComment();
77+
$output .= isset($comment) ? (' COMMENT ' . $comment) : '';
78+
return $output;
79+
}
6380

64-
if (!empty($parameters)) {
65-
$output .= '(' . implode(', ', $parameters) . ')';
66-
}
6781

68-
foreach ($obj->getOptions() as $option => $value) {
69-
$output .= ' ' . $option;
82+
/**
83+
* @param array<SqlSchema\Column> $objs
84+
* @return list<string>
85+
*/
86+
function formatColumns(array $objs): array
87+
{
88+
$res = [];
7089

71-
if (isset($value)) {
72-
$output .= ' = ' . $value;
73-
}
74-
}
90+
foreach ($objs as $obj) {
91+
$res[] = formatColumn($obj);
92+
}
7593

76-
$output .= $obj->isNullable() ? ' NULL' : ' NOT NULL';
77-
$output .= $obj->isAutoIncrement() ? ' AUTO_INCREMENT' : '';
94+
return $res;
95+
}
7896

79-
$comment = $obj->getComment();
80-
$output .= isset($comment) ? (' COMMENT ' . $comment) : '';
81-
return $output;
8297

83-
} elseif ($obj instanceof SqlSchema\Index) {
84-
$output = $obj->getType();
85-
$output .= ' ' . $obj->getName() . ' (';
86-
$first = TRUE;
98+
function formatIndex(SqlSchema\Index $obj): string
99+
{
100+
$output = $obj->getType();
101+
$output .= ' ' . $obj->getName() . ' (';
102+
$first = TRUE;
87103

88-
foreach ($obj->getColumns() as $column) {
89-
if (!$first) {
90-
$output .= ', ';
91-
}
92-
$output .= format($column);
93-
$first = FALSE;
104+
foreach ($obj->getColumns() as $column) {
105+
if (!$first) {
106+
$output .= ', ';
94107
}
108+
$output .= formatIndexColumn($column);
109+
$first = FALSE;
110+
}
95111

96-
$output .= ')';
97-
return $output;
98-
99-
} elseif ($obj instanceof SqlSchema\IndexColumn) {
100-
$output = $obj->getName();
101-
$length = $obj->getLength();
102-
$output .= isset($length) ? '(' . $length . ')' : '';
103-
$output .= ' ' . $obj->getOrder();
104-
return $output;
105-
106-
} elseif ($obj instanceof SqlSchema\ForeignKey) {
107-
$output = 'CONSTRAINT ' . $obj->getName();
108-
$output .= ' FOREIGN KEY (';
109-
$first = TRUE;
110-
111-
foreach ($obj->getColumns() as $column) {
112-
if (!$first) {
113-
$output .= ', ';
114-
}
115-
$output .= $column;
116-
$first = FALSE;
112+
$output .= ')';
113+
return $output;
114+
}
115+
116+
117+
function formatIndexColumn(SqlSchema\IndexColumn $obj): string
118+
{
119+
$output = $obj->getName();
120+
$length = $obj->getLength();
121+
$output .= isset($length) ? '(' . $length . ')' : '';
122+
$output .= ' ' . $obj->getOrder();
123+
return $output;
124+
}
125+
126+
127+
/**
128+
* @param array<SqlSchema\IndexColumn> $objs
129+
* @return list<string>
130+
*/
131+
function formatIndexColumns(array $objs): array
132+
{
133+
$res = [];
134+
135+
foreach ($objs as $obj) {
136+
$res[] = formatIndexColumn($obj);
137+
}
138+
139+
return $res;
140+
}
141+
142+
143+
144+
function formatForeignKey(SqlSchema\ForeignKey $obj): string
145+
{
146+
$output = 'CONSTRAINT ' . $obj->getName();
147+
$output .= ' FOREIGN KEY (';
148+
$first = TRUE;
149+
150+
foreach ($obj->getColumns() as $column) {
151+
if (!$first) {
152+
$output .= ', ';
117153
}
154+
$output .= $column;
155+
$first = FALSE;
156+
}
118157

119-
$output .= ') REFERENCES ' . $obj->getTargetTable() . ' (';
120-
$first = TRUE;
158+
$output .= ') REFERENCES ' . $obj->getTargetTable() . ' (';
159+
$first = TRUE;
121160

122-
foreach ($obj->getTargetColumns() as $targetColumn) {
123-
if (!$first) {
124-
$output .= ', ';
125-
}
126-
$output .= $targetColumn;
127-
$first = FALSE;
161+
foreach ($obj->getTargetColumns() as $targetColumn) {
162+
if (!$first) {
163+
$output .= ', ';
128164
}
165+
$output .= $targetColumn;
166+
$first = FALSE;
167+
}
168+
169+
$output .= ')';
170+
return $output;
171+
}
172+
173+
174+
/**
175+
* @param array<SqlSchema\ForeignKey> $objs
176+
* @return list<string>
177+
*/
178+
function formatForeignKeys(array $objs): array
179+
{
180+
$res = [];
129181

130-
$output .= ')';
131-
return $output;
182+
foreach ($objs as $obj) {
183+
$res[] = formatForeignKey($obj);
132184
}
133185

134-
throw new Exception("Unknow object " . get_class($obj));
186+
return $res;
135187
}

0 commit comments

Comments
 (0)