-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseTest.php
57 lines (45 loc) · 1.62 KB
/
DatabaseTest.php
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
<?php
namespace FpDbTest;
use Exception;
class DatabaseTest
{
private DatabaseInterface $db;
public function __construct(DatabaseInterface $db)
{
$this->db = $db;
}
public function testBuildQuery(): void
{
$results = [];
$results[] = $this->db->buildQuery('SELECT name FROM users WHERE user_id = 1');
$results[] = $this->db->buildQuery(
'SELECT * FROM users WHERE name = ? AND block = 0',
['Jack']
);
$results[] = $this->db->buildQuery(
'SELECT ?# FROM users WHERE user_id = ?d AND block = ?d',
[['name', 'email'], 2, true]
);
$results[] = $this->db->buildQuery(
'UPDATE users SET ?a WHERE user_id = -1',
[['name' => 'Jack', 'email' => null]]
);
foreach ([null, true] as $block) {
$results[] = $this->db->buildQuery(
'SELECT name FROM users WHERE ?# IN (?a){ AND block = ?d}',
['user_id', [1, 2, 3], $block ?? $this->db->skip()]
);
}
$correct = [
'SELECT name FROM users WHERE user_id = 1',
'SELECT * FROM users WHERE name = \'Jack\' AND block = 0',
'SELECT `name`, `email` FROM users WHERE user_id = 2 AND block = 1',
'UPDATE users SET `name` = \'Jack\', `email` = NULL WHERE user_id = -1',
'SELECT name FROM users WHERE `user_id` IN (1, 2, 3)',
'SELECT name FROM users WHERE `user_id` IN (1, 2, 3) AND block = 1',
];
if ($results !== $correct) {
throw new Exception('Failure.');
}
}
}