-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathsqlite3Test.php
More file actions
389 lines (326 loc) · 13.6 KB
/
sqlite3Test.php
File metadata and controls
389 lines (326 loc) · 13.6 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
<?php
namespace ezsql\Tests\sqlite;
use Exception;
use ezsql\Config;
use ezsql\Database\ez_sqlite3;
use ezsql\Tests\EZTestCase;
use function ezsql\functions\{
column,
primary,
eq,
sqliteInstance
};
/**
* Generated by PHPUnit_SkeletonGenerator on 2018-03-08 at 02:54:12.
*/
class sqlite3Test extends EZTestCase
{
/**
* constant string path and file name of the SQLite test database
*/
const TEST_SQLITE_DB = 'ez_test.sqlite3';
const TEST_SQLITE_DB_DIR = './tests/sqlite/';
/**
* @var ez_sqlite3
*/
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('sqlite3')) {
$this->markTestSkipped(
'The sqlite3 Lib is not available.'
);
}
$this->object = sqliteInstance([self::TEST_SQLITE_DB_DIR, self::TEST_SQLITE_DB]);
$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->drop("test_table");
$this->object = null;
}
public function testSettings()
{
$this->assertTrue($this->object->settings() instanceof \ezsql\ConfigInterface);
}
public function testDisconnect()
{
$this->object->connect();
$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 testConnect()
{
$this->assertTrue($this->object->connect());
$this->assertTrue($this->object->isConnected());
}
public function testQuick_connect()
{
$this->assertNotNull($this->object->quick_connect(self::TEST_SQLITE_DB_DIR, self::TEST_SQLITE_DB));
}
public function testSQLite3Escape()
{
$this->object->connect(self::TEST_SQLITE_DB_DIR, self::TEST_SQLITE_DB);
$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->object->connect(self::TEST_SQLITE_DB_DIR, self::TEST_SQLITE_DB);
// Create a table..
$this->object->drop("test_table");
$this->assertEquals(0, $this->object->query("CREATE TABLE test_table ( MyColumnA INTEGER PRIMARY KEY, MyColumnB TEXT(32) );"));
// Insert test data
for ($i = 0; $i < 3; ++$i) {
$this->assertNotNull($this->object->query('INSERT INTO test_table (MyColumnB) VALUES ("' . md5(microtime()) . '");'));
}
// Get list of tables from current database..
$my_tables = $this->object->get_results("SELECT * FROM sqlite_master WHERE sql NOTNULL;");
// Loop through each row of results..
foreach ($my_tables as $table) {
// Get results of DESC table..
$this->assertNotNull($this->object->get_results("SELECT * FROM $table->name;"));
}
// Get rid of the table we created..
$this->object->query("DROP TABLE test_table;");
}
public function testCreate()
{
$this->object->connect(self::TEST_SQLITE_DB_DIR, self::TEST_SQLITE_DB);
$this->assertEquals(
$this->object->create(
'new_create_test',
column('id', INTEGERS, notNULL, 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']
),
0
);
$this->object->prepareOn();
}
public function testDrop()
{
$this->assertEquals($this->object->drop('new_create_test'), 0);
}
public function testInsert()
{
$this->object->query('CREATE TABLE test_table(id integer, test_key varchar(50), PRIMARY KEY (ID))');
$result = $this->object->insert('test_table', array('test_key' => 'test 1'));
$this->assertEquals(0, $result);
}
public function testUpdate()
{
$this->object->query('CREATE TABLE test_table(id integer, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$this->object->insert('test_table', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->object->insert('test_table', array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$result = $this->object->insert('test_table', array('test_key' => 'test 3', 'test_value' => 'testing string 3'));
$this->assertEquals($result, 3);
$test_table['test_key'] = 'the key string';
$where = ['test_key', '=', 'test 1'];
$this->assertEquals(
1,
$this->object->update('test_table', $test_table, $where)
);
$this->assertEquals(
1,
$this->object->update(
'test_table',
$test_table,
eq('test_key', 'test 3'),
eq('test_value', 'testing string 3')
)
);
$where = eq('test_value', 'testing string 4');
$this->assertEquals(
0,
$this->object->update('test_table', $test_table, $where)
);
$this->assertEquals(
1,
$this->object->update('test_table', $test_table, ['test_key', '=', 'test 2'])
);
}
public function testDelete()
{
$this->object->query('CREATE TABLE test_table(id integer, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$this->object->insert('test_table', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->object->insert('test_table', array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$this->object->insert('test_table', array('test_key' => 'test 3', 'test_value' => 'testing string 3'));
$where = array('test_key', '=', 'test 1');
$this->assertEquals($this->object->delete('test_table', $where), 1);
$this->assertEquals($this->object->delete(
'test_table',
array('test_key', '=', 'test 3'),
array('test_value', '=', 'testing string 3')
), 1);
$where = array('test_value', '=', 'testing 2');
$this->assertEquals(0, $this->object->delete('test_table', $where));
$where = ['test_key', '=', 'test 2'];
$this->assertEquals(1, $this->object->delete('test_table', $where));
}
public function testSelect()
{
$this->object->query('CREATE TABLE test_table(id integer, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$this->object->insert('test_table', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->object->insert('test_table', array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$this->object->insert('test_table', array('test_key' => 'test 3', 'test_value' => 'testing string 3'));
$result = $this->object->select('test_table');
$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('test_table', 'id', $this->object->where($where));
foreach ($result as $row) {
$this->assertEquals(2, $row->id);
}
$where = [eq('test_value', 'testing string 3')];
$result = $this->object->select('test_table', 'test_key', $this->object->where($where));
foreach ($result as $row) {
$this->assertEquals('test 3', $row->test_key);
}
$result = $this->object->select('test_table', 'test_value', $this->object->where(eq('test_key', 'test 1')));
foreach ($result as $row) {
$this->assertEquals('testing string 1', $row->test_value);
}
}
public function testBeginTransactionCommit()
{
$this->object->connect();
$this->object->query('CREATE TABLE IF NOT EXISTS test_table(id integer, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$commit = null;
try {
$commit = true;
$this->object->beginTransaction();
$this->object->insert('test_table', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->object->insert('test_table', array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$this->object->insert('test_table', 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('test_table');
$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('test_table'));
}
}
public function testBeginTransactionRollback()
{
$this->object->query('CREATE TABLE IF NOT EXISTS test_table(id integer, test_key varchar(50), test_value varchar(50), PRIMARY KEY (ID))');
$commit = null;
try {
$commit = true;
$this->object->beginTransaction();
$this->object->insert('test_table', array('test_key' => 'test 1', 'test_value' => 'testing string 1'));
$this->object->insert('test_table', array('test_key' => 'test 2', 'test_value' => 'testing string 2'));
$this->object->insert('test_table', array('test_keyx' => 'test 3', 'test_valuex' => '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('test_table');
$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('test_table');
} else {
//echo ("Error! rollback.");
$result = $this->object->select('test_table');
$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('test_table');
}
}
public function testQuery_prepared()
{
$this->object->prepareOff();
$this->object->drop('prepare_test');
$this->object->create(
'prepare_test',
column('id', INTEGERS, PRIMARY),
column('prepare_key', VARCHAR, 50)
);
$this->object->insert('prepare_test', ['id' => 1, 'prepare_key' => 'test 2']);
$this->object->query_prepared('INSERT INTO prepare_test( id, prepare_key ) VALUES( ?, ? )', [4, 'test 10']);
$this->object->query_prepared('INSERT INTO prepare_test( id, prepare_key ) VALUES( ?, ? )', [9, 'test 3']);
$this->object->query_prepared('SELECT id, prepare_key FROM prepare_test WHERE id = ?', [9]);
$query = $this->object->queryResult();
foreach ($query as $row) {
$this->assertEquals(9, $row->id);
$this->assertEquals('test 3', $row->prepare_key);
}
$this->object->query_prepared('SELECT id, prepare_key FROM prepare_test WHERE id = ?', [1]);
$query = $this->object->queryResult();
foreach ($query as $row) {
$this->assertEquals(1, $row->id);
$this->assertEquals('test 2', $row->prepare_key);
}
$this->object->query_prepared('SELECT id, prepare_key FROM prepare_test WHERE id = ?', [4]);
$query = $this->object->queryResult();
foreach ($query as $row) {
$this->assertEquals(4, $row->id);
$this->assertEquals('test 10', $row->prepare_key);
}
$this->object->drop('prepare_test');
}
public function test__Construct_Error()
{
$this->expectExceptionMessageRegExp('/[Missing configuration details]/');
$this->assertNull(new ez_sqlite3());
}
public function test__construct()
{
unset($GLOBALS['ez' . \SQLITE3]);
$settings = new Config('sqlite3', [self::TEST_SQLITE_DB_DIR, self::TEST_SQLITE_DB]);
$this->assertNotNull(new ez_sqlite3($settings));
}
}