|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace QB\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use PDO; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use QB\Generic\Clause\Column; |
|
10
|
|
|
use QB\Generic\Clause\Table; |
|
11
|
|
|
use QB\Generic\Expr\Expr; |
|
12
|
|
|
use QB\PDOHelper; |
|
13
|
|
|
use QB\PostgreSQL\Factory\Factory; |
|
14
|
|
|
|
|
15
|
|
|
class PostgreSQLTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Factory */ |
|
18
|
|
|
protected Factory $sut; |
|
19
|
|
|
|
|
20
|
|
|
protected \PDO $pdo; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp(): void |
|
23
|
|
|
{ |
|
24
|
|
|
if (!array_key_exists('POSTGRES_USER', $_ENV)) { |
|
25
|
|
|
$this->markTestSkipped('no db'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$this->sut = new Factory(); |
|
29
|
|
|
|
|
30
|
|
|
$dns = sprintf( |
|
31
|
|
|
'pgsql:dbname=%s;host=%s', |
|
32
|
|
|
$_ENV['POSTGRES_DB'], |
|
33
|
|
|
'postgres' |
|
34
|
|
|
); |
|
35
|
|
|
$username = $_ENV['POSTGRES_USER']; |
|
36
|
|
|
$password = $_ENV['POSTGRES_PASSWORD']; |
|
37
|
|
|
$options = null; |
|
38
|
|
|
|
|
39
|
|
|
$this->pdo = new \PDO($dns, $username, $password, $options); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testSelectOneCustomer() |
|
43
|
|
|
{ |
|
44
|
|
|
$sql = (string)$this->sut->select() |
|
45
|
|
|
->addFrom('customers') |
|
46
|
|
|
->addColumns('customerName') |
|
47
|
|
|
->setLimit(1); |
|
48
|
|
|
|
|
49
|
|
|
$statement = $this->pdo->query($sql); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertSame('Atelier graphique', $statement->fetchColumn()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testSelectComplex() |
|
55
|
|
|
{ |
|
56
|
|
|
$limit = 5; |
|
57
|
|
|
|
|
58
|
|
|
$columnQuery = $this->sut->select() |
|
59
|
|
|
->addFrom(new Table('employees', 'boss')) |
|
60
|
|
|
->addColumns('lastName') |
|
61
|
|
|
->addWhere(new Expr('boss.employeeNumber = employees.reportsTo')); |
|
62
|
|
|
|
|
63
|
|
|
$customerTypeColumn = new Column(new Expr("'customers'"), 'type'); |
|
64
|
|
|
$employeeTypeColumn = new Column(new Expr("'employees'"), 'type'); |
|
65
|
|
|
|
|
66
|
|
|
$unionQuery = $this->sut->select() |
|
67
|
|
|
->addFrom('customers') |
|
68
|
|
|
->addColumns('contactLastName', 'NULL', $customerTypeColumn); |
|
69
|
|
|
|
|
70
|
|
|
$query = $this->sut->select() |
|
71
|
|
|
->addFrom('employees') |
|
72
|
|
|
->addColumns('lastName', new Column($columnQuery, 'bossLastName'), $employeeTypeColumn) |
|
73
|
|
|
->addInnerJoin('offices', 'employees.officeCode = o.officeCode', 'o') |
|
74
|
|
|
->addWhere(new Expr('employees.jobTitle = ?', ['Sales Rep'])) |
|
75
|
|
|
->addWhere('o.city = \'NYC\'') |
|
76
|
|
|
->addUnion($unionQuery) |
|
77
|
|
|
->setOuterOrderBy('type', 'DESC') |
|
78
|
|
|
->setOuterOrderBy('lastName') |
|
79
|
|
|
->setOuterLimit($limit); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertCount($limit, PDOHelper::fetchAll($this->pdo, $query, PDO::FETCH_ASSOC)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testInsertUpdateDelete() |
|
85
|
|
|
{ |
|
86
|
|
|
try { |
|
87
|
|
|
$this->pdo->exec('BEGIN'); |
|
88
|
|
|
|
|
89
|
|
|
// INSERT |
|
90
|
|
|
$query = $this->sut->insert() |
|
91
|
|
|
->setInto(new Table('offices')) |
|
92
|
|
|
->setColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
|
93
|
|
|
->addValues("'abc'", "'Berlin'", "'+49 101 123 4567'", "''", "'Germany'", "'10111'", "'NA'"); |
|
94
|
|
|
|
|
95
|
|
|
$statement = $this->pdo->prepare((string)$query); |
|
96
|
|
|
|
|
97
|
|
|
$result = $statement->execute(); |
|
98
|
|
|
$this->assertTrue($result); |
|
99
|
|
|
|
|
100
|
|
|
// UPDATE |
|
101
|
|
|
$query = $this->sut->update() |
|
102
|
|
|
->addFrom(new Table('offices')) |
|
103
|
|
|
->setValues(['territory' => "'Berlin'"]) |
|
104
|
|
|
->addWhere("officeCode = 'oc'"); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertTrue(PDOHelper::execute($this->pdo, $query)); |
|
107
|
|
|
|
|
108
|
|
|
// DELETE |
|
109
|
|
|
$query = $this->sut->delete() |
|
110
|
|
|
->addFrom(new Table('offices')) |
|
111
|
|
|
->addWhere(new Expr('officeCode = ?', ['abc'])); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertTrue(PDOHelper::execute($this->pdo, $query)); |
|
114
|
|
|
|
|
115
|
|
|
// COMMIT |
|
116
|
|
|
$this->pdo->exec('COMMIT'); |
|
117
|
|
|
} catch (\Exception $e) { |
|
118
|
|
|
if ($this->pdo->inTransaction()) { |
|
119
|
|
|
$this->pdo->exec('ROLLBACK'); |
|
120
|
|
|
} |
|
121
|
|
|
$this->fail($e->getMessage() . PHP_EOL . $e->getTraceAsString()); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function testUpsert() |
|
126
|
|
|
{ |
|
127
|
|
|
try { |
|
128
|
|
|
$this->pdo->exec('BEGIN'); |
|
129
|
|
|
|
|
130
|
|
|
// INSERT |
|
131
|
|
|
$query = $this->sut->insert() |
|
132
|
|
|
->setInto(new Table('offices')) |
|
133
|
|
|
->setColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
|
134
|
|
|
->addValues("'abc'", "'Berlin'", "'+49 101 123 4567'", "''", "'Germany'", "'10111'", "'NA'") |
|
135
|
|
|
->addValues("'bcd'", "'Budapest'", "'+36 70 101 1234'", "''", "'Hungary'", "'1011'", "'NA'") |
|
136
|
|
|
->setReturning('*'); |
|
137
|
|
|
|
|
138
|
|
|
$values = PDOHelper::fetchAll($this->pdo, $query, \PDO::FETCH_ASSOC); |
|
139
|
|
|
|
|
140
|
|
|
$expectedValues = [ |
|
141
|
|
|
[ |
|
142
|
|
|
'officecode' => 'abc', |
|
143
|
|
|
'city' => 'Berlin', |
|
144
|
|
|
'phone' => '+49 101 123 4567', |
|
145
|
|
|
'addressline1' => '', |
|
146
|
|
|
'addressline2' => null, |
|
147
|
|
|
'state' => null, |
|
148
|
|
|
'country' => 'Germany', |
|
149
|
|
|
'postalcode' => '10111', |
|
150
|
|
|
'territory' => 'NA', |
|
151
|
|
|
], |
|
152
|
|
|
[ |
|
153
|
|
|
'officecode' => 'bcd', |
|
154
|
|
|
'city' => 'Budapest', |
|
155
|
|
|
'phone' => '+36 70 101 1234', |
|
156
|
|
|
'addressline1' => '', |
|
157
|
|
|
'addressline2' => null, |
|
158
|
|
|
'state' => null, |
|
159
|
|
|
'country' => 'Hungary', |
|
160
|
|
|
'postalcode' => '1011', |
|
161
|
|
|
'territory' => 'NA', |
|
162
|
|
|
], |
|
163
|
|
|
]; |
|
164
|
|
|
$this->assertSame($expectedValues, $values); |
|
165
|
|
|
|
|
166
|
|
|
// DELETE |
|
167
|
|
|
$query = $this->sut->delete() |
|
168
|
|
|
->addFrom(new Table('offices')) |
|
169
|
|
|
->addWhere(new Expr('officeCode IN (?)', [['abc', 'bcd']])); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertTrue(PDOHelper::execute($this->pdo, $query)); |
|
172
|
|
|
|
|
173
|
|
|
// COMMIT |
|
174
|
|
|
$this->pdo->exec('COMMIT'); |
|
175
|
|
|
} catch (\Exception $e) { |
|
176
|
|
|
if ($this->pdo->inTransaction()) { |
|
177
|
|
|
$this->pdo->exec('ROLLBACK'); |
|
178
|
|
|
} |
|
179
|
|
|
$this->fail($e->getMessage() . PHP_EOL . $e->getTraceAsString()); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|