-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathCreateTableStatementTest.php
More file actions
240 lines (211 loc) · 7.48 KB
/
CreateTableStatementTest.php
File metadata and controls
240 lines (211 loc) · 7.48 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
<?php
declare(strict_types=1);
namespace Tempest\Database\Tests\QueryStatements;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Tempest\Database\Config\DatabaseDialect;
use Tempest\Database\QueryStatements\CreateTableStatement;
use Tempest\Database\QueryStatements\OnDelete;
use Tempest\Database\QueryStatements\PrimaryKeyStatement;
use Tempest\Database\QueryStatements\RawStatement;
/**
* @internal
*/
final class CreateTableStatementTest extends TestCase
{
public function test_integer_table_database_dialect(): void
{
}
#[DataProvider('provide_create_table_database_dialects')]
public function test_create_a_table(DatabaseDialect $dialect, string $validSql): void
{
$statement = new CreateTableStatement('migrations', [
new PrimaryKeyStatement(),
new RawStatement('`name` VARCHAR(255) NOT NULL'),
])->compile($dialect);
$this->assertSame($validSql, $statement);
}
public static function provide_create_table_database_dialects(): iterable
{
yield 'mysql' => [
DatabaseDialect::MYSQL,
<<<SQL
CREATE TABLE `migrations` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL
);
SQL,
];
yield 'postgresql' => [
DatabaseDialect::POSTGRESQL,
<<<SQL
CREATE TABLE `migrations` (
`id` SERIAL PRIMARY KEY,
`name` VARCHAR(255) NOT NULL
);
SQL,
];
yield 'sqlite' => [
DatabaseDialect::SQLITE,
<<<SQL
CREATE TABLE `migrations` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` VARCHAR(255) NOT NULL
);
SQL,
];
}
#[DataProvider('provide_fk_create_table_database_drivers')]
public function test_create_a_foreign_key_constraint(DatabaseDialect $dialect, string $validSql): void
{
$statement = new CreateTableStatement('books')
->primary()
->belongsTo('books.author_id', 'authors.id', OnDelete::CASCADE)
->varchar('name')
->compile($dialect);
$this->assertSame($validSql, $statement);
$statement = new CreateTableStatement('books')
->primary()
->foreignId('author_id', constrainedOn: 'authors', onDelete: OnDelete::CASCADE)
->varchar('name')
->compile($dialect);
$this->assertSame($validSql, $statement);
$statement = new CreateTableStatement('books')
->primary()
->foreignId('books.author_id', constrainedOn: 'authors.id', onDelete: OnDelete::CASCADE)
->varchar('name')
->compile($dialect);
$this->assertSame($validSql, $statement);
}
public static function provide_fk_create_table_database_drivers(): Generator
{
yield 'mysql' => [
DatabaseDialect::MYSQL,
<<<SQL
CREATE TABLE `books` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`author_id` INTEGER NOT NULL,
CONSTRAINT `fk_authors_books_author_id` FOREIGN KEY books(author_id) REFERENCES authors(id) ON DELETE CASCADE ON UPDATE NO ACTION,
`name` VARCHAR(255) NOT NULL
);
SQL,
];
yield 'postgresql' => [
DatabaseDialect::POSTGRESQL,
<<<SQL
CREATE TABLE `books` (
`id` SERIAL PRIMARY KEY,
`author_id` INTEGER NOT NULL,
CONSTRAINT `fk_authors_books_author_id` FOREIGN KEY(author_id) REFERENCES authors(id) ON DELETE CASCADE ON UPDATE NO ACTION,
`name` VARCHAR(255) NOT NULL
);
SQL,
];
yield 'sqlite' => [
DatabaseDialect::SQLITE,
<<<SQL
CREATE TABLE `books` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`author_id` INTEGER NOT NULL,
`name` VARCHAR(255) NOT NULL
);
SQL,
];
}
#[DataProvider('provide_fk_create_table_database_drivers_explicit')]
public function test_create_a_foreign_key_constraint_with_explicit_column(DatabaseDialect $dialect, string $validSql): void
{
$statement = new CreateTableStatement('books')
->primary()
->integer('author_id')
->foreignKey('books.author_id', 'authors.id', OnDelete::CASCADE)
->varchar('name')
->compile($dialect);
$this->assertSame($validSql, $statement);
}
public static function provide_fk_create_table_database_drivers_explicit(): Generator
{
yield 'mysql' => [
DatabaseDialect::MYSQL,
<<<SQL
CREATE TABLE `books` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`author_id` INTEGER NOT NULL,
CONSTRAINT `fk_authors_books_author_id` FOREIGN KEY books(author_id) REFERENCES authors(id) ON DELETE CASCADE ON UPDATE NO ACTION,
`name` VARCHAR(255) NOT NULL
);
SQL,
];
yield 'postgresql' => [
DatabaseDialect::POSTGRESQL,
<<<SQL
CREATE TABLE `books` (
`id` SERIAL PRIMARY KEY,
`author_id` INTEGER NOT NULL,
CONSTRAINT `fk_authors_books_author_id` FOREIGN KEY(author_id) REFERENCES authors(id) ON DELETE CASCADE ON UPDATE NO ACTION,
`name` VARCHAR(255) NOT NULL
);
SQL,
];
yield 'sqlite' => [
DatabaseDialect::SQLITE,
<<<SQL
CREATE TABLE `books` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`author_id` INTEGER NOT NULL,
`name` VARCHAR(255) NOT NULL
);
SQL,
];
}
#[DataProvider('provide_uuid_primary_database_dialects')]
public function test_create_table_with_uuid_primary(DatabaseDialect $dialect, string $validSql): void
{
$uuid = new CreateTableStatement('users')
->uuid('uuid')
->text('name')
->text('email')
->compile($dialect);
$primary = new CreateTableStatement('users')
->primary('uuid', uuid: true)
->text('name')
->text('email')
->compile($dialect);
$this->assertSame($validSql, $uuid);
$this->assertSame($validSql, $primary);
}
public static function provide_uuid_primary_database_dialects(): iterable
{
yield 'mysql' => [
DatabaseDialect::MYSQL,
<<<SQL
CREATE TABLE `users` (
`uuid` VARCHAR(36) PRIMARY KEY,
`name` TEXT NOT NULL,
`email` TEXT NOT NULL
);
SQL,
];
yield 'postgresql' => [
DatabaseDialect::POSTGRESQL,
<<<SQL
CREATE TABLE `users` (
`uuid` UUID PRIMARY KEY,
`name` TEXT NOT NULL,
`email` TEXT NOT NULL
);
SQL,
];
yield 'sqlite' => [
DatabaseDialect::SQLITE,
<<<SQL
CREATE TABLE `users` (
`uuid` TEXT PRIMARY KEY,
`name` TEXT NOT NULL,
`email` TEXT NOT NULL
);
SQL,
];
}
}