xref: /PHP-8.3/ext/mysqli/tests/mysqli_driver.phpt (revision a21edc52)
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
13$driver = new mysqli_driver();
14
15$client_info = mysqli_get_client_info();
16if (($tmp = $driver->client_info) !== $client_info) {
17    printf("[002] Expecting %s/%s, got %s/%s\n",
18        gettype($client_info), $client_info,
19        gettype($tmp), $tmp);
20}
21
22$client_version = mysqli_get_client_version();
23if (($tmp = $driver->client_version) !== $client_version) {
24    printf("[003] Expecting %s/%s, got %s/%s\n",
25    gettype($client_version), $client_version,
26    gettype($tmp), $tmp);
27}
28
29$all_modes = [
30    MYSQLI_REPORT_ALL,
31    MYSQLI_REPORT_STRICT,
32    MYSQLI_REPORT_STRICT|MYSQLI_REPORT_ERROR,
33    MYSQLI_REPORT_STRICT|MYSQLI_REPORT_INDEX,
34    MYSQLI_REPORT_ERROR,
35    MYSQLI_REPORT_ERROR|MYSQLI_REPORT_INDEX,
36    MYSQLI_REPORT_INDEX,
37    MYSQLI_REPORT_OFF
38];
39$report_mode = $driver->report_mode;
40if (!is_int($report_mode)) {
41    printf("[005] Expecting int/any, got %s/%s\n",
42    gettype($report_mode), $report_mode);
43}
44
45if (!in_array($report_mode, $all_modes)) {
46    printf("[006] Illegal report mode returned? Got %s, expected %s\n",
47        $report_mode, implode(', ', $all_modes));
48}
49
50$driver->report_mode = MYSQLI_REPORT_STRICT;
51$ok = false;
52try {
53    if ($link = my_mysqli_connect($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket)) {
54        printf("[007] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
55        $host, $user . 'unknown_really', $db, $port, $socket);
56    }
57    mysqli_close($link);
58} catch (mysqli_sql_exception $e) {
59    $ok = true;
60    if ('' == $e->getMessage()) {
61        printf("[008] getMessage() has returned an empty string.\n");
62    }
63    if ('' == $e->getCode()) {
64        printf("[009] getCode() has returned an empty string.\n");
65    }
66    if ('' == $e->getFile()) {
67        printf("[010] getFile() has returned an empty string.\n");
68    }
69    if ('' == $e->getLine()) {
70        printf("[011] getLine() has returned an empty string.\n");
71    }
72    if ([] == $e->getTrace()) {
73        printf("[012] getTrace() has returned an empty array.\n");
74    }
75    if ('' == $e->getTraceAsString()) {
76        printf("[013] getTraceAsString() has returned an empty string.\n");
77    }
78    if ('' == $e->__toString()) {
79        printf("[014] __toString() has returned an empty string.\n");
80    }
81}
82if (!$ok) {
83    printf("[015] Error reporting mode has not been switched to exceptions and or no exception thrown\n");
84}
85
86$driver->report_mode = MYSQLI_REPORT_OFF;
87if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
88    printf("[016] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
89}
90mysqli_query($link, "NO_SQL");
91mysqli_close($link);
92
93$driver->report_mode = MYSQLI_REPORT_ERROR;
94
95if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
96    printf("[017] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
97}
98mysqli_query($link, "NO_SQL");
99mysqli_close($link);
100
101if (MYSQLI_REPORT_ERROR !== $driver->report_mode) {
102    printf("[018] Error reporting mode should be different\n");
103}
104
105/* TODO - more report testing should go in here, but it's not really documented what behaviour is expected */
106
107$driver->report_mode = $report_mode;
108
109print "done!";
110?>
111--EXPECTF--
112Warning: 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
113done!
114