-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbIntegrationTest.php
More file actions
334 lines (276 loc) · 9.95 KB
/
DbIntegrationTest.php
File metadata and controls
334 lines (276 loc) · 9.95 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
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Connection Component Integration Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Connection;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Connection\Db;
use \PDOException;
/**
* Integration tests for Db class with real database operations.
* Uses SQLite in-memory database for testing.
*/
class DbIntegrationTest extends TestCase
{
/**
* @var Db
*/
private $db;
/**
* @var string
*/
private $testDbPath = ':memory:';
/**
* Set up test environment before each test.
*/
protected function setUp() : void
{
// Skip if SQLite is not available
if ( !extension_loaded('pdo_sqlite') ) {
$this->markTestSkipped('SQLite PDO extension is not available.');
}
// Create a real Db instance with SQLite in-memory database
$config = [
'driver' => 'sqlite',
'database' => $this->testDbPath
];
try {
$this->db = new class ($config) extends Db {
protected function connect(array $config = []) : void
{
$dsn = "sqlite:{$config['database']}";
$this->pdo = new \PDO($dsn);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
$this->isConnected = true;
}
};
// Create test table
$this->createTestTable();
} catch (PDOException $e) {
$this->markTestSkipped('Could not create test database: ' . $e->getMessage());
}
}
/**
* Create test table for integration tests.
*/
private function createTestTable() : void
{
$sql = "
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
age INTEGER,
active BOOLEAN DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
";
$this->db->query($sql);
}
/**
* Test real database INSERT operation.
*/
public function testRealInsertOperation() : void
{
$sql = "INSERT INTO users (name, email, age, active) VALUES (:name, :email, :age, :active)";
$result = $this->db->query($sql, [
'name' => 'John Doe',
'email' => 'john@example.com',
'age' => 30,
'active' => true
]);
$this->assertEquals(1, $result); // Should return 1 affected row
// Test lastInsertId
$lastId = $this->db->lastInsertId();
$this->assertGreaterThan(0, $lastId);
}
/**
* Test real database SELECT operation.
*/
public function testRealSelectOperation() : void
{
// Insert test data first
$this->insertTestUsers();
$sql = "SELECT * FROM users WHERE active = :active ORDER BY name";
$result = $this->db->query($sql, ['active' => true]);
$this->assertIsArray($result);
$this->assertCount(2, $result);
$this->assertEquals('Alice Smith', $result[0]['name']);
$this->assertEquals('John Doe', $result[1]['name']);
}
/**
* Test real database UPDATE operation.
*/
public function testRealUpdateOperation() : void
{
// Insert test data first
$this->insertTestUsers();
$sql = "UPDATE users SET age = :age WHERE name = :name";
$result = $this->db->query($sql, [
'age' => 35,
'name' => 'John Doe'
]);
$this->assertEquals(1, $result); // Should return 1 affected row
// Verify the update
$selectSql = "SELECT age FROM users WHERE name = :name";
$updatedUser = $this->db->row($selectSql, ['name' => 'John Doe']);
$this->assertEquals(35, $updatedUser['age']);
}
/**
* Test real database DELETE operation.
*/
public function testRealDeleteOperation() : void
{
// Insert test data first
$this->insertTestUsers();
$sql = "DELETE FROM users WHERE email = :email";
$result = $this->db->query($sql, ['email' => 'inactive@example.com']);
$this->assertEquals(1, $result); // Should return 1 affected row
// Verify the deletion
$countSql = "SELECT COUNT(*) FROM users";
$count = $this->db->single($countSql);
$this->assertEquals(2, $count); // Should have 2 users left
}
/**
* Test row method with real data.
*/
public function testRealRowMethod() : void
{
$this->insertTestUsers();
$sql = "SELECT * FROM users WHERE email = :email";
$result = $this->db->row($sql, ['email' => 'john@example.com']);
$this->assertIsArray($result);
$this->assertEquals('John Doe', $result['name']);
$this->assertEquals('john@example.com', $result['email']);
$this->assertEquals(30, $result['age']);
}
/**
* Test single method with real data.
*/
public function testRealSingleMethod() : void
{
$this->insertTestUsers();
$sql = "SELECT COUNT(*) FROM users WHERE active = :active";
$result = $this->db->single($sql, ['active' => true]);
$this->assertEquals('2', $result); // SQLite returns string
}
/**
* Test column method with real data.
*/
public function testRealColumnMethod() : void
{
$this->insertTestUsers();
$sql = "SELECT name FROM users WHERE active = :active ORDER BY name";
$result = $this->db->column($sql, ['active' => true]);
$this->assertIsArray($result);
$this->assertCount(2, $result);
$this->assertEquals(['Alice Smith', 'John Doe'], $result);
}
/**
* Test transaction operations.
*/
public function testRealTransactionOperations() : void
{
$this->assertTrue($this->db->beginTransaction());
try {
// Insert multiple users in a transaction
$sql = "INSERT INTO users (name, email, age) VALUES (:name, :email, :age)";
$this->db->query($sql, ['name' => 'User 1', 'email' => 'user1@example.com', 'age' => 25]);
$this->db->query($sql, ['name' => 'User 2', 'email' => 'user2@example.com', 'age' => 28]);
$this->assertTrue($this->db->executeTransaction());
// Verify both users were inserted
$count = $this->db->single("SELECT COUNT(*) FROM users");
$this->assertEquals('2', $count);
} catch (\Exception $e) {
$this->db->rollBack();
$this->fail('Transaction failed: ' . $e->getMessage());
}
}
/**
* Test transaction rollback.
*/
public function testRealTransactionRollback() : void
{
// Insert one user first
$this->db->query("INSERT INTO users (name, email) VALUES ('Initial User', 'initial@example.com')");
$this->assertTrue($this->db->beginTransaction());
try {
// Insert a user
$this->db->query("INSERT INTO users (name, email) VALUES ('Temp User', 'temp@example.com')");
// Rollback the transaction
$this->assertTrue($this->db->rollBack());
// Verify only the initial user exists
$count = $this->db->single("SELECT COUNT(*) FROM users");
$this->assertEquals('1', $count);
$user = $this->db->single("SELECT name FROM users");
$this->assertEquals('Initial User', $user);
} catch (\Exception $e) {
$this->fail('Transaction rollback test failed: ' . $e->getMessage());
}
}
/**
* Test error handling with invalid SQL.
*/
public function testRealErrorHandling() : void
{
$this->expectException(PDOException::class);
$this->expectExceptionMessageMatches('/Query execution failed:/');
$invalidSql = "SELECT * FROM non_existent_table";
$this->db->query($invalidSql);
}
/**
* Test parameter binding with various data types.
*/
public function testRealParameterBinding() : void
{
$sql = "INSERT INTO users (name, email, age, active) VALUES (:name, :email, :age, :active)";
// Test with different data types
$result = $this->db->query($sql, [
'name' => 'Test User',
'email' => 'test@example.com',
'age' => null, // NULL value
'active' => false // Boolean false
]);
$this->assertEquals(1, $result);
// Verify the data was inserted correctly
$user = $this->db->row("SELECT * FROM users WHERE email = 'test@example.com'");
$this->assertEquals('Test User', $user['name']);
$this->assertNull($user['age']);
$this->assertEquals(0, $user['active']); // SQLite stores boolean as integer
}
/**
* Helper method to insert test users.
*/
private function insertTestUsers() : void
{
$users = [
['name' => 'John Doe', 'email' => 'john@example.com', 'age' => 30, 'active' => true],
['name' => 'Alice Smith', 'email' => 'alice@example.com', 'age' => 25, 'active' => true],
['name' => 'Inactive User', 'email' => 'inactive@example.com', 'age' => 40, 'active' => false]
];
$sql = "INSERT INTO users (name, email, age, active) VALUES (:name, :email, :age, :active)";
foreach ($users as $user) {
$this->db->query($sql, $user);
}
}
/**
* Clean up after each test.
*/
protected function tearDown() : void
{
if ( $this->db ) {
$this->db->close();
}
$this->db = null;
}
}