1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class QueryTestAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource QueryTest.php |
6
|
|
|
* @created 12.05.2017 |
7
|
|
|
* @package chillerlan\DatabaseTest\Query |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\DatabaseTest\Query; |
14
|
|
|
|
15
|
|
|
use chillerlan\Database\{DBQuery, DBResult}; |
16
|
|
|
use chillerlan\Database\Drivers\DBDriverException; |
17
|
|
|
use chillerlan\Database\Query\{ |
18
|
|
|
CreateDatabaseInterface, CreateTableInterface, DeleteInterface, |
19
|
|
|
InsertInterface, SelectInterface, StatementInterface, UpdateInterface |
20
|
|
|
}; |
21
|
|
|
use chillerlan\DatabaseTest\TestAbstract; |
22
|
|
|
|
23
|
|
|
abstract class QueryTestAbstract extends TestAbstract{ |
24
|
|
|
|
25
|
|
|
const TEST_DBNAME = 'vagrant'; |
26
|
|
|
const TEST_TABLENAME = 'querytest'; |
27
|
|
|
/** |
28
|
|
|
* @var \chillerlan\Database\DBQuery |
29
|
|
|
*/ |
30
|
|
|
protected $statement; |
31
|
|
|
|
32
|
|
|
public function setUp(){ |
33
|
|
|
parent::setUp(); |
34
|
|
|
|
35
|
|
|
$this->statement = new DBQuery($this->DBDriver); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testInstance(){ |
39
|
|
|
|
40
|
|
|
$update = $this->statement->update; |
41
|
|
|
$this->assertInstanceOf(StatementInterface::class, $update); |
42
|
|
|
$this->assertInstanceOf(UpdateInterface::class, $update); |
43
|
|
|
|
44
|
|
|
$delete = $this->statement->delete; |
45
|
|
|
$this->assertInstanceOf(StatementInterface::class, $delete); |
46
|
|
|
$this->assertInstanceOf(DeleteInterface::class, $delete); |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function createDatabase(){ |
51
|
|
|
$createdb = $this->statement->create->database(self::TEST_DBNAME); |
52
|
|
|
$this->assertInstanceOf(StatementInterface::class, $createdb); |
53
|
|
|
$this->assertInstanceOf(CreateDatabaseInterface::class, $createdb); |
54
|
|
|
|
55
|
|
|
return $createdb->name(self::TEST_DBNAME)->charset('utf8'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @expectedException \chillerlan\Database\Query\QueryException |
60
|
|
|
* @expectedExceptionMessage no name specified |
61
|
|
|
*/ |
62
|
|
|
public function testCreateDatabaseNoName(){ |
63
|
|
|
$this->statement->create->database()->sql(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testCreateTable(){ |
67
|
|
|
|
68
|
|
|
try{ |
69
|
|
|
$this->DBDriver->raw('DROP TABLE '.self::TEST_TABLENAME); |
70
|
|
|
} |
71
|
|
|
catch(DBDriverException $e){ |
72
|
|
|
var_dump('cannot drop "'.self::TEST_TABLENAME.'", table does not exist'); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$createTable = $this->statement->create->table(self::TEST_DBNAME.'.'.self::TEST_TABLENAME); |
76
|
|
|
|
77
|
|
|
$this->assertInstanceOf(StatementInterface::class, $createTable); |
78
|
|
|
$this->assertInstanceOf(CreateTableInterface::class, $createTable); |
79
|
|
|
|
80
|
|
|
$createTable->name(self::TEST_TABLENAME) |
81
|
|
|
->ifNotExists() |
82
|
|
|
# ->temp() |
83
|
|
|
->charset('utf8') |
84
|
|
|
->field('id', 'int', 10) |
85
|
|
|
->field('hash', 'varchar', 32) |
86
|
|
|
->field('data', 'text', null, null, 'utf8') |
87
|
|
|
->field('value', 'decimal', '9,6') |
88
|
|
|
->field('active', 'boolean', null, null, null, null, null, 'false') |
89
|
|
|
->field('created', 'timestamp', null, null, null, null, 'CURRENT_TIMESTAMP') |
90
|
|
|
->primaryKey('id') |
91
|
|
|
; |
92
|
|
|
|
93
|
|
|
# print_r(PHP_EOL.$createTable->sql().PHP_EOL); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$this->assertTrue($createTable->execute()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @expectedException \chillerlan\Database\Query\QueryException |
100
|
|
|
* @expectedExceptionMessage no name specified |
101
|
|
|
*/ |
102
|
|
|
public function testCreateTableNoName(){ |
103
|
|
|
$this->statement->create->table()->sql(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testInsert(){ |
107
|
|
|
$insert = $this->statement->insert; |
108
|
|
|
|
109
|
|
|
$this->assertInstanceOf(StatementInterface::class, $insert); |
110
|
|
|
$this->assertInstanceOf(InsertInterface::class, $insert); |
111
|
|
|
|
112
|
|
|
$insert |
113
|
|
|
->into(self::TEST_TABLENAME) |
114
|
|
|
->values(['id' => 0, 'hash' => md5(0), 'data' => 'foo', 'value' => 123.456, 'active' => 1]) |
115
|
|
|
; |
116
|
|
|
|
117
|
|
|
# print_r(PHP_EOL.$insert->sql().PHP_EOL); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$this->assertTrue($insert->execute()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testInsertMulti(){ |
123
|
|
|
$insert = $this->statement->insert; |
124
|
|
|
|
125
|
|
|
$insert |
126
|
|
|
->into(self::TEST_TABLENAME) |
127
|
|
|
->values([ |
128
|
|
|
['id' => 1, 'hash' => md5(1), 'data' => 'foo', 'value' => 123.456, 'active' => 0], |
129
|
|
|
['id' => 2, 'hash' => md5(2), 'data' => 'foo', 'value' => 123.456789, 'active' => 1], |
130
|
|
|
['id' => 3, 'hash' => md5(3), 'data' => 'foo', 'value' => 123.456, 'active' => 0], |
131
|
|
|
]) |
132
|
|
|
; |
133
|
|
|
|
134
|
|
|
$this->assertTrue($insert->execute()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @expectedException \chillerlan\Database\Query\QueryException |
139
|
|
|
* @expectedExceptionMessage no values given |
140
|
|
|
*/ |
141
|
|
|
public function testInsertInvalidData(){ |
142
|
|
|
$this->statement->insert->into(self::TEST_TABLENAME)->values([])->sql(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testSelect(){ |
146
|
|
|
$select = $this->statement->select; |
147
|
|
|
$this->assertInstanceOf(StatementInterface::class, $select); |
148
|
|
|
$this->assertInstanceOf(SelectInterface::class, $select); |
149
|
|
|
|
150
|
|
|
$select |
151
|
|
|
->cols([ |
152
|
|
|
'id' => 't1.id', |
153
|
|
|
'hash' => ['t1.hash', 'upper'], |
154
|
|
|
]) |
155
|
|
|
->from(['t1' => self::TEST_TABLENAME]) |
156
|
|
|
->offset(1) |
157
|
|
|
->limit(2); |
158
|
|
|
|
159
|
|
|
# print_r(PHP_EOL.$select->sql().PHP_EOL); |
|
|
|
|
160
|
|
|
$result = $select->execute(); |
161
|
|
|
$this->assertInstanceOf(DBResult::class, $result); |
162
|
|
|
$this->assertCount(2, $result); |
163
|
|
|
# print_r($result); |
|
|
|
|
164
|
|
|
|
165
|
|
|
$select = $this->statement->select; |
166
|
|
|
$select |
167
|
|
|
->cols(['id', 'hash', 'value']) |
168
|
|
|
->from([self::TEST_TABLENAME]) |
169
|
|
|
->where('active', 1) |
170
|
|
|
; |
171
|
|
|
|
172
|
|
|
# print_r(PHP_EOL.$select->sql().PHP_EOL); |
|
|
|
|
173
|
|
|
$result = $select->execute(); |
174
|
|
|
$this->assertInstanceOf(DBResult::class, $result); |
175
|
|
|
$this->assertCount(2, $result); |
176
|
|
|
# print_r($result); |
|
|
|
|
177
|
|
|
|
178
|
|
|
$select = $this->statement->select; |
179
|
|
|
$select |
180
|
|
|
->from([self::TEST_TABLENAME]) |
181
|
|
|
->where('id', [1,2,3], 'in') |
182
|
|
|
->orderby(['id' => 'desc']) |
183
|
|
|
; |
184
|
|
|
$result = $select->execute(); |
185
|
|
|
# print_r($result); |
|
|
|
|
186
|
|
|
|
187
|
|
|
if((bool)$result[0]->active){ |
188
|
|
|
// postgres t/f |
189
|
|
|
$this->markTestSkipped('['.$this->DBDriver->dialect.'] invalid boolean value'); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->assertCount(3, $result); |
193
|
|
|
$this->assertFalse((bool)$result[0]->active); |
194
|
|
|
$this->assertSame(3, (int)$result[0]->id); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @expectedException \chillerlan\Database\Query\QueryException |
199
|
|
|
* @expectedExceptionMessage no FROM expression specified |
200
|
|
|
*/ |
201
|
|
|
public function testSelectEmptyFrom(){ |
202
|
|
|
$this->statement->select->from([])->sql(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
#public function testDelete(){} |
|
|
|
|
206
|
|
|
|
207
|
|
|
#public function testUpdate(){} |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|