Skip to content

Commit 1ab3856

Browse files
author
Ulf Wendel
committed
Let's try not to have 'uft8'-only tests and avoid setting the charset upon connect. I've changed the tests to make no assumptions on the server charset setting. var_dump()/EXPECTF tests got replaced with dynamic tests which try to take the current charset into account.
1 parent a6adb25 commit 1ab3856

8 files changed

+209
-33
lines changed

ext/mysqli/tests/connect.inc

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
if ($socket) {
2121
ini_set('mysqli.default_socket', $socket);
2222
}
23+
2324
/* Development setting: test experimal features and/or feature requests that never worked before? */
2425
$TEST_EXPERIMENTAL = (in_array(getenv("MYSQL_TEST_EXPERIMENTAL"), array(0, 1))) ?
2526
((1 == getenv("MYSQL_TEST_EXPERIMENTAL")) ? true : false) :
@@ -90,12 +91,10 @@
9091

9192
if ($flags !== false) {
9293
$link = mysqli_init();
93-
$link->options(MYSQLI_SET_CHARSET_NAME, "utf8");
9494
if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags))
9595
$link = false;
9696
} else {
9797
$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket);
98-
$link->set_charset("utf8");
9998
}
10099

101100
return $link;
@@ -129,6 +128,100 @@
129128
}
130129
}
131130
}
131+
132+
function my_get_charsets($link) {
133+
134+
/* Those tree are set by SET NAMES */
135+
$charsets = array(
136+
'client' => NULL,
137+
'results' => NULL,
138+
'connection' => NULL,
139+
);
140+
141+
if (!($res = mysqli_query($link, "SHOW VARIABLES LIKE '%character%'"))) {
142+
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
143+
return $charsets;
144+
}
145+
146+
$names = array();
147+
while ($row = mysqli_fetch_assoc($res)) {
148+
$names[$row['Variable_name']] = $row['Value'];
149+
}
150+
mysqli_free_result($res);
151+
152+
if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_client']))) ||
153+
!($details = mysqli_fetch_assoc($res))) {
154+
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
155+
return $charsets;
156+
}
157+
mysqli_free_result($res);
158+
159+
$charsets['client'] = array(
160+
'charset' => $details['Charset'],
161+
'desc' => $details['Description'],
162+
'collation' => $details['Default collation'],
163+
'maxlen' => $details['Maxlen'],
164+
'nr' => NULL,
165+
);
166+
167+
if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
168+
!($collation = mysqli_fetch_assoc($res))) {
169+
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
170+
return $charsets;
171+
}
172+
mysqli_free_result($res);
173+
$charsets['client']['nr'] = $collation['Id'];
174+
175+
if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_results']))) ||
176+
!($details = mysqli_fetch_assoc($res))) {
177+
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
178+
return $charsets;
179+
}
180+
mysqli_free_result($res);
181+
182+
$charsets['results'] = array(
183+
'charset' => $details['Charset'],
184+
'desc' => $details['Description'],
185+
'collation' => $details['Default collation'],
186+
'maxlen' => $details['Maxlen'],
187+
'nr' => NULL,
188+
);
189+
190+
if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
191+
!($collation = mysqli_fetch_assoc($res))) {
192+
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
193+
return $charsets;
194+
}
195+
mysqli_free_result($res);
196+
$charsets['results']['nr'] = $collation['Id'];
197+
198+
199+
if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_connection']))) ||
200+
!($details = mysqli_fetch_assoc($res))) {
201+
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
202+
return $charsets;
203+
}
204+
mysqli_free_result($res);
205+
206+
$charsets['connection'] = array(
207+
'charset' => $details['Charset'],
208+
'desc' => $details['Description'],
209+
'collation' => $details['Default collation'],
210+
'maxlen' => $details['Maxlen'],
211+
'nr' => NULL,
212+
);
213+
214+
if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
215+
!($collation = mysqli_fetch_assoc($res))) {
216+
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
217+
return $charsets;
218+
}
219+
mysqli_free_result($res);
220+
$charsets['connection']['nr'] = $collation['Id'];
221+
222+
return $charsets;
223+
}
224+
132225
} else {
133226
printf("skip Eeeek/BUG/FIXME - connect.inc included twice! skipif bug?\n");
134227
}

ext/mysqli/tests/mysqli_change_user_set_names.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ $tmp = mysqli_fetch_assoc($res);
1616
mysqli_free_result($res);
1717
$version = explode('.', $tmp['server_version']);
1818
if (empty($version))
19-
die(sprintf("skip Cannot determine server version, need MySQL Server 4.1+ for the test!"));
19+
die(sprintf("skip Cannot determine server version, we need MySQL Server 4.1+ for the test!"));
2020

2121
if ($version[0] <= 4 && $version[1] < 1)
22-
die(sprintf("ski Need MySQL Server 4.1+ for the test!"));
22+
die(sprintf("skip We need MySQL Server 4.1+ for the test!"));
2323
?>
2424
--FILE--
2525
<?php

ext/mysqli/tests/mysqli_fetch_field.phpt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,51 @@ require_once('skipifconnectfailure.inc');
2222

2323
require('table.inc');
2424

25+
$charsets = my_get_charsets($link);
2526
if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
2627
printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
2728
}
2829

29-
while ($tmp = mysqli_fetch_field($res))
30-
var_dump($tmp);
30+
/* ID column, binary charset */
31+
$tmp = mysqli_fetch_field($res);
3132
var_dump($tmp);
3233

34+
/* label column, result set charset */
35+
$tmp = mysqli_fetch_field($res);
36+
var_dump($tmp);
37+
if ($tmp->charsetnr != $charsets['results']['nr']) {
38+
printf("[004] Expecting charset %s/%d got %d\n",
39+
$charsets['results']['charset'],
40+
$charsets['results']['nr'], $tmp->charsetnr);
41+
}
42+
if ($tmp->length != (1 * $charsets['results']['maxlen'])) {
43+
printf("[005] Expecting length %d got %d\n",
44+
$charsets['results']['maxlen'],
45+
$tmp->max_length);
46+
}
47+
48+
var_dump(mysqli_fetch_field($res));
49+
3350
mysqli_free_result($res);
3451

3552
// Read http://bugs.php.net/bug.php?id=42344 on defaults!
3653
if (NULL !== ($tmp = mysqli_fetch_field($res)))
37-
printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
54+
printf("[006] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
3855

3956
if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
40-
printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
57+
printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
4158

4259
if (!mysqli_query($link, "CREATE TABLE test(id INT NOT NULL DEFAULT 1)"))
43-
printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
60+
printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
4461

4562
if (!mysqli_query($link, "INSERT INTO test(id) VALUES (2)"))
46-
printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
63+
printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
4764

4865
if (!$res = mysqli_query($link, "SELECT id as _default_test FROM test")) {
49-
printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
66+
printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
5067
}
5168
var_dump(mysqli_fetch_assoc($res));
69+
/* binary */
5270
var_dump(mysqli_fetch_field($res));
5371
mysqli_free_result($res);
5472

@@ -97,7 +115,7 @@ object(stdClass)#%d (11) {
97115
[%u|b%"def"]=>
98116
%unicode|string%(0) ""
99117
[%u|b%"max_length"]=>
100-
int(1)
118+
int(%d)
101119
[%u|b%"length"]=>
102120
int(%d)
103121
[%u|b%"charsetnr"]=>

ext/mysqli/tests/mysqli_fetch_field_oo.phpt

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,36 @@ require_once('skipifconnectfailure.inc');
2424
printf("[002] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
2525
$host, $user, $db, $port, $socket);
2626

27-
if (!$res = $mysqli->query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
28-
printf("[003] [%d] %s\n", $mysqli->errno, $mysqli->error);
29-
}
30-
3127
if (!is_null($tmp = @$res->fetch_field($link)))
3228
printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
3329

34-
while ($tmp = $res->fetch_field()) {
35-
var_dump($tmp);
30+
$charsets = my_get_charsets($link);
31+
32+
if (!$res = $mysqli->query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
33+
printf("[004] [%d] %s\n", $mysqli->errno, $mysqli->error);
3634
}
35+
36+
var_dump($res->fetch_field());
37+
38+
$tmp = $res->fetch_field();
3739
var_dump($tmp);
40+
if ($tmp->charsetnr != $charsets['results']['nr']) {
41+
printf("[005] Expecting charset %s/%d got %d\n",
42+
$charsets['results']['charset'],
43+
$charsets['results']['nr'], $tmp->charsetnr);
44+
}
45+
if ($tmp->length != (1 * $charsets['results']['maxlen'])) {
46+
printf("[006] Expecting length %d got %d\n",
47+
$charsets['results']['maxlen'],
48+
$tmp->max_length);
49+
}
50+
51+
var_dump($res->fetch_field());
3852

3953
$res->free_result();
4054

4155
if (NULL !== ($tmp = $res->fetch_field()))
42-
printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
56+
printf("[007] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
4357

4458
$mysqli->close();
4559
print "done!";
@@ -85,7 +99,7 @@ object(stdClass)#%d (11) {
8599
[%u|b%"def"]=>
86100
%unicode|string%(0) ""
87101
[%u|b%"max_length"]=>
88-
int(1)
102+
int(%d)
89103
[%u|b%"length"]=>
90104
int(%d)
91105
[%u|b%"charsetnr"]=>

ext/mysqli/tests/mysqli_fetch_fields.phpt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,31 @@ require_once('skipifconnectfailure.inc');
2121
printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
2222

2323
require('table.inc');
24+
$charsets = my_get_charsets($link);
25+
2426
if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
2527
printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
2628
}
2729

2830
$fields = mysqli_fetch_fields($res);
29-
foreach ($fields as $k => $field)
31+
foreach ($fields as $k => $field) {
3032
var_dump($field);
33+
switch ($k) {
34+
case 1:
35+
/* label column, result set charset */
36+
if ($field->charsetnr != $charsets['results']['nr']) {
37+
printf("[004] Expecting charset %s/%d got %d\n",
38+
$charsets['results']['charset'],
39+
$charsets['results']['nr'], $field->charsetnr);
40+
}
41+
if ($field->length != (1 * $charsets['results']['maxlen'])) {
42+
printf("[005] Expecting length %d got %d\n",
43+
$charsets['results']['maxlen'],
44+
$field->max_length);
45+
}
46+
break;
47+
}
48+
}
3149

3250
mysqli_free_result($res);
3351

ext/mysqli/tests/mysqli_field_seek.phpt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ require_once('skipifconnectfailure.inc');
6666
printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
6767

6868
require('table.inc');
69+
$charsets = my_get_charsets($link);
70+
6971
if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1", MYSQLI_USE_RESULT)) {
7072
printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
7173
}
@@ -75,7 +77,21 @@ require_once('skipifconnectfailure.inc');
7577
var_dump(mysqli_field_seek($res, 0));
7678
var_dump(mysqli_fetch_field($res));
7779
var_dump(mysqli_field_seek($res, 1));
78-
var_dump(mysqli_fetch_field($res));
80+
81+
$field = mysqli_fetch_field($res);
82+
var_dump($field);
83+
/* label column, result set charset */
84+
if ($field->charsetnr != $charsets['results']['nr']) {
85+
printf("[004] Expecting charset %s/%d got %d\n",
86+
$charsets['results']['charset'],
87+
$charsets['results']['nr'], $field->charsetnr);
88+
}
89+
if ($field->length != (1 * $charsets['results']['maxlen'])) {
90+
printf("[005] Expecting length %d got %d\n",
91+
$charsets['results']['maxlen'],
92+
$field->max_length);
93+
}
94+
7995
var_dump(mysqli_field_tell($res));
8096
var_dump(mysqli_field_seek($res, 2));
8197
var_dump(mysqli_fetch_field($res));
@@ -96,8 +112,6 @@ require_once('skipifconnectfailure.inc');
96112

97113
var_dump(mysqli_field_seek($res, 0));
98114

99-
100-
101115
mysqli_close($link);
102116
print "done!";
103117
?>
@@ -170,7 +184,7 @@ object(stdClass)#%d (11) {
170184
[%u|b%"def"]=>
171185
%unicode|string%(0) ""
172186
[%u|b%"max_length"]=>
173-
int(0)
187+
int(%d)
174188
[%u|b%"length"]=>
175189
int(%d)
176190
[%u|b%"charsetnr"]=>

ext/mysqli/tests/mysqli_stmt_bind_param_check_param_no_change.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require_once('skipifconnectfailure.inc');
99
--FILE--
1010
<?php
1111
require('table.inc');
12+
$link->set_charset('latin1');
1213

1314
class foo {
1415
// @var $bar string

0 commit comments

Comments
 (0)