1--TEST--
2mysqli_get_charset()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11    require_once 'connect.inc';
12    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
13        printf("Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
14            $host, $user, $db, $port, $socket);
15        exit(1);
16    }
17
18    if (!$res = mysqli_query($link, 'SELECT version() AS server_version'))
19        printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
20    $tmp = mysqli_fetch_assoc($res);
21    mysqli_free_result($res);
22    $version = explode('.', $tmp['server_version']);
23    if (empty($version))
24        printf("[005] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
25
26    if ($version[0] <= 4 && $version[1] < 1)
27        printf("[006] Need MySQL Server 4.1+ for the test!\n");
28
29    if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
30        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
31    $tmp = mysqli_fetch_assoc($res);
32    mysqli_free_result($res);
33    if (!($character_set_connection = $tmp['charset']) || !($collation_connection = $tmp['collation']))
34        printf("[008] Cannot determine current character set and collation\n");
35
36    if (!$res = mysqli_query($link, $sql = sprintf("SHOW CHARACTER SET LIKE '%s'", $character_set_connection)))
37        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
38    $tmp = mysqli_fetch_assoc($res);
39    if (empty($tmp))
40        printf("[010] Cannot fetch Maxlen and/or Comment, test will fail: $sql\n");
41
42    $maxlen = (isset($tmp['Maxlen'])) ? $tmp['Maxlen'] : '';
43    $comment = (isset($tmp['Description'])) ? $tmp['Description'] : '';
44
45    if (!$res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $collation_connection)))
46        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
47    $tmp = mysqli_fetch_assoc($res);
48    mysqli_free_result($res);
49    if (!($id = $tmp['Id']))
50        printf("[012] Cannot fetch Id/Number, test will fail\n");
51
52    if (!$res = mysqli_query($link, sprintf("SHOW VARIABLES LIKE 'character_sets_dir'")))
53        printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
54    $tmp = mysqli_fetch_assoc($res);
55    mysqli_free_result($res);
56    if (!($character_sets_dir = $tmp['Value']))
57        printf("[014] Cannot fetch character_sets_dir, test will fail\n");
58
59    if (!is_object($charset = mysqli_get_charset($link)))
60        printf("[015] Expecting object/std_class, got %s/%s\n", gettype($charset), $charset);
61
62    if (!isset($charset->charset) ||
63        !in_array(gettype($charset->charset), array("string", "unicode")) ||
64        ($character_set_connection !== $charset->charset))
65        printf("[016] Expecting string/%s, got %s/%s\n", $character_set_connection, gettype($charset->charset), $charset->charset);
66    if (!isset($charset->collation) ||
67        !in_array(gettype($charset->collation), array("string", "unicode")) ||
68        ($collation_connection !== $charset->collation))
69        printf("[017] Expecting string/%s, got %s/%s\n", $collation_connection, gettype($charset->collation), $charset->collation);
70
71    if (!isset($charset->dir) ||
72        !is_string($charset->dir))
73        printf("[019] Expecting string - ideally %s*, got %s/%s\n", $character_sets_dir, gettype($charset->dir), $charset->dir);
74
75    if (!isset($charset->min_length) ||
76        !(is_int($charset->min_length)) ||
77        ($charset->min_length < 0) ||
78        ($charset->min_length > $charset->max_length))
79        printf("[020] Expecting int between 0 ... %d, got %s/%s\n", $charset->max_length,
80            gettype($charset->min_length), $charset->min_length);
81
82    if (!isset($charset->number) ||
83        !is_int($charset->number) ||
84        ($charset->number !== (int)$id))
85        printf("[021] Expecting int/%d, got %s/%s\n", $id, gettype($charset->number), $charset->number);
86
87    if (!isset($charset->state) ||
88        !is_int($charset->state))
89        printf("[022] Expecting int/any, got %s/%s\n", gettype($charset->state), $charset->state);
90
91    mysqli_close($link);
92
93    try {
94        mysqli_get_charset($link);
95    } catch (Error $exception) {
96        echo $exception->getMessage() . "\n";
97    }
98
99    print "done!";
100?>
101--EXPECT--
102mysqli object is already closed
103done!
104