|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under |
|
5
|
|
|
* the terms of the GPL-2 license. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Konrad Abicht <[email protected]> |
|
8
|
|
|
* (c) Benjamin Nowack |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Tests\Integration; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use sweetrdf\InMemoryStoreSqlite\PDOSQLiteAdapter; |
|
18
|
|
|
use Tests\TestCase; |
|
19
|
|
|
|
|
20
|
|
|
class PDOSQLiteAdapterTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
protected function setUp(): void |
|
23
|
|
|
{ |
|
24
|
|
|
parent::setUp(); |
|
25
|
|
|
|
|
26
|
|
|
$this->subjectUnderTest = new PDOSQLiteAdapter(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/* |
|
30
|
|
|
* Tests for connect |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
public function testConnectCreateNewConnection() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->subjectUnderTest->disconnect(); |
|
36
|
|
|
|
|
37
|
|
|
// do explicit reconnect |
|
38
|
|
|
$this->subjectUnderTest = new PDOSQLiteAdapter(); |
|
39
|
|
|
|
|
40
|
|
|
$this->subjectUnderTest->exec('CREATE TABLE test (id INTEGER)'); |
|
41
|
|
|
$this->assertEquals([], $this->subjectUnderTest->fetchList('SELECT * FROM test;')); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testEscape() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->assertEquals('"hallo"', $this->subjectUnderTest->escape('"hallo"')); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/* |
|
50
|
|
|
* Tests for exec |
|
51
|
|
|
*/ |
|
52
|
|
|
|
|
53
|
|
|
public function testExec() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->subjectUnderTest->exec('CREATE TABLE users (id INTEGER, name TEXT NOT NULL)'); |
|
56
|
|
|
$this->subjectUnderTest->exec('INSERT INTO users (id, name) VALUES (1, "foobar");'); |
|
57
|
|
|
$this->subjectUnderTest->exec('INSERT INTO users (id, name) VALUES (2, "foobar2");'); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertEquals(2, $this->subjectUnderTest->exec('DELETE FROM users;')); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/* |
|
63
|
|
|
* Tests for fetchRow |
|
64
|
|
|
*/ |
|
65
|
|
|
|
|
66
|
|
|
public function testFetchRow() |
|
67
|
|
|
{ |
|
68
|
|
|
// valid query |
|
69
|
|
|
$this->subjectUnderTest->exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)'); |
|
70
|
|
|
$this->assertFalse($this->subjectUnderTest->fetchRow('SELECT * FROM users')); |
|
71
|
|
|
|
|
72
|
|
|
// add data |
|
73
|
|
|
$this->subjectUnderTest->exec('INSERT INTO users (id, name) VALUES (1, "foobar");'); |
|
74
|
|
|
$this->assertEquals( |
|
75
|
|
|
[ |
|
76
|
|
|
'id' => 1, |
|
77
|
|
|
'name' => 'foobar', |
|
78
|
|
|
], |
|
79
|
|
|
$this->subjectUnderTest->fetchRow('SELECT * FROM users WHERE id = 1;') |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/* |
|
84
|
|
|
* Tests for fetchList |
|
85
|
|
|
*/ |
|
86
|
|
|
|
|
87
|
|
|
public function testFetchList() |
|
88
|
|
|
{ |
|
89
|
|
|
// valid query |
|
90
|
|
|
$sql = 'CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)'; |
|
91
|
|
|
$this->subjectUnderTest->exec($sql); |
|
92
|
|
|
$this->assertEquals([], $this->subjectUnderTest->fetchList('SELECT * FROM users')); |
|
93
|
|
|
|
|
94
|
|
|
// add data |
|
95
|
|
|
$this->subjectUnderTest->exec('INSERT INTO users (id, name) VALUES (1, "foobar");'); |
|
96
|
|
|
$this->assertEquals( |
|
97
|
|
|
[ |
|
98
|
|
|
[ |
|
99
|
|
|
'id' => 1, |
|
100
|
|
|
'name' => 'foobar', |
|
101
|
|
|
], |
|
102
|
|
|
], |
|
103
|
|
|
$this->subjectUnderTest->fetchList('SELECT * FROM users') |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testGetPDO() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->assertTrue($this->subjectUnderTest->getPDO() instanceof \PDO); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/* |
|
113
|
|
|
* Tests for getNumberOfRows |
|
114
|
|
|
*/ |
|
115
|
|
|
|
|
116
|
|
|
public function testGetNumberOfRows() |
|
117
|
|
|
{ |
|
118
|
|
|
// create test table |
|
119
|
|
|
$this->subjectUnderTest->exec('CREATE TABLE pet (name TEXT)'); |
|
120
|
|
|
$this->subjectUnderTest->exec('INSERT INTO pet VALUES ("cat")'); |
|
121
|
|
|
$this->subjectUnderTest->exec('INSERT INTO pet VALUES ("dog")'); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertEquals(2, $this->subjectUnderTest->getNumberOfRows('SELECT * FROM pet;')); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testGetNumberOfRowsInvalidQuery() |
|
127
|
|
|
{ |
|
128
|
|
|
$this->expectException('Exception'); |
|
129
|
|
|
|
|
130
|
|
|
$this->subjectUnderTest->getNumberOfRows('SHOW TABLES of x'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/* |
|
134
|
|
|
* Tests for getServerVersion |
|
135
|
|
|
*/ |
|
136
|
|
|
|
|
137
|
|
|
public function testGetServerVersion() |
|
138
|
|
|
{ |
|
139
|
|
|
// check server version |
|
140
|
|
|
$this->assertEquals( |
|
141
|
|
|
1, |
|
142
|
|
|
preg_match( |
|
143
|
|
|
'/[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}/', |
|
144
|
|
|
'Found: '.$this->subjectUnderTest->getServerVersion() |
|
145
|
|
|
) |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/* |
|
150
|
|
|
* Tests for insert |
|
151
|
|
|
*/ |
|
152
|
|
|
|
|
153
|
|
|
public function testInsert() |
|
154
|
|
|
{ |
|
155
|
|
|
// create test table |
|
156
|
|
|
$this->subjectUnderTest->exec('CREATE TABLE pet (name TEXT)'); |
|
157
|
|
|
|
|
158
|
|
|
$this->subjectUnderTest->insert('pet', ['name' => 'test1']); |
|
159
|
|
|
$this->subjectUnderTest->insert('pet', ['name' => 'test2']); |
|
160
|
|
|
|
|
161
|
|
|
$this->assertEquals(2, $this->subjectUnderTest->getNumberOfRows('SELECT * FROM pet;')); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function testInsertTableNameSpecialChars() |
|
165
|
|
|
{ |
|
166
|
|
|
$this->expectException(Exception::class); |
|
167
|
|
|
$this->expectExceptionMessage('Invalid table name given.'); |
|
168
|
|
|
|
|
169
|
|
|
// create test table |
|
170
|
|
|
$this->subjectUnderTest->exec('CREATE TABLE pet (name TEXT)'); |
|
171
|
|
|
|
|
172
|
|
|
$this->subjectUnderTest->insert('pet"', ['name' => 'test1']); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function testQuery() |
|
176
|
|
|
{ |
|
177
|
|
|
// valid query |
|
178
|
|
|
$sql = 'CREATE TABLE MyGuests (id INTEGER PRIMARY KEY AUTOINCREMENT)'; |
|
179
|
|
|
$this->subjectUnderTest->exec($sql); |
|
180
|
|
|
|
|
181
|
|
|
$foundTable = false; |
|
182
|
|
|
foreach ($this->subjectUnderTest->getAllTables() as $table) { |
|
183
|
|
|
if ('MyGuests' == $table) { |
|
184
|
|
|
$foundTable = true; |
|
185
|
|
|
break; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
$this->assertTrue($foundTable, 'Expected table not found.'); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function testQueryInvalid() |
|
192
|
|
|
{ |
|
193
|
|
|
$this->expectException('Exception'); |
|
194
|
|
|
|
|
195
|
|
|
// invalid query |
|
196
|
|
|
$this->assertFalse($this->subjectUnderTest->simpleQuery('invalid query')); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|