1--TEST--
2mysqli_get_charset()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8if (!function_exists('mysqli_get_charset'))
9	die("skip: function not available");
10?>
11--FILE--
12<?php
13	require_once("connect.inc");
14
15	$tmp    = NULL;
16	$link   = NULL;
17
18	if (!is_null($tmp = @mysqli_get_charset()))
19		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
20
21	if (!is_null($tmp = @mysqli_get_charset($link)))
22		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
23
24	if (!is_null($tmp = @mysqli_set_charset($link, $link)))
25		printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
26
27	require('table.inc');
28
29	if (!$res = mysqli_query($link, 'SELECT version() AS server_version'))
30		printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
31	$tmp = mysqli_fetch_assoc($res);
32	mysqli_free_result($res);
33	$version = explode('.', $tmp['server_version']);
34	if (empty($version))
35		printf("[005] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
36
37	if ($version[0] <= 4 && $version[1] < 1)
38		printf("[006] Need MySQL Server 4.1+ for the test!\n");
39
40	if (!$res = mysqli_query($link, 'SELECT @@character_set_connection AS charset, @@collation_connection AS collation'))
41		printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
42	$tmp = mysqli_fetch_assoc($res);
43	mysqli_free_result($res);
44	if (!($character_set_connection = $tmp['charset']) || !($collation_connection = $tmp['collation']))
45		printf("[008] Cannot determine current character set and collation\n");
46
47	if (!$res = mysqli_query($link, $sql = sprintf("SHOW CHARACTER SET LIKE '%s'", $character_set_connection)))
48		printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
49	$tmp = mysqli_fetch_assoc($res);
50	if (empty($tmp))
51		printf("[010] Cannot fetch Maxlen and/or Comment, test will fail: $sql\n");
52
53	$maxlen = (isset($tmp['Maxlen'])) ? $tmp['Maxlen'] : '';
54	$comment = (isset($tmp['Description'])) ? $tmp['Description'] : '';
55
56	if (!$res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $collation_connection)))
57		printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
58	$tmp = mysqli_fetch_assoc($res);
59	mysqli_free_result($res);
60	if (!($id = $tmp['Id']))
61		printf("[012] Cannot fetch Id/Number, test will fail\n");
62
63	if (!$res = mysqli_query($link, sprintf("SHOW VARIABLES LIKE 'character_sets_dir'")))
64		printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
65	$tmp = mysqli_fetch_assoc($res);
66	mysqli_free_result($res);
67	if (!($character_sets_dir = $tmp['Value']))
68		printf("[014] Cannot fetch character_sets_dir, test will fail\n");
69
70	if (!is_object($charset = mysqli_get_charset($link)))
71		printf("[015] Expecting object/std_class, got %s/%s\n", gettype($charset), $charset);
72
73	if (!isset($charset->charset) ||
74		!in_array(gettype($charset->charset), array("string", "unicode")) ||
75		($character_set_connection !== $charset->charset))
76		printf("[016] Expecting string/%s, got %s/%s\n", $character_set_connection, gettype($charset->charset), $charset->charset);
77	if (!isset($charset->collation) ||
78		!in_array(gettype($charset->collation), array("string", "unicode")) ||
79		($collation_connection !== $charset->collation))
80		printf("[017] Expecting string/%s, got %s/%s\n", $collation_connection, gettype($charset->collation), $charset->collation);
81
82	if (!isset($charset->dir) ||
83		!is_string($charset->dir))
84		printf("[019] Expecting string - ideally %s*, got %s/%s\n", $character_sets_dir, gettype($charset->dir), $charset->dir);
85
86	if (!isset($charset->min_length) ||
87		!(is_int($charset->min_length)) ||
88		($charset->min_length < 0) ||
89		($charset->min_length > $charset->max_length))
90		printf("[020] Expecting int between 0 ... %d, got %s/%s\n", $charset->max_length,
91			gettype($charset->min_length), $charset->min_length);
92
93	if (!isset($charset->number) ||
94		!is_int($charset->number) ||
95		($charset->number !== (int)$id))
96		printf("[021] Expecting int/%d, got %s/%s\n", $id, gettype($charset->number), $charset->number);
97
98	if (!isset($charset->state) ||
99		!is_int($charset->state))
100		printf("[022] Expecting int/any, got %s/%s\n", gettype($charset->state), $charset->state);
101
102	mysqli_close($link);
103
104	if (NULL !== ($tmp = mysqli_get_charset($link)))
105		printf("[023] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
106
107	print "done!";
108?>
109--CLEAN--
110<?php
111	require_once("clean_table.inc");
112?>
113--EXPECTF--
114Warning: mysqli_get_charset(): Couldn't fetch mysqli in %s on line %d
115done!
116