1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class DriverTestAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource DriverTestAbstract.php |
6
|
|
|
* @created 29.05.2017 |
7
|
|
|
* @package chillerlan\DatabaseTest\Drivers |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\DatabaseTest\Drivers; |
14
|
|
|
|
15
|
|
|
use chillerlan\Database\DBResult; |
16
|
|
|
use chillerlan\Database\Drivers\DBDriverInterface; |
17
|
|
|
use chillerlan\DatabaseTest\TestAbstract; |
18
|
|
|
|
19
|
|
|
abstract class DriverTestAbstract extends TestAbstract{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \chillerlan\Database\Drivers\DBDriverInterface |
23
|
|
|
*/ |
24
|
|
|
protected $DBDriver; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
protected $driver; |
28
|
|
|
|
29
|
|
|
protected $SQL_RAW_TRUNCATE = 'DELETE FROM test'; |
30
|
|
|
protected $SQL_RAW_SELECT_ALL = 'SELECT * FROM test'; |
31
|
|
|
protected $SQL_RAW_DROP = 'DROP TABLE IF EXISTS test'; |
32
|
|
|
protected $SQL_RAW_CREATE = 'CREATE TABLE IF NOT EXISTS test (id INTEGER NOT NULL, hash VARCHAR(32) NOT NULL)'; |
33
|
|
|
protected $SQL_RAW_INSERT = 'INSERT INTO test (id, hash) VALUES (%1$d, \'%2$s\')'; |
34
|
|
|
protected $SQL_RAW_ERROR = '-'; |
35
|
|
|
|
36
|
|
|
protected $SQL_PREPARED_INSERT = 'INSERT INTO test (id, hash) VALUES (?, ?)'; |
37
|
|
|
|
38
|
|
|
protected $SQL_INDEX_COL = 'id'; |
39
|
|
|
protected $SQL_DATA_COL = 'hash'; |
40
|
|
|
|
41
|
|
|
protected function setUp(){ |
42
|
|
|
parent::setUp(); |
43
|
|
|
|
44
|
|
|
$this->DBDriver = (new $this->driver($this->dbOptions))->connect(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function tearDown(){ |
48
|
|
|
$this->DBDriver->disconnect(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function resultTest(){ |
52
|
|
|
$DBResult = $this->DBDriver->raw($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL); |
53
|
|
|
$DBResultPrepared = $this->DBDriver->prepared($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL); // coverage |
54
|
|
|
|
55
|
|
|
$this->assertEquals($DBResult, $DBResultPrepared); |
56
|
|
|
|
57
|
|
|
$this->assertCount(10, $DBResult); |
58
|
|
|
|
59
|
|
View Code Duplication |
$DBResult->__each(function($row, $i){ |
|
|
|
|
60
|
|
|
/** @var \chillerlan\Database\DBResultRow $row */ |
61
|
|
|
$this->assertEquals($row->{$this->SQL_INDEX_COL}, $i); |
62
|
|
|
$this->assertSame(md5($row->{$this->SQL_INDEX_COL}), $row->{$this->SQL_DATA_COL}()); |
63
|
|
|
|
64
|
|
|
$row->__each(function($v, $j) use ($row){ |
65
|
|
|
$this->assertEquals($row->{$j}, $v); |
66
|
|
|
$this->assertEquals($row[$j], $v); |
67
|
|
|
}); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_TRUNCATE)); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testInstance(){ |
74
|
|
|
$this->assertInstanceOf(DBDriverInterface::class, $this->DBDriver); |
75
|
|
|
$this->assertNotNull($this->DBDriver->getDBResource()); |
76
|
|
|
|
77
|
|
|
print_r([ |
78
|
|
|
'driver' => get_class($this->DBDriver), |
79
|
|
|
'client' => $this->DBDriver->getClientInfo(), |
80
|
|
|
'server' => $this->DBDriver->getServerInfo(), |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testCreateTable(){ |
85
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_DROP)); |
|
|
|
|
86
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_CREATE)); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testTruncate(){ |
90
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_TRUNCATE)); |
|
|
|
|
91
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_SELECT_ALL)); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @expectedException \chillerlan\Database\DBException |
96
|
|
|
* @expectedExceptionMessage sql error: |
97
|
|
|
*/ |
98
|
|
|
public function testRawSQLError(){ |
99
|
|
|
$this->DBDriver->raw($this->SQL_RAW_ERROR); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testRaw(){ |
103
|
|
|
|
104
|
|
|
// don't try this at home! |
105
|
|
|
foreach(range(0, 9) as $k){ |
106
|
|
|
$this->assertTrue($this->DBDriver->raw(sprintf($this->SQL_RAW_INSERT, $k, md5($k)))); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->resultTest(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @expectedException \chillerlan\Database\DBException |
114
|
|
|
* @expectedExceptionMessage sql error: |
115
|
|
|
*/ |
116
|
|
|
public function testPreparedSQLError(){ |
117
|
|
|
$this->DBDriver->prepared($this->SQL_RAW_ERROR); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function testPrepared(){ |
|
|
|
|
121
|
|
|
|
122
|
|
|
foreach(range(0, 9) as $k){ |
123
|
|
|
$this->assertTrue($this->DBDriver->prepared($this->SQL_PREPARED_INSERT, [$k, md5($k)])); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->resultTest(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @expectedException \chillerlan\Database\DBException |
131
|
|
|
* @expectedExceptionMessage invalid data |
132
|
|
|
*/ |
133
|
|
|
public function testMultiInvalidData(){ |
134
|
|
|
$this->DBDriver->multi($this->SQL_RAW_ERROR, []); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @expectedException \chillerlan\Database\DBException |
139
|
|
|
* @expectedExceptionMessage sql error: |
140
|
|
|
*/ |
141
|
|
|
public function testMultiSQLError(){ |
142
|
|
|
$this->DBDriver->multi($this->SQL_RAW_ERROR, [[0]]); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
View Code Duplication |
public function testMulti(){ |
|
|
|
|
146
|
|
|
|
147
|
|
|
$this->assertTrue($this->DBDriver->multi($this->SQL_PREPARED_INSERT, array_map(function($k){ |
148
|
|
|
return [$k, md5($k)]; |
149
|
|
|
}, range(0, 9)))); |
150
|
|
|
|
151
|
|
|
$this->resultTest(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @expectedException \chillerlan\Database\DBException |
156
|
|
|
* @expectedExceptionMessage invalid data |
157
|
|
|
*/ |
158
|
|
|
public function testMultiCallbackInvalidData(){ |
159
|
|
|
$this->DBDriver->multi_callback($this->SQL_RAW_ERROR, [], function(){}); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @expectedException \chillerlan\Database\DBException |
164
|
|
|
* @expectedExceptionMessage invalid callback |
165
|
|
|
*/ |
166
|
|
|
public function testMultiCallbackInvalidCallback(){ |
167
|
|
|
$this->DBDriver->multi_callback($this->SQL_RAW_ERROR, [[0]], ''); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @expectedException \chillerlan\Database\DBException |
172
|
|
|
* @expectedExceptionMessage sql error: |
173
|
|
|
*/ |
174
|
|
|
public function testMultiCallbackSQLError(){ |
175
|
|
|
$this->DBDriver->multi_callback($this->SQL_RAW_ERROR, [[0]], function(){return [];}); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
View Code Duplication |
public function testMultiCallback(){ |
|
|
|
|
179
|
|
|
|
180
|
|
|
$this->assertTrue($this->DBDriver->multi_callback($this->SQL_PREPARED_INSERT, range(0, 9), function($k){ |
181
|
|
|
return [$k, md5($k)]; |
182
|
|
|
})); |
183
|
|
|
|
184
|
|
|
$this->resultTest(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.