-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathpostgresqlTest.php
More file actions
438 lines (367 loc) · 16.1 KB
/
postgresqlTest.php
File metadata and controls
438 lines (367 loc) · 16.1 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
namespace ezsql\Tests\postgresql;
use ezsql\Config;
use ezsql\Database\ez_pgsql;
use ezsql\Tests\EZTestCase;
use function ezsql\functions\{
column,
creating,
deleting,
dropping,
updating,
primary,
eq,
pgsqlInstance,
selecting,
inserting,
table_setup,
where
};
class postgresqlTest extends EZTestCase
{
/**
* constant database port
*/
const TEST_DB_PORT = '5432';
/**
* @var ez_pgsql
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp(): void
{
if (!extension_loaded('pgsql')) {
$this->markTestSkipped(
'The PostgreSQL Lib is not available.'
);
}
$this->object = pgsqlInstance([self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT]);
$this->object->prepareOn();
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown(): void
{
$this->object = null;
}
public function testSettings()
{
$this->assertTrue($this->object->settings() instanceof \ezsql\ConfigInterface);
}
public function testQuick_connect()
{
$this->assertTrue($this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT));
}
public function testConnect()
{
$this->errors = array();
set_error_handler(array($this, 'errorHandler'));
$this->assertFalse($this->object->connect('self::TEST_DB_USER', 'self::TEST_DB_PASSWORD', 'self::TEST_DB_NAME', 'self::TEST_DB_CHARSET'));
$this->assertTrue($this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT));
$this->assertTrue($this->object->connect());
}
public function testEscape()
{
$result = $this->object->escape("This is'nt escaped.");
$this->assertEquals("This is''nt escaped.", $result);
}
public function testSysDate()
{
$this->assertEquals('NOW()', $this->object->sysDate());
}
public function testQuery()
{
$this->assertTrue($this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT));
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
$this->object->query('CREATE TABLE unit_test(id serial, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$this->assertEquals($this->object->query('INSERT INTO unit_test(test_key, test_value) VALUES(\'test 1\', \'testing string 1\')'), 1);
$this->object->reset();
$this->assertNotNull($this->object->query('INSERT INTO unit_test(test_key, test_value) VALUES(\'test 2\', \'testing string 2\')'));
$this->object->disconnect();
$this->assertFalse($this->object->query('INSERT INTO unit_test(test_key, test_value) VALUES(\'test 3\', \'testing string 3\')'));
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
}
public function testCreate()
{
$this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT);
$this->assertEquals(
$this->object->create(
'new_create_test',
column('id', AUTO),
column('create_key', VARCHAR, 50),
primary('id_pk', 'id')
),
0
);
$this->object->prepareOff();
$this->assertEquals(
$this->object->insert(
'new_create_test',
['create_key' => 'test 2']
),
1
);
$this->object->prepareOn();
}
public function testDrop()
{
$this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT);
$this->assertEquals($this->object->drop('new_create_test'), 0);
}
public function testInsert()
{
$this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT);
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
$this->object->query('CREATE TABLE unit_test(id serial, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$result = $this->object->insert('unit_test', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->assertEquals($result, 1);
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
}
public function testUpdate()
{
$this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT);
$this->object->query('CREATE TABLE unit_test(id serial, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$this->object->insert('unit_test', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->object->insert('unit_test', array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$result = $this->object->insert('unit_test', array('test_key' => 'test 3', 'test_value' => 'testing string 3'));
$this->assertEquals($result, 3);
$unit_test['test_key'] = 'the key string';
$where = eq('test_key', 'test 1');
$this->assertEquals(
1,
$this->object->update('unit_test', $unit_test, $where)
);
$this->assertEquals(1, $this->object->update(
'unit_test',
$unit_test,
array('test_key', EQ, 'test 3', 'and'),
array('test_value', '=', 'testing string 3')
));
$where = array('test_value', EQ, 'testing string 4');
$this->assertEquals(
0,
$this->object->update('unit_test', $unit_test, $where)
);
$this->assertEquals(
1,
$this->object->update('unit_test', $unit_test, eq('test_key', 'test 2'))
);
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
}
public function testUpdatingDeleting()
{
$this->object->prepareOff();
$this->object->drop('unit_test');
$this->assertFalse($this->object->updating([]));
$this->assertFalse($this->object->deleting([]));
$this->assertFalse($this->object->inserting([]));
$this->assertFalse($this->object->selecting());
table_setup('unit_test');
$this->assertEquals(
0,
creating(
column('id', AUTO, PRIMARY),
column('test_key', VARCHAR, 50),
column('test_value', VARCHAR, 50)
)
);
inserting(array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
inserting(array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$result = inserting(array('test_key' => 'test 3', 'test_value' => 'testing string 3'));
$this->assertEquals($result, 3);
$unit_test['test_key'] = 'the key string';
$this->assertEquals(1, updating($unit_test, eq('test_key', 'test 1')));
$this->assertEquals(1, deleting(eq('test_key', 'test 3')));
$result = selecting('test_value', eq('test_key', 'the key string'));
foreach ($result as $row) {
$this->assertEquals('testing string 1', $row->test_value);
}
dropping();
}
public function testDelete()
{
$this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT);
$this->object->query('CREATE TABLE unit_test(id serial, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$this->object->insert('unit_test', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->object->insert('unit_test', array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$this->object->insert('unit_test', array('test_key' => 'test 3', 'test_value' => 'testing string 3'));
$where = array('test_key', '=', 'test 1');
$this->assertEquals($this->object->delete('unit_test', $where), 1);
$this->assertEquals(1, $this->object->delete(
'unit_test',
array('test_key', '=', 'test 3'),
array('test_value', '=', 'testing string 3')
));
$where = array('test_value', '=', 'testing 2');
$this->assertEquals(
0,
$this->object->delete('unit_test', $where)
);
$where = eq('test_key', 'test 2');
$this->assertEquals(
1,
$this->object->delete('unit_test', $where)
);
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
}
public function testDisconnect()
{
$this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT);
$this->assertTrue($this->object->isConnected());
$this->assertNotNull($this->object->handle());
$this->object->disconnect();
$this->assertFalse($this->object->isConnected());
$this->object->reset();
$this->assertNull($this->object->handle());
}
public function testGetHost()
{
$this->assertEquals(self::TEST_DB_HOST, $this->object->getHost());
}
public function testGetPort()
{
$this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT);
$this->assertEquals(self::TEST_DB_PORT, $this->object->getPort());
}
public function testSelect()
{
$this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT);
$this->object->query('CREATE TABLE unit_test(id serial, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$this->object->insert('unit_test', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->object->insert('unit_test', array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$this->object->insert('unit_test', array('test_key' => 'test 3', 'test_value' => 'testing string 3'));
$result = $this->object->select('unit_test');
$i = 1;
foreach ($result as $row) {
$this->assertEquals($i, $row->id);
$this->assertEquals('testing string ' . $i, $row->test_value);
$this->assertEquals('test ' . $i, $row->test_key);
++$i;
}
$where = eq('id', '2');
$result = $this->object->select('unit_test', 'id', $this->object->where($where));
foreach ($result as $row) {
$this->assertEquals(2, $row->id);
}
$where = [eq('test_value', 'testing string 3', _AND), eq('id', '3')];
$result = $this->object->select('unit_test', 'test_key', $this->object->where($where));
foreach ($result as $row) {
$this->assertEquals('test 3', $row->test_key);
}
$result = $this->object->select('unit_test', 'test_value', where(eq('test_key', 'test 1')));
foreach ($result as $row) {
$this->assertEquals('testing string 1', $row->test_value);
}
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
}
public function testBeginTransactionCommit()
{
$this->object->connect();
$this->object->query('CREATE TABLE unit_test(id serial, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$commit = null;
try {
$commit = true;
$this->object->beginTransaction();
$this->object->insert('unit_test', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->object->insert('unit_test', array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$this->object->insert('unit_test', array('test_key' => 'test 3', 'test_value' => 'testing string 3'));
$this->object->commit();
} catch (\Exception $ex) {
$commit = false;
$this->object->rollback();
echo ("Error! This rollback message shouldn't have been displayed: ") . $ex->getMessage();
}
if ($commit) {
$result = $this->object->select('unit_test');
$i = 1;
foreach ($result as $row) {
$this->assertEquals($i, $row->id);
$this->assertEquals('testing string ' . $i, $row->test_value);
$this->assertEquals('test ' . $i, $row->test_key);
++$i;
}
$this->assertEquals(0, $this->object->drop('unit_test'));
}
}
public function testBeginTransactionRollback()
{
$this->object->connect();
$this->object->query('CREATE TABLE unit_test(id int(11) NOT NULL AUTO_INCREMENT, test_key varchar(50), PRIMARY KEY (ID)');
$commit = null;
try {
$commit = true;
$this->object->beginTransaction();
$this->object->insert('unit_test', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->object->insert('unit_test', array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$this->object->insert('unit_test', array('test_key' => 'test 3', 'test_value' => 'testing string 3'));
$this->object->commit();
} catch (\Exception $ex) {
$commit = false;
$this->object->rollback();
}
if ($commit) {
echo ("Error! This message shouldn't have been displayed.");
$result = $this->object->select('unit_test');
$i = 1;
foreach ($result as $row) {
$this->assertEquals($i, $row->id);
$this->assertEquals('should not be seen ' . $i, $row->test_value);
$this->assertEquals('test ' . $i, $row->test_key);
++$i;
}
$this->object->drop('unit_test');
} else {
//echo ("Error! rollback.");
$result = $this->object->select('unit_test');
$i = 1;
foreach ($result as $row) {
$this->assertEquals($i, $row->id);
$this->assertEquals('should not be seen ' . $i, $row->test_value);
$this->assertEquals('test ' . $i, $row->test_key);
++$i;
}
$this->assertEquals(0, $result);
$this->object->drop('unit_test');
}
}
public function testQuery_prepared()
{
$this->object->prepareOff();
$this->object->connect();
$this->object->drop('prepare_test');
$this->assertEquals(
0,
$this->object->create(
'prepare_test',
column('id', AUTO, PRIMARY),
column('prepare_key', VARCHAR, 50)
)
);
$this->object->insert('prepare_test', array('prepare_key' => 'test 1'));
$result = $this->object->insert('prepare_test', array('prepare_key' => 'test 2'));
$this->assertEquals(2, $result);
$this->object->query_prepared('SELECT id, prepare_key FROM prepare_test WHERE id = $1', [1]);
$query = $this->object->queryResult();
foreach ($query as $row) {
$this->assertEquals(1, $row->id);
$this->assertEquals('test 1', $row->prepare_key);
}
$this->object->drop('prepare_test');
}
public function test__Construct_Error()
{
$this->expectExceptionMessageRegExp('/[Missing configuration details]/');
$this->assertNull(new ez_pgsql());
}
public function test__construct()
{
unset($GLOBALS['ez' . \PGSQL]);
$settings = new Config('pgsql', [self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT]);
$this->assertNotNull(new ez_pgsql($settings));
}
}