-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathUpdateQueryBuilderTest.php
More file actions
350 lines (297 loc) · 9.12 KB
/
UpdateQueryBuilderTest.php
File metadata and controls
350 lines (297 loc) · 9.12 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
namespace Tests\Tempest\Integration\Database\Builder;
use Tempest\Database\Builder\QueryBuilders\UpdateQueryBuilder;
use Tempest\Database\Config\DatabaseDialect;
use Tempest\Database\Database;
use Tempest\Database\Exceptions\HasManyRelationCouldNotBeUpdated;
use Tempest\Database\Exceptions\HasOneRelationCouldNotBeUpdated;
use Tempest\Database\Exceptions\UpdateStatementWasInvalid;
use Tempest\Database\Id;
use Tempest\Database\Migrations\CreateMigrationsTable;
use Tempest\Database\Query;
use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable;
use Tests\Tempest\Fixtures\Migrations\CreatePublishersTable;
use Tests\Tempest\Fixtures\Modules\Books\Models\Author;
use Tests\Tempest\Fixtures\Modules\Books\Models\AuthorType;
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use function Tempest\Database\query;
final class UpdateQueryBuilderTest extends FrameworkIntegrationTestCase
{
public function test_update_on_plain_table(): void
{
$query = query('chapters')
->update(
title: 'Chapter 01',
index: 1,
)
->where('`id` = ?', 10)
->build();
$this->assertSameWithoutBackticks(
<<<SQL
UPDATE `chapters`
SET `title` = ?, `index` = ?
WHERE `id` = ?
SQL,
$query->toSql(),
);
$this->assertSame(
['Chapter 01', 1, 10],
$query->bindings,
);
}
public function test_global_update(): void
{
$query = query('chapters')
->update(index: 0)
->allowAll()
->build();
$this->assertSameWithoutBackticks(
<<<SQL
UPDATE `chapters`
SET `index` = ?
SQL,
$query->toSql(),
);
$this->assertSame(
[0],
$query->bindings,
);
}
public function test_global_update_fails_without_allow_all(): void
{
$this->expectException(UpdateStatementWasInvalid::class);
query('chapters')
->update(index: 0)
->build()
->toSql();
}
public function test_model_update_with_values(): void
{
$query = query(Book::class)
->update(
title: 'Chapter 02',
)
->where('`id` = ?', 10)
->build();
$this->assertSameWithoutBackticks(
<<<SQL
UPDATE `books`
SET `title` = ?
WHERE `id` = ?
SQL,
$query->toSql(),
);
$this->assertSame(
['Chapter 02', 10],
$query->bindings,
);
}
public function test_model_update_with_object(): void
{
$book = Book::new(
id: new Id(10),
title: 'Chapter 01',
);
$query = query($book)
->update(
title: 'Chapter 02',
)
->build();
$this->assertSameWithoutBackticks(
<<<SQL
UPDATE `books`
SET `title` = ?
WHERE `books`.`id` = ?
SQL,
$query->toSql(),
);
$this->assertSame(
['Chapter 02', 10],
$query->bindings,
);
}
public function test_model_values_get_serialized(): void
{
$author = Author::new(
id: new Id(10),
);
$query = query($author)
->update(
type: AuthorType::A,
)
->build();
$this->assertSame(
['a', 10],
$query->bindings,
);
}
public function test_insert_new_relation_on_update(): void
{
$book = Book::new(
id: new Id(10),
);
$bookQuery = query($book)
->update(author: Author::new(name: 'Brent'))
->build();
$this->assertSameWithoutBackticks(
<<<SQL
UPDATE `books`
SET `author_id` = ?
WHERE `books`.`id` = ?
SQL,
$bookQuery->toSql(),
);
$this->assertInstanceOf(Query::class, $bookQuery->bindings[0]);
$authorQuery = $bookQuery->bindings[0];
$expected = <<<SQL
INSERT INTO `authors` (`name`)
VALUES (?)
SQL;
if ($this->container->get(Database::class)->dialect === DatabaseDialect::POSTGRESQL) {
$expected .= ' RETURNING *';
}
$this->assertSameWithoutBackticks(
$expected,
$authorQuery->toSql(),
);
$this->assertSame(['Brent'], $authorQuery->bindings);
}
public function test_attach_existing_relation_on_update(): void
{
$book = Book::new(
id: new Id(10),
);
$bookQuery = query($book)
->update(author: Author::new(id: new Id(5), name: 'Brent'))
->build();
$this->assertSameWithoutBackticks(
<<<SQL
UPDATE `books`
SET `author_id` = ?
WHERE `books`.`id` = ?
SQL,
$bookQuery->toSql(),
);
$this->assertSame([5, 10], $bookQuery->bindings);
}
public function test_update_has_many_relation_via_parent_model_throws_exception(): void
{
try {
query(Book::class)
->update(
title: 'Timeline Taxi',
chapters: ['title' => 'Chapter 01'],
)
->build();
} catch (HasManyRelationCouldNotBeUpdated $hasManyRelationCouldNotBeUpdated) {
$this->assertStringContainsString(Book::class . '::$chapters', $hasManyRelationCouldNotBeUpdated->getMessage());
}
}
public function test_update_has_one_relation_via_parent_model_throws_exception(): void
{
try {
query(Book::class)
->update(
title: 'Timeline Taxi',
isbn: ['value' => '979-8344313764'],
)
->build();
} catch (HasOneRelationCouldNotBeUpdated $hasOneRelationCouldNotBeUpdated) {
$this->assertStringContainsString(Book::class . '::$isbn', $hasOneRelationCouldNotBeUpdated->getMessage());
}
}
public function test_update_on_plain_table_with_conditions(): void
{
$query = query('chapters')
->update(
title: 'Chapter 01',
index: 1,
)
->when(
true,
fn (UpdateQueryBuilder $query) => $query->where('`id` = ?', 10),
)
->when(
false,
fn (UpdateQueryBuilder $query) => $query->where('`id` = ?', 20),
)
->build();
$this->assertSameWithoutBackticks(
<<<SQL
UPDATE `chapters`
SET `title` = ?, `index` = ?
WHERE `id` = ?
SQL,
$query->toSql(),
);
$this->assertSame(
['Chapter 01', 1, 10],
$query->bindings,
);
}
public function test_update_with_non_object_model(): void
{
$this->migrate(CreateMigrationsTable::class, CreatePublishersTable::class, CreateAuthorTable::class);
query('authors')->insert(
['id' => 1, 'name' => 'Brent'],
['id' => 2, 'name' => 'Other'],
)->execute();
query('authors')->update(
name: 'Brendt',
)->where('id = ?', 1)->execute();
$count = query('authors')->count()->where('name = ?', 'Brendt')->execute();
$this->assertSame(1, $count);
}
public function test_multiple_where(): void
{
$sql = query('books')
->update(
title: 'Timeline Taxi',
)
->where('title = ?', 'a')
->where('author_id = ?', 1)
->where('OR author_id = ?', 2)
->where('AND author_id <> NULL')
->toSql();
$expected = <<<SQL
UPDATE `books`
SET title = ?
WHERE title = ?
AND author_id = ?
OR author_id = ?
AND author_id <> NULL
SQL;
$this->assertSameWithoutBackticks($expected, $sql);
}
public function test_multiple_where_field(): void
{
$sql = query('books')
->update(
title: 'Timeline Taxi',
)
->whereField('title', 'a')
->whereField('author_id', 1)
->toSql();
$expected = <<<SQL
UPDATE `books`
SET title = ?
WHERE books.title = ?
AND books.author_id = ?
SQL;
$this->assertSameWithoutBackticks($expected, $sql);
}
public function test_tap(): void
{
$query = query(Book::class)
->update(title: 'Chapter 02')
->tap(fn (UpdateQueryBuilder $query) => $query->where('id = ?', 10))
->build();
$this->assertSameWithoutBackticks(<<<SQL
UPDATE `books`
SET `title` = ?
WHERE `id` = ?
SQL, $query->toSql());
$this->assertSame(['Chapter 02', 10], $query->bindings);
}
}