xref: /PHP-8.2/ext/mysqli/tests/connect.inc (revision 62d393b1)
1<?php
2    /*
3    Default values are "localhost", "root",
4    database "test" and empty password.
5    Change the MYSQL_TEST environment values
6    if you want to use another configuration
7    */
8
9    $driver    = new mysqli_driver;
10    $driver->report_mode = MYSQLI_REPORT_OFF;
11
12    $host      = getenv("MYSQL_TEST_HOST")     ?: "127.0.0.1";
13    $port      = getenv("MYSQL_TEST_PORT")     ?: 3306;
14    $user      = getenv("MYSQL_TEST_USER")     ?: "root";
15    $passwd    = getenv("MYSQL_TEST_PASSWD")   ?: "";
16    $db        = getenv("MYSQL_TEST_DB")       ?: "test";
17    $engine    = getenv("MYSQL_TEST_ENGINE")   ?: "InnoDB";
18    $socket    = getenv("MYSQL_TEST_SOCKET")   ?: null;
19    $skip_on_connect_failure  = getenv("MYSQL_TEST_SKIP_CONNECT_FAILURE") ?: true;
20    $connect_flags = (int)getenv("MYSQL_TEST_CONNECT_FLAGS") ?: 0;
21    if ($socket) {
22        ini_set('mysqli.default_socket', $socket);
23    }
24
25    /* Development setting: test experimental features and/or feature requests that never worked before? */
26    $TEST_EXPERIMENTAL = 1 == getenv("MYSQL_TEST_EXPERIMENTAL");
27
28    if (!function_exists('sys_get_temp_dir')) {
29        function sys_get_temp_dir() {
30
31            if (!empty($_ENV['TMP']))
32                return realpath( $_ENV['TMP'] );
33            if (!empty($_ENV['TMPDIR']))
34                return realpath( $_ENV['TMPDIR'] );
35            if (!empty($_ENV['TEMP']))
36                return realpath( $_ENV['TEMP'] );
37
38            $temp_file = tempnam(md5(uniqid(rand(), TRUE)), '');
39            if ($temp_file) {
40                $temp_dir = realpath(dirname($temp_file));
41                unlink($temp_file);
42                return $temp_dir;
43            }
44            return FALSE;
45        }
46    }
47
48    if (!function_exists('my_mysqli_connect')) {
49
50        /**
51        * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
52        *
53        * @param bool $enable_env_flags Enable setting of connection flags through 	env(MYSQL_TEST_CONNECT_FLAGS)?
54        */
55        function my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
56            global $connect_flags;
57
58            $flags = $enable_env_flags? $connect_flags:0;
59            if ($flags !== 0) {
60                $link = mysqli_init();
61                if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags))
62                    $link = false;
63            } else {
64                $link = mysqli_connect($host, $user, $passwd, $db, $port, $socket);
65            }
66
67            return $link;
68        }
69
70        /**
71        * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
72        *
73        * @param bool $enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)
74        */
75        function my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags = 0, $enable_env_flags = true) {
76            global $connect_flags;
77
78            if ($enable_env_flags)
79                $flags = $flags | $connect_flags;
80
81            return mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags);
82        }
83
84        class my_mysqli extends mysqli {
85            public function __construct($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
86                global $connect_flags;
87
88                $flags = ($enable_env_flags) ? $connect_flags : 0;
89
90                if ($flags !== 0) {
91                    parent::__construct();
92                    $this->real_connect($host, $user, $passwd, $db, $port, $socket, $flags);
93                } else {
94                    parent::__construct($host, $user, $passwd, $db, $port, $socket);
95                }
96            }
97        }
98
99        function have_innodb($link) {
100            if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'"))
101                && ($row = $res->fetch_row())
102                && !empty($row)
103            ) {
104                return !($row[1] == 'DISABLED' || $row[1] == 'NO');
105            }
106            // MySQL 5.6.1+
107            if ($res = $link->query('SHOW ENGINES')) {
108                while ($row = $res->fetch_assoc()) {
109                    if (!isset($row['Engine']) || !isset($row['Support'])) {
110                        return false;
111                    }
112
113                    if (($row['Engine'] == 'InnoDB')
114                        && (($row['Support'] == 'YES') || ($row['Support'] == 'DEFAULT'))
115                    ) {
116                        return true;
117                    }
118                }
119            }
120            return false;
121        }
122
123    } else {
124        printf("skip Eeeek/BUG/FIXME - connect.inc included twice! skipif bug?\n");
125    }
126
127    function handle_catchable_fatal($errno, $error, $file, $line) {
128        static $errcodes = array();
129        if (empty($errcodes)) {
130            $constants = get_defined_constants();
131            foreach ($constants as $name => $value) {
132                if (substr($name, 0, 2) == "E_")
133                    $errcodes[$value] = $name;
134            }
135        }
136        printf("[%s] %s in %s on line %s\n",
137            (isset($errcodes[$errno])) ? $errcodes[$errno] : $errno,
138             $error, $file, $line);
139
140        return true;
141    }
142?>
143