|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class SQLTest extends PHPUnit_Framework_TestCase { |
|
4
|
|
|
public function __construct(){ |
|
5
|
|
|
SQL::register('database_a','sqlite::memory:'); |
|
6
|
|
|
SQL::register('database_b','sqlite::memory:'); |
|
7
|
|
|
} |
|
8
|
|
|
|
|
9
|
|
|
public function testCreateTable(){ |
|
10
|
|
|
$results = SQL::exec("CREATE TABLE users ( |
|
11
|
|
|
id integer primary key, |
|
12
|
|
|
email text, |
|
13
|
|
|
password text |
|
14
|
|
|
);"); |
|
15
|
|
|
$this->assertNotFalse($results); |
|
16
|
|
|
|
|
17
|
|
|
$results = SQL::using('database_a')->exec("CREATE TABLE users ( |
|
18
|
|
|
id integer primary key, |
|
19
|
|
|
email text, |
|
20
|
|
|
password text |
|
21
|
|
|
);"); |
|
22
|
|
|
$this->assertNotFalse($results); |
|
23
|
|
|
|
|
24
|
|
|
$results = SQL::using('database_b')->exec("CREATE TABLE users ( |
|
25
|
|
|
id integer primary key, |
|
26
|
|
|
email text, |
|
27
|
|
|
password text |
|
28
|
|
|
);"); |
|
29
|
|
|
$this->assertNotFalse($results); |
|
30
|
|
|
|
|
31
|
|
|
$database_b = SQL::using('database_b'); |
|
32
|
|
|
|
|
33
|
|
|
$database_b->insert('users',[ |
|
34
|
|
|
'email' => '[email protected]', |
|
35
|
|
|
'password' => 'kek', |
|
36
|
|
|
]); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertEquals($database_b->value("SELECT password FROM users"),"kek"); |
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
public function testInsert(){ |
|
44
|
|
|
$id1 = SQL::insert('users',[ |
|
45
|
|
|
'email' => '[email protected]', |
|
46
|
|
|
'password' => '1111', |
|
47
|
|
|
]); |
|
48
|
|
|
|
|
49
|
|
|
$id2 = SQL::insert('users',[ |
|
50
|
|
|
'email' => '[email protected]', |
|
51
|
|
|
'password' => '2222', |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
$id3 = SQL::insert('users',[ |
|
55
|
|
|
'email' => '[email protected]', |
|
56
|
|
|
'password' => '3333', |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
$id4 = SQL::insert('users',[ |
|
60
|
|
|
'email' => '[email protected]', |
|
61
|
|
|
'password' => '4444', |
|
62
|
|
|
]); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertTrue(($id1 == 1) && ($id4 == 4)); |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testEachRowCallback(){ |
|
69
|
|
|
$cc = 0; |
|
70
|
|
|
SQL::each('SELECT id FROM users',function($row) use (&$cc) { |
|
71
|
|
|
$cc += $row->id; |
|
72
|
|
|
}); |
|
73
|
|
|
$this->assertEquals(10,$cc); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testEachRetrievingAll(){ |
|
77
|
|
|
$results = SQL::each('SELECT id FROM users'); |
|
78
|
|
|
$espect = '[{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"}]'; |
|
79
|
|
|
$this->assertEquals($espect,json_encode($results)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testUpdate(){ |
|
83
|
|
|
$results = SQL::update('users',[ |
|
84
|
|
|
'id' => 2, |
|
85
|
|
|
'password' => 'test', |
|
86
|
|
|
]); |
|
87
|
|
|
$this->assertTrue($results); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testGetValue(){ |
|
91
|
|
|
$results = SQL::value('SELECT password FROM users WHERE id=?',[2]); |
|
92
|
|
|
$this->assertEquals("test", $results); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testInsertOrUpdate(){ |
|
96
|
|
|
$iou = SQL::insertOrUpdate('users',[ |
|
97
|
|
|
'id' => "2", |
|
98
|
|
|
'password' => '2002', |
|
99
|
|
|
]); |
|
100
|
|
|
$check = SQL::value('SELECT password FROM users WHERE id=?',[2]); |
|
101
|
|
|
$this->assertNotFalse($iou); |
|
102
|
|
|
$check = SQL::value('SELECT password FROM users WHERE id=?',[2]); |
|
103
|
|
|
$this->assertEquals(2002, $check); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testDeleteSingle(){ |
|
107
|
|
|
$this->assertNotFalse(SQL::delete('users',2)); |
|
108
|
|
|
$this->assertEquals("1,3,4",SQL::value("SELECT GROUP_CONCAT(id) FROM users")); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testDeleteMultiple(){ |
|
112
|
|
|
$this->assertNotFalse(SQL::delete('users',[1,4])); |
|
113
|
|
|
$this->assertEquals("3",SQL::value("SELECT GROUP_CONCAT(id) FROM users")); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function testDeleteAll(){ |
|
117
|
|
|
$this->assertNotFalse(SQL::delete('users')); |
|
118
|
|
|
$this->assertEquals(0,SQL::value("SELECT count(*) FROM users")); |
|
119
|
|
|
} |
|
120
|
|
|
/**/ |
|
121
|
|
|
} |
|
122
|
|
|
|