|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace QB\PostgreSQL\Statement; |
|
6
|
|
|
|
|
7
|
|
|
use QB\Generic\Clause\Table; |
|
8
|
|
|
use QB\Generic\Statement\InsertTest as GenericInsertTest; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
|
|
11
|
|
|
class InsertTest extends GenericInsertTest |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @suppress PhanNoopCast |
|
15
|
|
|
*/ |
|
16
|
|
|
public function testToStringThrowsAnExceptionIfNotInitialized() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->expectException(RuntimeException::class); |
|
19
|
|
|
|
|
20
|
|
|
(string)(new Insert()); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testWitDefaultValues() |
|
24
|
|
|
{ |
|
25
|
|
|
$sql = (string)$this->getSut('foo'); |
|
26
|
|
|
|
|
27
|
|
|
$parts = []; |
|
28
|
|
|
$parts[] = 'INSERT INTO foo'; |
|
29
|
|
|
$parts[] = 'DEFAULT VALUES'; |
|
30
|
|
|
|
|
31
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
|
32
|
|
|
|
|
33
|
|
|
$this->assertSame($expectedSql, $sql); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testComplex() |
|
37
|
|
|
{ |
|
38
|
|
|
$sql = (string)$this->getSut('foo') |
|
39
|
|
|
->setColumns('id', 'bar_id', 'baz') |
|
40
|
|
|
->addValues('1234', '2345', 'a') |
|
41
|
|
|
->addValues('3456', '4567', 'b'); |
|
42
|
|
|
|
|
43
|
|
|
$parts = []; |
|
44
|
|
|
$parts[] = 'INSERT INTO foo (id, bar_id, baz)'; |
|
45
|
|
|
$parts[] = 'VALUES (?, ?, ?),'; |
|
46
|
|
|
$parts[] = '(?, ?, ?)'; |
|
47
|
|
|
|
|
48
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertSame($expectedSql, $sql); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testAddMultipleRows() |
|
54
|
|
|
{ |
|
55
|
|
|
$query = $this->getSut('offices') |
|
56
|
|
|
->setInto(new Table('offices')) |
|
57
|
|
|
->setColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
|
58
|
|
|
->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA') |
|
59
|
|
|
->addValues('bcd', 'Budapest', '+36 70 101 1234', '', 'Hungary', '1011', 'NA') |
|
60
|
|
|
->addValues('cde', 'Pécs', '+36 70 222 3456', 'Rákóczi út', 'Hungary', '723', 'NA') |
|
61
|
|
|
->setReturning('*'); |
|
62
|
|
|
|
|
63
|
|
|
$parts = []; |
|
64
|
|
|
$parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)'; |
|
65
|
|
|
$parts[] = 'VALUES (?, ?, ?, ?, ?, ?, ?),'; |
|
66
|
|
|
$parts[] = '(?, ?, ?, ?, ?, ?, ?),'; |
|
67
|
|
|
$parts[] = '(?, ?, ?, ?, ?, ?, ?)'; |
|
68
|
|
|
$parts[] = 'RETURNING *'; |
|
69
|
|
|
|
|
70
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertSame($expectedSql, (string)$query); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testAddOnConflictDoNothing() |
|
76
|
|
|
{ |
|
77
|
|
|
$query = $this->getSut('offices') |
|
78
|
|
|
->setInto(new Table('offices')) |
|
79
|
|
|
->setColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
|
80
|
|
|
->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA') |
|
81
|
|
|
->setDoNothing(); |
|
82
|
|
|
|
|
83
|
|
|
$parts = []; |
|
84
|
|
|
$parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)'; |
|
85
|
|
|
$parts[] = 'VALUES (?, ?, ?, ?, ?, ?, ?)'; |
|
86
|
|
|
$parts[] = 'ON CONFLICT DO NOTHING'; |
|
87
|
|
|
|
|
88
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertSame($expectedSql, (string)$query); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testAddOnConflictDoUpdate() |
|
94
|
|
|
{ |
|
95
|
|
|
$query = $this->getSut('offices') |
|
96
|
|
|
->setInto(new Table('offices')) |
|
97
|
|
|
->setColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
|
98
|
|
|
->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA') |
|
99
|
|
|
->setOnConflict('officeCode', 'city') |
|
100
|
|
|
->setDoUpdate('officeCode = EXCLUDED.officeCode', 'city = EXCLUDED.city') |
|
101
|
|
|
->setReturning('*'); |
|
102
|
|
|
|
|
103
|
|
|
$parts = []; |
|
104
|
|
|
$parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)'; |
|
105
|
|
|
$parts[] = 'VALUES (?, ?, ?, ?, ?, ?, ?)'; |
|
106
|
|
|
$parts[] = 'ON CONFLICT (officeCode, city) DO UPDATE'; |
|
107
|
|
|
$parts[] = 'SET officeCode = EXCLUDED.officeCode, city = EXCLUDED.city'; |
|
108
|
|
|
$parts[] = 'RETURNING *'; |
|
109
|
|
|
|
|
110
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertSame($expectedSql, (string)$query); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $table |
|
117
|
|
|
* |
|
118
|
|
|
* @return Insert |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getSut(string $table): Insert |
|
121
|
|
|
{ |
|
122
|
|
|
return (new Insert())->setInto($table); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|