Skip to content

Commit

Permalink
Improve mysqli_character_set_name tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-tekiela committed Sep 19, 2023
1 parent f957335 commit 5f6bf3e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 97 deletions.
72 changes: 27 additions & 45 deletions ext/mysqli/tests/mysqli_character_set_name.phpt
@@ -1,5 +1,5 @@
--TEST--
mysqli_character_set_name(), mysql_client_encoding() [alias]
mysqli_character_set_name()
--EXTENSIONS--
mysqli
--SKIPIF--
Expand All @@ -8,55 +8,37 @@ require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php
/* NOTE: http://bugs.mysql.com/bug.php?id=7923 makes this test fail very likely on all 4.1.x - 5.0.x! */
require_once 'connect.inc';

if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
printf("[005] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
$host, $user, $db, $port, $socket);

if (!$res = mysqli_query($link, 'SELECT version() AS server_version'))
printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
$tmp = mysqli_fetch_assoc($res);
mysqli_free_result($res);
$version = explode('.', $tmp['server_version']);
if (empty($version))
printf("[006] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");

if ($version[0] <= 4 && $version[1] < 1)
printf("[007] Need MySQL Server 4.1+ for the test!\n");

if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
$tmp = mysqli_fetch_assoc($res);
mysqli_free_result($res);
if (!$tmp['charset'])
printf("[009] Cannot determine current character set and collation\n");

$charset = mysqli_character_set_name($link);
if ($tmp['charset'] !== $charset) {
if ($tmp['collation'] === $charset) {
printf("[010] Could be known server bug http://bugs.mysql.com/bug.php?id=7923, collation %s instead of character set returned, expected string/%s, got %s/%s\n",
/* NOTE: http://bugs.mysql.com/bug.php?id=7923 makes this test fail very likely on all 4.1.x - 5.0.x! */
require_once 'connect.inc';

$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);

if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
printf("[001] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
$tmp = mysqli_fetch_assoc($res);
mysqli_free_result($res);
if (!$tmp['charset'])
printf("[002] Cannot determine current character set and collation\n");

$charset = mysqli_character_set_name($link);
if ($tmp['charset'] !== $charset) {
if ($tmp['collation'] === $charset) {
printf("[003] Could be known server bug http://bugs.mysql.com/bug.php?id=7923, collation %s instead of character set returned, expected string/%s, got %s/%s\n",
$tmp['collation'], $tmp['charset'], gettype($charset), $charset);
} else {
printf("[011] Expecting character set %s/%s, got %s/%s\n", gettype($tmp['charset']), $tmp['charset'], gettype($charset), $charset);
}
}

$charset2 = mysqli_character_set_name($link);
if ($charset2 !== $charset) {
printf("[012] Alias mysqli_character_set_name returned %s/%s, expected %s/%s\n", gettype($charset2), $charset2, gettype($charset), $charset);
} else {
printf("[004] Expecting character set %s/%s, got %s/%s\n", gettype($tmp['charset']), $tmp['charset'], gettype($charset), $charset);
}
}

mysqli_close($link);
mysqli_close($link);

try {
mysqli_character_set_name($link);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}
try {
mysqli_character_set_name($link);
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

print "done!";
print "done!";
?>
--EXPECT--
mysqli object is already closed
Expand Down
80 changes: 28 additions & 52 deletions ext/mysqli/tests/mysqli_character_set_name_oo.phpt
@@ -1,5 +1,5 @@
--TEST--
mysqli_character_set_name(), mysql_client_encoding() [alias]
mysqli_character_set_name()
--EXTENSIONS--
mysqli
--SKIPIF--
Expand All @@ -8,62 +8,38 @@ require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php
/* NOTE: http://bugs.mysql.com/bug.php?id=7923 makes this test fail very likely on all 4.1.x - 5.0.x! */
require_once 'connect.inc';

$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);

if (!$res = $mysqli->query('SELECT version() AS server_version'))
printf("[003] [%d] %s\n", $mysqli->errno, $mysqli->error);
$tmp = $res->fetch_assoc();
$res->free_result();
$version = explode('.', $tmp['server_version']);
if (empty($version))
printf("[006] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");

if ($version[0] <= 4 && $version[1] < 1)
printf("[007] Need MySQL Server 4.1+ for the test!\n");

if (!$res = $mysqli->query('SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
printf("[008] [%d] %s\n", $mysqli->errno, $mysqli->error);
$tmp = $res->fetch_assoc();
$res->free_result();
if (!$tmp['charset'])
printf("[009] Cannot determine current character set and collation\n");

$charset = $mysqli->character_set_name();
if ($tmp['charset'] !== $charset) {
if ($tmp['collation'] === $charset) {
printf("[010] Could be known server bug http://bugs.mysql.com/bug.php?id=7923, collation %s instead of character set returned, expected string/%s, got %s/%s\n",
$tmp['collation'], $tmp['charset'], gettype($charset), $charset);
} else {
printf("[011] Expecting character set %s/%s, got %s/%s\n", gettype($tmp['charset']), $tmp['charset'], gettype($charset), $charset);
}
/* NOTE: http://bugs.mysql.com/bug.php?id=7923 makes this test fail very likely on all 4.1.x - 5.0.x! */
require_once 'connect.inc';

$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);

if (!$res = $mysqli->query('SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
printf("[001] [%d] %s\n", $mysqli->errno, $mysqli->error);
$tmp = $res->fetch_assoc();
$res->free_result();
if (!$tmp['charset'])
printf("[002] Cannot determine current character set and collation\n");

$charset = $mysqli->character_set_name();
if ($tmp['charset'] !== $charset) {
if ($tmp['collation'] === $charset) {
printf("[003] Could be known server bug http://bugs.mysql.com/bug.php?id=7923, collation %s instead of character set returned, expected string/%s, got %s/%s\n",
$tmp['collation'], $tmp['charset'], gettype($charset), $charset);
} else {
printf("[004] Expecting character set %s/%s, got %s/%s\n", gettype($tmp['charset']), $tmp['charset'], gettype($charset), $charset);
}
}

$charset2 = $mysqli->character_set_name();
if ($charset2 !== $charset) {
printf("[012] Alias mysqli_character_set_name returned %s/%s, expected %s/%s\n",
gettype($charset2), $charset2, gettype($charset), $charset);
}
$mysqli->close();

$mysqli->close();
try {
$mysqli->character_set_name();
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

try {
$mysqli->character_set_name();
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

try {
$mysqli->character_set_name();
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

print "done!";
print "done!";
?>
--EXPECT--
my_mysqli object is already closed
my_mysqli object is already closed
done!

0 comments on commit 5f6bf3e

Please sign in to comment.