xref: /PHP-8.1/ext/mysqli/tests/mysqli_driver.phpt (revision b5a14e6c)
1--TEST--
2mysqli_driver class
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11require_once('connect.inc');
12
13if (!is_object($driver = new mysqli_driver())) {
14    printf("[001] Failed to create mysqli_driver object\n");
15}
16
17$client_info = mysqli_get_client_info();
18if (($tmp = $driver->client_info) !== $client_info) {
19    printf("[002] Expecting %s/%s, got %s/%s\n",
20        gettype($client_info), $client_info,
21        gettype($tmp), $tmp);
22}
23
24$client_version = mysqli_get_client_version();
25if (($tmp = $driver->client_version) !== $client_version) {
26    printf("[003] Expecting %s/%s, got %s/%s\n",
27    gettype($client_version), $client_version,
28    gettype($tmp), $tmp);
29}
30
31$all_modes = [
32    MYSQLI_REPORT_ALL,
33    MYSQLI_REPORT_STRICT,
34    MYSQLI_REPORT_STRICT|MYSQLI_REPORT_ERROR,
35    MYSQLI_REPORT_STRICT|MYSQLI_REPORT_INDEX,
36    MYSQLI_REPORT_ERROR,
37    MYSQLI_REPORT_ERROR|MYSQLI_REPORT_INDEX,
38    MYSQLI_REPORT_INDEX,
39    MYSQLI_REPORT_OFF
40];
41$report_mode = $driver->report_mode;
42if (!is_int($report_mode)) {
43    printf("[005] Expecting int/any, got %s/%s\n",
44    gettype($report_mode), $report_mode);
45}
46
47if (!in_array($report_mode, $all_modes)) {
48    printf("[006] Illegal report mode returned? Got %s, expected %s\n",
49        $report_mode, implode(', ', $all_modes));
50}
51
52$driver->report_mode = MYSQLI_REPORT_STRICT;
53$ok = false;
54try {
55    if ($link = my_mysqli_connect($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket)) {
56        printf("[007] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
57        $host, $user . 'unknown_really', $db, $port, $socket);
58    }
59    mysqli_close($link);
60} catch (mysqli_sql_exception $e) {
61    $ok = true;
62    if ('' == $e->getMessage()) {
63        printf("[008] getMessage() has returned an empty string.\n");
64    }
65    if ('' == $e->getCode()) {
66        printf("[009] getCode() has returned an empty string.\n");
67    }
68    if ('' == $e->getFile()) {
69        printf("[010] getFile() has returned an empty string.\n");
70    }
71    if ('' == $e->getLine()) {
72        printf("[011] getLine() has returned an empty string.\n");
73    }
74    if ([] == $e->getTrace()) {
75        printf("[012] getTrace() has returned an empty array.\n");
76    }
77    if ('' == $e->getTraceAsString()) {
78        printf("[013] getTraceAsString() has returned an empty string.\n");
79    }
80    if ('' == $e->__toString()) {
81        printf("[014] __toString() has returned an empty string.\n");
82    }
83}
84if (!$ok) {
85    printf("[015] Error reporting mode has not been switched to exceptions and or no exception thrown\n");
86}
87
88$driver->report_mode = MYSQLI_REPORT_OFF;
89if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
90    printf("[016] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
91}
92mysqli_query($link, "NO_SQL");
93mysqli_close($link);
94
95$driver->report_mode = MYSQLI_REPORT_ERROR;
96
97if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
98    printf("[017] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
99}
100mysqli_query($link, "NO_SQL");
101mysqli_close($link);
102
103if (MYSQLI_REPORT_ERROR !== $driver->report_mode) {
104    printf("[018] Error reporting mode should be different\n");
105}
106
107/* TODO - more report testing should go in here, but it's not really documented what behaviour is expected */
108
109$driver->report_mode = $report_mode;
110
111$reconnect = $driver->reconnect;
112if (!is_bool($reconnect)) {
113    printf("[019] Expecting boolean/any, got %s/%s\n",
114    gettype($reconnect), $reconnect);
115}
116
117/* pointless, but I need more documentation */
118$driver->reconnect = true;
119$driver->reconnect = false;
120$driver->reconnect = $reconnect;
121
122print "done!";
123?>
124--EXPECTF--
125Warning: mysqli_query(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'NO_SQL' at line 1 in %s on line %d
126done!
127