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 @@character_set_connection AS charset, @@collation_connection AS collation'))
19        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
20    $tmp = mysqli_fetch_assoc($res);
21    mysqli_free_result($res);
22    if (!($character_set_connection = $tmp['charset']) || !($collation_connection = $tmp['collation']))
23        printf("[008] Cannot determine current character set and collation\n");
24
25    if (!$res = mysqli_query($link, $sql = sprintf("SHOW CHARACTER SET LIKE '%s'", $character_set_connection)))
26        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
27    $tmp = mysqli_fetch_assoc($res);
28    if (empty($tmp))
29        printf("[010] Cannot fetch Maxlen and/or Comment, test will fail: $sql\n");
30
31    $maxlen = (isset($tmp['Maxlen'])) ? $tmp['Maxlen'] : '';
32    $comment = (isset($tmp['Description'])) ? $tmp['Description'] : '';
33
34    if (!$res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $collation_connection)))
35        printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
36    $tmp = mysqli_fetch_assoc($res);
37    mysqli_free_result($res);
38    if (!($id = $tmp['Id']))
39        printf("[012] Cannot fetch Id/Number, test will fail\n");
40
41    if (!$res = mysqli_query($link, sprintf("SHOW VARIABLES LIKE 'character_sets_dir'")))
42        printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
43    $tmp = mysqli_fetch_assoc($res);
44    mysqli_free_result($res);
45    if (!($character_sets_dir = $tmp['Value']))
46        printf("[014] Cannot fetch character_sets_dir, test will fail\n");
47
48    if (!is_object($charset = mysqli_get_charset($link)))
49        printf("[015] Expecting object/std_class, got %s/%s\n", gettype($charset), $charset);
50
51    if (!isset($charset->charset) ||
52        !in_array(gettype($charset->charset), array("string", "unicode")) ||
53        ($character_set_connection !== $charset->charset))
54        printf("[016] Expecting string/%s, got %s/%s\n", $character_set_connection, gettype($charset->charset), $charset->charset);
55    if (!isset($charset->collation) ||
56        !in_array(gettype($charset->collation), array("string", "unicode")) ||
57        ($collation_connection !== $charset->collation))
58        printf("[017] Expecting string/%s, got %s/%s\n", $collation_connection, gettype($charset->collation), $charset->collation);
59
60    if (!isset($charset->dir) ||
61        !is_string($charset->dir))
62        printf("[019] Expecting string - ideally %s*, got %s/%s\n", $character_sets_dir, gettype($charset->dir), $charset->dir);
63
64    if (!isset($charset->min_length) ||
65        !(is_int($charset->min_length)) ||
66        ($charset->min_length < 0) ||
67        ($charset->min_length > $charset->max_length))
68        printf("[020] Expecting int between 0 ... %d, got %s/%s\n", $charset->max_length,
69            gettype($charset->min_length), $charset->min_length);
70
71    if (!isset($charset->number) ||
72        !is_int($charset->number) ||
73        ($charset->number !== (int)$id))
74        printf("[021] Expecting int/%d, got %s/%s\n", $id, gettype($charset->number), $charset->number);
75
76    if (!isset($charset->state) ||
77        !is_int($charset->state))
78        printf("[022] Expecting int/any, got %s/%s\n", gettype($charset->state), $charset->state);
79
80    mysqli_close($link);
81
82    try {
83        mysqli_get_charset($link);
84    } catch (Error $exception) {
85        echo $exception->getMessage() . "\n";
86    }
87
88    print "done!";
89?>
90--EXPECT--
91mysqli object is already closed
92done!
93