Repository navigation
Expand file tree
/
Copy pathSchemaBuilderTest.php
More file actions
277 lines (236 loc) · 10 KB
/
Copy pathSchemaBuilderTest.php
File metadata and controls
277 lines (236 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Database;
use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\Database\Model\Register;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Schema\Schema;
use Hyperf\DbConnection\Db;
use HyperfTest\Database\Stubs\ContainerStub;
use PHPUnit\Framework\TestCase;
use function Hyperf\Collection\collect;
/**
* @internal
* @coversNothing
*/
class SchemaBuilderTest extends TestCase
{
protected function setUp(): void
{
$container = ContainerStub::getContainer();
$db = new Db($container);
$container->allows('get')->with(Db::class)->andReturns($db);
$connectionResolverInterface = $container->get(ConnectionResolverInterface::class);
Register::setConnectionResolver($connectionResolverInterface);
}
protected function tearDown(): void
{
Register::unsetConnectionResolver();
}
public function testGetTables(): void
{
Schema::create('foo', static function (Blueprint $table) {
$table->comment('This is a comment');
$table->increments('id');
});
Schema::create('bar', static function (Blueprint $table) {
$table->string('name');
});
Schema::create('baz', static function (Blueprint $table) {
$table->integer('votes');
});
$tables = Schema::getTables();
$this->assertEmpty(array_diff(['foo', 'bar', 'baz'], array_column($tables, 'name')));
$this->assertNotEmpty(array_filter($tables, static function ($table) {
return $table['name'] === 'foo' && $table['comment'] === 'This is a comment';
}));
Schema::drop('foo');
Schema::drop('bar');
Schema::drop('baz');
}
public function testGetForeignKeys()
{
Schema::create('users_copy', function (Blueprint $table) {
$table->id();
});
Schema::create('posts_copy', function (Blueprint $table) {
$table->foreignId('user_id')->nullable()->constrained('users_copy')->cascadeOnUpdate()->nullOnDelete();
});
$foreignKeys = Schema::getForeignKeys('posts_copy');
$this->assertCount(1, $foreignKeys);
$this->assertTrue(collect($foreignKeys)->contains(
fn ($foreign) => $foreign['columns'] === ['user_id']
&& $foreign['foreign_table'] === 'users_copy' && $foreign['foreign_columns'] === ['id']
&& $foreign['on_update'] === 'cascade' && $foreign['on_delete'] === 'set null'
));
Schema::drop('posts_copy');
Schema::drop('users_copy');
}
public function testGetCompoundForeignKeys()
{
Schema::create('tb_parent', function (Blueprint $table) {
$table->id();
$table->integer('a');
$table->integer('b');
$table->unique(['b', 'a']);
});
Schema::create('tb_child', function (Blueprint $table) {
$table->integer('c');
$table->integer('d');
$table->foreign(['d', 'c'], 'test_fk')->references(['b', 'a'])->on('tb_parent');
});
$foreignKeys = Schema::getForeignKeys('tb_child');
$this->assertCount(1, $foreignKeys);
$this->assertTrue(collect($foreignKeys)->contains(
fn ($foreign) => $foreign['columns'] === ['d', 'c']
&& $foreign['foreign_table'] === 'tb_parent'
&& $foreign['foreign_columns'] === ['b', 'a']
));
Schema::drop('tb_child');
Schema::drop('tb_parent');
}
public function testWhenTableHasColumn(): void
{
Schema::create('foo', static function (Blueprint $table) {
$table->comment('This is a comment');
$table->increments('id');
});
Schema::whenTableDoesntHaveColumn('foo', 'name', static function (Blueprint $table) {
$table->string('name');
});
$this->assertTrue(Schema::hasColumn('foo', 'name'));
Schema::whenTableHasColumn('foo', 'name', static function (Blueprint $table) {
$table->dropColumn('name');
});
$this->assertFalse(Schema::hasColumn('foo', 'name'));
Schema::drop('foo');
}
public function testViews(): void
{
Schema::create('view_1', static function (Blueprint $table) {
$table->comment('This is a comment');
$table->increments('id');
});
Db::table('view_1')->insert(['id' => 1]);
Db::statement('create view demo1 as select id from view_1');
$this->assertNotEmpty(array_filter(Schema::getViews(), function ($table) {
return $table['name'] === 'demo1';
}));
$this->assertTrue(Schema::hasView('demo1'));
Schema::dropAllViews();
$this->assertFalse(Schema::hasView('demo1'));
$this->assertEmpty(array_filter(Schema::getViews(), function ($table) {
return $table['name'] === 'demo1';
}));
Schema::drop('view_1');
}
public function testColumn(): void
{
Schema::create('column_1', static function (Blueprint $table) {
$table->comment('This is a comment');
$table->increments('id');
$table->string('name');
$table->integer('ranking');
});
$this->assertTrue(Schema::hasColumn('column_1', 'name'));
$this->assertTrue(Schema::hasColumn('column_1', 'naMe'));
$this->assertFalse(Schema::hasColumn('column_1', 'names'));
$this->assertTrue(Schema::hasColumn('column_1', 'ranking'));
$this->assertFalse(Schema::hasColumn('column_1', 'rankings'));
$this->assertTrue(Schema::hasColumns('column_1', ['name', 'ranking']));
$this->assertFalse(Schema::hasColumns('column_1', ['names', 'ranking']));
$this->assertSame('string', Schema::getColumnType('column_1', 'name'));
$this->assertSame('integer', Schema::getColumnType('column_1', 'ranking'));
$columns = Schema::getColumnTypeListing('column_1');
$this->assertSame(['id', 'name', 'ranking'], array_column($columns, 'column_name'));
Schema::drop('column_1');
}
public function index(): void
{
Schema::create('users', function (Blueprint $table) {
$table->string('name');
$table->string('email');
});
Schema::table('users', function (Blueprint $table) {
$table->index(['name', 'email'], 'index1');
});
$indexes = Schema::getIndexListing('users');
$this->assertContains('index1', $indexes);
$this->assertNotContains('index2', $indexes);
Schema::table('users', function (Blueprint $table) {
$table->renameIndex('index1', 'index2');
});
$this->assertFalse(Schema::hasIndex('users', 'index1'));
$this->assertTrue(collect(Schema::getIndexes('users'))->contains(
fn ($index) => $index['name'] === 'index2' && $index['columns'] === ['name', 'email']
));
Schema::create('foo', function (Blueprint $table) {
$table->id();
$table->string('bar');
$table->integer('baz');
$table->unique(['baz', 'bar']);
});
$indexes = Schema::getIndexes('foo');
$this->assertCount(2, $indexes);
$this->assertTrue(collect($indexes)->contains(
fn ($index) => $index['columns'] === ['id'] && $index['primary']
));
$this->assertTrue(collect($indexes)->contains(
fn ($index) => $index['name'] === 'foo_baz_bar_unique' && $index['columns'] === ['baz', 'bar'] && $index['unique']
));
$this->assertTrue(Schema::hasIndex('foo', 'foo_baz_bar_unique'));
$this->assertTrue(Schema::hasIndex('foo', 'foo_baz_bar_unique', 'unique'));
$this->assertTrue(Schema::hasIndex('foo', ['baz', 'bar']));
$this->assertTrue(Schema::hasIndex('foo', ['baz', 'bar'], 'unique'));
$this->assertFalse(Schema::hasIndex('foo', ['baz', 'bar'], 'primary'));
Schema::drop('foo');
Schema::create('foo', function (Blueprint $table) {
$table->string('bar')->index('my_index');
});
$indexes = Schema::getIndexes('foo');
$this->assertCount(1, $indexes);
$this->assertTrue(
$indexes[0]['name'] === 'my_index'
&& $indexes[0]['columns'] === ['bar']
&& ! $indexes[0]['unique']
&& ! $indexes[0]['primary']
);
$this->assertTrue(Schema::hasIndex('foo', 'my_index'));
$this->assertTrue(Schema::hasIndex('foo', ['bar']));
$this->assertFalse(Schema::hasIndex('foo', 'my_index', 'primary'));
$this->assertFalse(Schema::hasIndex('foo', ['bar'], 'unique'));
Schema::drop('foo');
Schema::create('foo', function (Blueprint $table) {
$table->unsignedBigInteger('key');
$table->string('bar')->unique();
$table->integer('baz');
$table->primary(['baz', 'key']);
});
$indexes = Schema::getIndexes('foo');
$this->assertCount(2, $indexes);
$this->assertTrue(collect($indexes)->contains(
fn ($index) => $index['columns'] === ['baz', 'key'] && $index['primary']
));
$this->assertTrue(collect($indexes)->contains(
fn ($index) => $index['name'] === 'foo_bar_unique' && $index['columns'] === ['bar'] && $index['unique']
));
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('title', 200);
$table->text('body');
$table->fulltext(['body', 'title']);
});
$indexes = Schema::getIndexes('articles');
$this->assertCount(2, $indexes);
$this->assertTrue(collect($indexes)->contains(fn ($index) => $index['columns'] === ['id'] && $index['primary']));
$this->assertTrue(collect($indexes)->contains('name', 'articles_body_title_fulltext'));
}
}