1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
9
|
|
|
use Yiisoft\Db\Schema\ColumnSchema; |
10
|
|
|
use Yiisoft\Db\Tests\Support\Stub\TableSchema; |
11
|
|
|
use Yiisoft\Db\Tests\Support\TestTrait; |
12
|
|
|
|
13
|
|
|
abstract class AbstractTableSchemaTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
use TestTrait; |
16
|
|
|
|
17
|
|
|
public function testCompositeFk(): void |
18
|
|
|
{ |
19
|
|
|
$tableSchema = new TableSchema(); |
20
|
|
|
|
21
|
|
|
$this->expectException(NotSupportedException::class); |
22
|
|
|
$this->expectExceptionMessage('Yiisoft\Db\Tests\Support\Stub\TableSchema does not support composite FK.'); |
23
|
|
|
|
24
|
|
|
$tableSchema->compositeFk(1, 'from', 'to'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testGetCatalogName(): void |
28
|
|
|
{ |
29
|
|
|
$tableSchema = new TableSchema(); |
30
|
|
|
|
31
|
|
|
$this->assertNull($tableSchema->getCatalogName()); |
32
|
|
|
|
33
|
|
|
$tableSchema->catalogName('test'); |
34
|
|
|
|
35
|
|
|
$this->assertSame('test', $tableSchema->getCatalogName()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetComment(): void |
39
|
|
|
{ |
40
|
|
|
$tableSchema = new TableSchema(); |
41
|
|
|
|
42
|
|
|
$this->assertNull($tableSchema->getComment()); |
43
|
|
|
|
44
|
|
|
$tableSchema->comment('test'); |
45
|
|
|
|
46
|
|
|
$this->assertSame('test', $tableSchema->getComment()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testGetColumn(): void |
50
|
|
|
{ |
51
|
|
|
// Defined column schema. |
52
|
|
|
$columnSchema = new ColumnSchema(); |
53
|
|
|
$columnSchema->name('id'); |
54
|
|
|
|
55
|
|
|
// Create table schema. |
56
|
|
|
$tableSchema = new TableSchema(); |
57
|
|
|
|
58
|
|
|
$this->assertNull($tableSchema->getColumn('id')); |
59
|
|
|
|
60
|
|
|
$tableSchema->columns('id', $columnSchema); |
61
|
|
|
|
62
|
|
|
$this->assertSame($columnSchema, $tableSchema->getColumn('id')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testGetColumns(): void |
66
|
|
|
{ |
67
|
|
|
// Defined column schema. |
68
|
|
|
$columnSchema = new ColumnSchema(); |
69
|
|
|
$columnSchema->name('id'); |
70
|
|
|
|
71
|
|
|
// Create table schema. |
72
|
|
|
$tableSchema = new TableSchema(); |
73
|
|
|
|
74
|
|
|
$this->assertSame([], $tableSchema->getColumns()); |
75
|
|
|
|
76
|
|
|
$tableSchema->columns('id', $columnSchema); |
77
|
|
|
|
78
|
|
|
$this->assertSame(['id' => $columnSchema], $tableSchema->getColumns()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testGetColumnName(): void |
82
|
|
|
{ |
83
|
|
|
// Defined column schema. |
84
|
|
|
$columnSchema = new ColumnSchema(); |
85
|
|
|
$columnSchema->name('id'); |
86
|
|
|
|
87
|
|
|
// Create table schema. |
88
|
|
|
$tableSchema = new TableSchema(); |
89
|
|
|
|
90
|
|
|
$this->assertNull($tableSchema->getColumn('id')); |
91
|
|
|
|
92
|
|
|
$tableSchema->columns('id', $columnSchema); |
93
|
|
|
|
94
|
|
|
$this->assertSame(['id'], $tableSchema->getColumnNames()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testGetCreateSql(): void |
98
|
|
|
{ |
99
|
|
|
$tableSchema = new TableSchema(); |
100
|
|
|
|
101
|
|
|
$this->assertNull($tableSchema->getCreateSql()); |
102
|
|
|
|
103
|
|
|
$tableSchema->createSql( |
104
|
|
|
<<<SQL |
105
|
|
|
CREATE TABLE `test` (`id` int(11) NOT NULL) |
106
|
|
|
SQL, |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$this->assertSame( |
110
|
|
|
<<<SQL |
111
|
|
|
CREATE TABLE `test` (`id` int(11) NOT NULL) |
112
|
|
|
SQL, |
113
|
|
|
$tableSchema->getCreateSql(), |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testGetForeignKeys(): void |
118
|
|
|
{ |
119
|
|
|
$tableSchema = new TableSchema(); |
120
|
|
|
|
121
|
|
|
$this->assertSame([], $tableSchema->getForeignKeys()); |
122
|
|
|
|
123
|
|
|
$tableSchema->foreignKeys(['id']); |
124
|
|
|
|
125
|
|
|
$this->assertSame(['id'], $tableSchema->getForeignKeys()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGetForeignKeysAndForeingKey(): void |
129
|
|
|
{ |
130
|
|
|
$tableSchema = new TableSchema(); |
131
|
|
|
|
132
|
|
|
$this->assertSame([], $tableSchema->getForeignKeys()); |
133
|
|
|
|
134
|
|
|
$tableSchema->foreignKey('id', ['test', 'id']); |
135
|
|
|
|
136
|
|
|
$this->assertSame(['id' => ['test', 'id']], $tableSchema->getForeignKeys()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testGetFullName(): void |
140
|
|
|
{ |
141
|
|
|
$tableSchema = new TableSchema(); |
142
|
|
|
|
143
|
|
|
$this->assertEmpty($tableSchema->getFullName()); |
144
|
|
|
|
145
|
|
|
$tableSchema->fullName('test'); |
146
|
|
|
|
147
|
|
|
$this->assertSame('test', $tableSchema->getFullName()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testGetName(): void |
151
|
|
|
{ |
152
|
|
|
$tableSchema = new TableSchema(); |
153
|
|
|
|
154
|
|
|
$this->assertEmpty($tableSchema->getName()); |
155
|
|
|
|
156
|
|
|
$tableSchema->name('test'); |
157
|
|
|
|
158
|
|
|
$this->assertSame('test', $tableSchema->getName()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testGetPrimaryKey(): void |
162
|
|
|
{ |
163
|
|
|
$tableSchema = new TableSchema(); |
164
|
|
|
|
165
|
|
|
$this->assertSame([], $tableSchema->getPrimaryKey()); |
166
|
|
|
|
167
|
|
|
$tableSchema->primaryKey('id'); |
168
|
|
|
|
169
|
|
|
$this->assertSame(['id'], $tableSchema->getPrimaryKey()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testGetSequencName(): void |
173
|
|
|
{ |
174
|
|
|
$tableSchema = new TableSchema(); |
175
|
|
|
|
176
|
|
|
$this->assertEmpty($tableSchema->getSequenceName()); |
177
|
|
|
|
178
|
|
|
$tableSchema->sequenceName('test'); |
179
|
|
|
|
180
|
|
|
$this->assertSame('test', $tableSchema->getSequenceName()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testGetServerName(): void |
184
|
|
|
{ |
185
|
|
|
$tableSchema = new TableSchema(); |
186
|
|
|
|
187
|
|
|
$this->assertEmpty($tableSchema->getServerName()); |
188
|
|
|
|
189
|
|
|
$tableSchema->serverName('test'); |
190
|
|
|
|
191
|
|
|
$this->assertSame('test', $tableSchema->getServerName()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testGetSchemaName(): void |
195
|
|
|
{ |
196
|
|
|
$tableSchema = new TableSchema(); |
197
|
|
|
|
198
|
|
|
$this->assertNull($tableSchema->getSchemaName()); |
199
|
|
|
|
200
|
|
|
$tableSchema->schemaName('test'); |
201
|
|
|
|
202
|
|
|
$this->assertSame('test', $tableSchema->getSchemaName()); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|