Skip to content

Commit 9bcbfa1

Browse files
Update ext/mysqli/tests/test_setup/test_helpers.inc
Co-authored-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent 6a1847e commit 9bcbfa1

File tree

1 file changed

+26
-91
lines changed

1 file changed

+26
-91
lines changed

ext/mysqli/tests/test_setup/test_helpers.inc

Lines changed: 26 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -76,132 +76,67 @@ function my_mysqli_connect(
7676
int $port,
7777
?string $socket = null,
7878
bool $enable_env_flags = true
79-
): \mysqli|false {
79+
): \mysqli {
80+
// Because the tests are meant to test both error modes, they can set the report_mode to a different value,
81+
// which we do not want to override. However, we want to make sure that if a connection cannot be made,
82+
// the constuctor will throw an exception. We store current report_mode in variable and restore it later.
83+
$driver = new mysqli_driver;
84+
$report_mode = $driver->report_mode;
85+
$driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
8086

81-
$flags = $enable_env_flags? get_environment_connection_flags():0;
87+
$flags = $enable_env_flags ? get_environment_connection_flags() : 0;
8288
if ($flags !== 0) {
8389
$link = mysqli_init();
84-
if (!mysqli_real_connect($link, $host, $user, $password, $db, $port, $socket, $flags)) {
85-
$link = false;
86-
}
90+
mysqli_real_connect($link, $host, $user, $password, $db, $port, $socket, $flags);
8791
} else {
8892
$link = mysqli_connect($host, $user, $password, $db, $port, $socket);
8993
}
9094

91-
return $link;
92-
}
93-
94-
/**
95-
* Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
96-
*
97-
* @param bool $enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)
98-
*/
99-
function my_mysqli_real_connect(
100-
mysqli $link,
101-
string $host,
102-
string $user,
103-
string $password,
104-
string $db,
105-
int $port,
106-
?string $socket = null,
107-
int $flags = 0,
108-
bool $enable_env_flags = true
109-
): \mysqli|false {
110-
if ($enable_env_flags) {
111-
$flags = $flags | get_environment_connection_flags();
112-
}
95+
// Restore error mode
96+
$driver->report_mode = $report_mode;
11397

114-
return mysqli_real_connect($link, $host, $user, $password, $db, $port, $socket, $flags);
98+
return $link;
11599
}
116100

117-
function default_mysqli_connect_ex(): \mysqli|false {
118-
return my_mysqli_connect(
119-
get_default_host(),
120-
get_default_user(),
121-
get_default_password(),
122-
get_default_database(),
123-
get_default_port(),
124-
null,
125-
);
126-
}
127101
function default_mysqli_connect(): \mysqli{
128-
$link = default_mysqli_connect_ex();
129-
if (!$link) {
130-
throw new Error("Cannot connect to default connection");
131-
}
132-
return $link;
133-
}
134-
function default_mysqli_real_connect(mysqli $link, int $flags = 0): bool {
135-
return my_mysqli_real_connect(
136-
$link,
102+
return my_mysqli_connect(
137103
get_default_host(),
138104
get_default_user(),
139105
get_default_password(),
140106
get_default_database(),
141107
get_default_port(),
142108
null,
143-
$flags,
144109
);
145110
}
146111

147112
function mysqli_check_skip_test(): void {
148-
/* Disable exceptions */
149-
mysqli_report(MYSQLI_REPORT_OFF);
150-
$link = default_mysqli_connect_ex();
151-
// Re-enable exceptions
152-
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
153-
if (!is_object($link)) {
113+
try {
114+
$link = default_mysqli_connect();
115+
} catch (\mysqli_sql_exception) {
154116
die(sprintf("skip Can't connect to MySQL Server - [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));
155117
}
156-
mysqli_close($link);
157118
}
158119

159120
function have_innodb(mysqli $link): bool {
160-
if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'"))
161-
&& ($row = $res->fetch_row())
162-
&& !empty($row)
163-
) {
164-
return !($row[1] == 'DISABLED' || $row[1] == 'NO');
165-
}
166-
// MySQL 5.6.1+
167-
if ($res = $link->query('SHOW ENGINES')) {
168-
while ($row = $res->fetch_assoc()) {
169-
if (!isset($row['Engine']) || !isset($row['Support'])) {
170-
return false;
171-
}
172-
173-
if (($row['Engine'] == 'InnoDB')
174-
&& (($row['Support'] == 'YES') || ($row['Support'] == 'DEFAULT'))
175-
) {
176-
return true;
177-
}
178-
}
179-
}
180-
return false;
121+
$res = $link->query("SELECT SUPPORT FROM INFORMATION_SCHEMA.ENGINES WHERE ENGINE = 'InnoDB'");
122+
$supported = $res->fetch_column();
123+
return $supported === 'YES' || $supported === 'DEFAULT';
181124
}
125+
182126
function mysqli_check_innodb_support_skip_test(): void {
183-
/* Disable exceptions */
184-
mysqli_report(MYSQLI_REPORT_OFF);
185-
$link = default_mysqli_connect_ex();
186-
if (!is_object($link)) {
187-
// Re-enable exceptions
188-
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
127+
try {
128+
$link = default_mysqli_connect();
129+
} catch (\mysqli_sql_exception) {
189130
die(sprintf("skip Can't connect to MySQL Server - [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));
190131
}
191-
$status = have_innodb($link);
192-
// Re-enable exceptions
193-
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
194-
mysqli_close($link);
195-
if (!$status) {
196-
die(sprintf("skip Needs InnoDB support, [%d] %s", $link->errno, $link->error));
132+
if (! have_innodb($link)) {
133+
die(sprintf("skip Needs InnoDB support"));
197134
}
198135
}
199136

200137
function tear_down_table_on_default_connection(string $table) {
201138
$link = default_mysqli_connect();
202-
if (!mysqli_query($link, 'DROP TABLE IF EXISTS ' . $table)) {
203-
printf("[clean] Failed to drop \"$table\" table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
204-
}
139+
mysqli_query($link, 'DROP TABLE IF EXISTS ' . $table);
205140
}
206141

207142
function setup_table_with_data_on_default_connection(string $table): mysqli {

0 commit comments

Comments
 (0)