-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathUpdateDataSQLTest.php
More file actions
146 lines (112 loc) · 5.07 KB
/
UpdateDataSQLTest.php
File metadata and controls
146 lines (112 loc) · 5.07 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
<?php
namespace DBDiff\Tests\Unit;
use PHPUnit\Framework\TestCase;
use DBDiff\SQLGen\DiffToSQL\UpdateDataSQL;
use DBDiff\SQLGen\Dialect\MySQLDialect;
use Diff\DiffOp\DiffOpAdd;
use Diff\DiffOp\DiffOpRemove;
use Diff\DiffOp\DiffOpChange;
class UpdateDataSQLTest extends TestCase
{
private function makeObj(string $table, array $diffMap, array $keys): object
{
return (object) [
'table' => $table,
'diff' => ['diff' => $diffMap, 'keys' => $keys],
];
}
// ── getUp ─────────────────────────────────────────────────────────────
public function testGetUpWithDiffOpChange(): void
{
$obj = $this->makeObj('users', [
'name' => new DiffOpChange('old_name', 'new_name'),
], ['id' => '1']);
$sql = new UpdateDataSQL($obj, new MySQLDialect());
$result = $sql->getUp();
$this->assertSame("UPDATE `users` SET `name` = 'new_name' WHERE `id` = '1';", $result);
}
public function testGetUpWithDiffOpChangeNullNewValue(): void
{
$obj = $this->makeObj('users', [
'email' => new DiffOpChange('old@test.com', null),
], ['id' => '2']);
$sql = new UpdateDataSQL($obj, new MySQLDialect());
$result = $sql->getUp();
$this->assertSame("UPDATE `users` SET `email` = NULL WHERE `id` = '2';", $result);
}
public function testGetUpWithDiffOpRemoveDoesNotCrash(): void
{
$obj = $this->makeObj('users', [
'old_col' => new DiffOpRemove('removed_value'),
], ['id' => '3']);
$sql = new UpdateDataSQL($obj, new MySQLDialect());
$result = $sql->getUp();
// DiffOpRemove has no getNewValue(), so should produce NULL
$this->assertSame("UPDATE `users` SET `old_col` = NULL WHERE `id` = '3';", $result);
}
public function testGetUpWithDiffOpAddDoesNotCrash(): void
{
$obj = $this->makeObj('users', [
'new_col' => new DiffOpAdd('added_value'),
], ['id' => '4']);
$sql = new UpdateDataSQL($obj, new MySQLDialect());
$result = $sql->getUp();
$this->assertSame("UPDATE `users` SET `new_col` = 'added_value' WHERE `id` = '4';", $result);
}
// ── getDown ───────────────────────────────────────────────────────────
public function testGetDownWithDiffOpChange(): void
{
$obj = $this->makeObj('users', [
'name' => new DiffOpChange('old_name', 'new_name'),
], ['id' => '1']);
$sql = new UpdateDataSQL($obj, new MySQLDialect());
$result = $sql->getDown();
$this->assertSame("UPDATE `users` SET `name` = 'old_name' WHERE `id` = '1';", $result);
}
public function testGetDownWithDiffOpChangeNullOldValue(): void
{
$obj = $this->makeObj('users', [
'email' => new DiffOpChange(null, 'new@test.com'),
], ['id' => '2']);
$sql = new UpdateDataSQL($obj, new MySQLDialect());
$result = $sql->getDown();
$this->assertSame("UPDATE `users` SET `email` = NULL WHERE `id` = '2';", $result);
}
public function testGetDownWithDiffOpAddDoesNotCrash(): void
{
// DiffOpAdd has no getOldValue() — this was the crash in Bug #2
$obj = $this->makeObj('users', [
'new_col' => new DiffOpAdd('added_value'),
], ['id' => '3']);
$sql = new UpdateDataSQL($obj, new MySQLDialect());
$result = $sql->getDown();
$this->assertSame("UPDATE `users` SET `new_col` = NULL WHERE `id` = '3';", $result);
}
public function testGetDownWithDiffOpRemoveDoesNotCrash(): void
{
$obj = $this->makeObj('users', [
'old_col' => new DiffOpRemove('removed_value'),
], ['id' => '4']);
$sql = new UpdateDataSQL($obj, new MySQLDialect());
$result = $sql->getDown();
$this->assertSame("UPDATE `users` SET `old_col` = 'removed_value' WHERE `id` = '4';", $result);
}
// ── Mixed DiffOp types in a single UPDATE ─────────────────────────────
public function testMixedDiffOpTypes(): void
{
$obj = $this->makeObj('users', [
'name' => new DiffOpChange('old', 'new'),
'bio' => new DiffOpAdd('new_bio'),
'phone' => new DiffOpRemove('old_phone'),
], ['id' => '5']);
$sql = new UpdateDataSQL($obj, new MySQLDialect());
$up = $sql->getUp();
$this->assertStringContainsString("`name` = 'new'", $up);
$this->assertStringContainsString("`bio` = 'new_bio'", $up);
$this->assertStringContainsString('`phone` = NULL', $up);
$down = $sql->getDown();
$this->assertStringContainsString("`name` = 'old'", $down);
$this->assertStringContainsString('`bio` = NULL', $down);
$this->assertStringContainsString("`phone` = 'old_phone'", $down);
}
}