1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\Tests\Dbal; |
6
|
|
|
|
7
|
|
|
use mysqli; |
8
|
|
|
use mysqli_result; |
9
|
|
|
use PhpMyAdmin\Dbal\DbiMysqli; |
10
|
|
|
use PhpMyAdmin\Tests\AbstractTestCase; |
11
|
|
|
|
12
|
|
|
use const MYSQLI_ASSOC; |
13
|
|
|
use const MYSQLI_BOTH; |
14
|
|
|
use const MYSQLI_NUM; |
15
|
|
|
use const PHP_VERSION_ID; |
16
|
|
|
|
17
|
|
|
class DbiMysqliTest extends AbstractTestCase |
18
|
|
|
{ |
19
|
|
|
/** @var DbiMysqli */ |
20
|
|
|
protected $object; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Sets up the fixture, for example, opens a network connection. |
24
|
|
|
* This method is called before a test is executed. |
25
|
|
|
* |
26
|
|
|
* @access protected |
27
|
|
|
*/ |
28
|
|
|
protected function setUp(): void |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
$this->object = new DbiMysqli(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetClientInfo(): void |
35
|
|
|
{ |
36
|
|
|
if (PHP_VERSION_ID < 80100) { |
37
|
|
|
$this->markTestSkipped('This test requires PHP 8.1'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @var mysqli $obj */ |
41
|
|
|
$obj = null; |
42
|
|
|
$this->assertNotEmpty($this->object->getClientInfo($obj)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Test for selectDb |
47
|
|
|
*/ |
48
|
|
|
public function testSelectDb(): void |
49
|
|
|
{ |
50
|
|
|
$databaseName = 'test'; |
51
|
|
|
$mysqli = $this->createMock(mysqli::class); |
52
|
|
|
$mysqli->expects($this->once()) |
53
|
|
|
->method('select_db') |
54
|
|
|
->with($this->equalTo($databaseName)) |
55
|
|
|
->willReturn(true); |
56
|
|
|
|
57
|
|
|
$this->assertTrue($this->object->selectDb($databaseName, $mysqli)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Test for realMultiQuery |
62
|
|
|
*/ |
63
|
|
|
public function testRealMultiQuery(): void |
64
|
|
|
{ |
65
|
|
|
$query = 'test'; |
66
|
|
|
$mysqli = $this->createMock(mysqli::class); |
67
|
|
|
$mysqli->expects($this->once()) |
68
|
|
|
->method('multi_query') |
69
|
|
|
->with($this->equalTo($query)) |
70
|
|
|
->willReturn(true); |
71
|
|
|
|
72
|
|
|
$this->assertTrue($this->object->realMultiQuery($mysqli, $query)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Test for fetchArray |
77
|
|
|
*/ |
78
|
|
|
public function testFetchArray(): void |
79
|
|
|
{ |
80
|
|
|
$expected = []; |
81
|
|
|
$result = $this->createMock(mysqli_result::class); |
82
|
|
|
$result->expects($this->once()) |
83
|
|
|
->method('fetch_array') |
84
|
|
|
->with($this->equalTo(MYSQLI_BOTH)) |
85
|
|
|
->willReturn($expected); |
86
|
|
|
|
87
|
|
|
$this->assertEquals($expected, $this->object->fetchArray($result)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Test for fetchAssoc |
92
|
|
|
*/ |
93
|
|
|
public function testFetchAssoc(): void |
94
|
|
|
{ |
95
|
|
|
$expected = []; |
96
|
|
|
$result = $this->createMock(mysqli_result::class); |
97
|
|
|
$result->expects($this->once()) |
98
|
|
|
->method('fetch_array') |
99
|
|
|
->with($this->equalTo(MYSQLI_ASSOC)) |
100
|
|
|
->willReturn($expected); |
101
|
|
|
|
102
|
|
|
$this->assertEquals($expected, $this->object->fetchAssoc($result)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test for fetchRow |
107
|
|
|
*/ |
108
|
|
|
public function testFetchRow(): void |
109
|
|
|
{ |
110
|
|
|
$expected = []; |
111
|
|
|
$result = $this->createMock(mysqli_result::class); |
112
|
|
|
$result->expects($this->once()) |
113
|
|
|
->method('fetch_array') |
114
|
|
|
->with($this->equalTo(MYSQLI_NUM)) |
115
|
|
|
->willReturn($expected); |
116
|
|
|
|
117
|
|
|
$this->assertEquals($expected, $this->object->fetchRow($result)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Test for dataSeek |
122
|
|
|
*/ |
123
|
|
|
public function testDataSeek(): void |
124
|
|
|
{ |
125
|
|
|
$offset = 1; |
126
|
|
|
$result = $this->createMock(mysqli_result::class); |
127
|
|
|
$result->expects($this->once()) |
128
|
|
|
->method('data_seek') |
129
|
|
|
->with($this->equalTo($offset)) |
130
|
|
|
->willReturn(true); |
131
|
|
|
|
132
|
|
|
$this->assertTrue($this->object->dataSeek($result, $offset)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Test for freeResult |
137
|
|
|
*/ |
138
|
|
|
public function testFreeResult(): void |
139
|
|
|
{ |
140
|
|
|
$result = $this->createMock(mysqli_result::class); |
141
|
|
|
$result->expects($this->once()) |
142
|
|
|
->method('close'); |
143
|
|
|
|
144
|
|
|
$this->object->freeResult($result); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Test for moreResults |
149
|
|
|
*/ |
150
|
|
|
public function testMoreResults(): void |
151
|
|
|
{ |
152
|
|
|
$mysqli = $this->createMock(mysqli::class); |
153
|
|
|
$mysqli->expects($this->once()) |
154
|
|
|
->method('more_results') |
155
|
|
|
->willReturn(true); |
156
|
|
|
|
157
|
|
|
$this->assertTrue($this->object->moreResults($mysqli)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Test for nextResult |
162
|
|
|
*/ |
163
|
|
|
public function testNextResult(): void |
164
|
|
|
{ |
165
|
|
|
$mysqli = $this->createMock(mysqli::class); |
166
|
|
|
$mysqli->expects($this->once()) |
167
|
|
|
->method('next_result') |
168
|
|
|
->willReturn(true); |
169
|
|
|
|
170
|
|
|
$this->assertTrue($this->object->nextResult($mysqli)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Test for storeResult |
175
|
|
|
*/ |
176
|
|
|
public function testStoreResult(): void |
177
|
|
|
{ |
178
|
|
|
$mysqli = $this->createMock(mysqli::class); |
179
|
|
|
$mysqli->expects($this->once()) |
180
|
|
|
->method('store_result') |
181
|
|
|
->willReturn(true); |
182
|
|
|
|
183
|
|
|
$this->assertTrue($this->object->storeResult($mysqli)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Test for numRows |
188
|
|
|
*/ |
189
|
|
|
public function testNumRows(): void |
190
|
|
|
{ |
191
|
|
|
$this->assertEquals(0, $this->object->numRows(false)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Test for escapeString |
196
|
|
|
*/ |
197
|
|
|
public function testEscapeString(): void |
198
|
|
|
{ |
199
|
|
|
$string = 'test'; |
200
|
|
|
$mysqli = $this->createMock(mysqli::class); |
201
|
|
|
$mysqli->expects($this->once()) |
202
|
|
|
->method('real_escape_string') |
203
|
|
|
->willReturn($string); |
204
|
|
|
|
205
|
|
|
$this->assertEquals($string, $this->object->escapeString($mysqli, $string)); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|