xref: /PHP-8.1/ext/mysqli/tests/connect.inc (revision 625b89af)
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 = (in_array(getenv("MYSQL_TEST_EXPERIMENTAL"), array(0, 1))) ?
27                ((1 == getenv("MYSQL_TEST_EXPERIMENTAL")) ? true : false) :
28                false;
29
30    $IS_MYSQLND = stristr(mysqli_get_client_info(), "mysqlnd");
31
32    if (!function_exists('sys_get_temp_dir')) {
33        function sys_get_temp_dir() {
34
35            if (!empty($_ENV['TMP']))
36                return realpath( $_ENV['TMP'] );
37            if (!empty($_ENV['TMPDIR']))
38                return realpath( $_ENV['TMPDIR'] );
39            if (!empty($_ENV['TEMP']))
40                return realpath( $_ENV['TEMP'] );
41
42            $temp_file = tempnam(md5(uniqid(rand(), TRUE)), '');
43            if ($temp_file) {
44                $temp_dir = realpath(dirname($temp_file));
45                unlink($temp_file);
46                return $temp_dir;
47            }
48            return FALSE;
49        }
50    }
51
52    if (!function_exists('my_mysqli_connect')) {
53
54        /**
55        * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
56        *
57        * @param enable_env_flags Enable setting of connection flags through 	env(MYSQL_TEST_CONNECT_FLAGS)?
58        */
59        function my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
60            global $connect_flags;
61
62            $flags = $enable_env_flags? $connect_flags:0;
63            if ($flags !== 0) {
64                $link = mysqli_init();
65                if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags))
66                    $link = false;
67            } else {
68                $link = mysqli_connect($host, $user, $passwd, $db, $port, $socket);
69            }
70
71            return $link;
72        }
73
74        /**
75        * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
76        *
77        * @param enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)
78        */
79        function my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags = 0, $enable_env_flags = true) {
80            global $connect_flags;
81
82            if ($enable_env_flags)
83                $flags = $flags | $connect_flags;
84
85            return mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags);
86        }
87
88        class my_mysqli extends mysqli {
89            public function __construct($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
90                global $connect_flags;
91
92                $flags = ($enable_env_flags) ? $connect_flags : 0;
93
94                if ($flags !== false) {
95                    parent::__construct();
96                    $this->real_connect($host, $user, $passwd, $db, $port, $socket, $flags);
97                } else {
98                    parent::__construct($host, $user, $passwd, $db, $port, $socket);
99                }
100            }
101        }
102
103        function have_innodb($link) {
104          if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'")) &&
105                ($row = $res->fetch_row()) &&
106                !empty($row)) {
107                if ($row[1] == "DISABLED" || $row[1] == "NO") {
108                    return false;
109                }
110                return true;
111          } else {
112                /* MySQL 5.6.1+ */
113                if ($res = $link->query("SHOW ENGINES")) {
114                    while ($row = $res->fetch_assoc()) {
115                        if (!isset($row['Engine']) || !isset($row['Support']))
116                            return false;
117
118                        if (('InnoDB' == $row['Engine']) &&
119                            (('YES' == $row['Support']) || ('DEFAULT' == $row['Support']))
120                            ) {
121                            return true;
122                        }
123                    }
124                    return false;
125                } else {
126                    return false;
127                }
128          }
129          return false;
130        }
131
132    } else {
133        printf("skip Eeeek/BUG/FIXME - connect.inc included twice! skipif bug?\n");
134    }
135
136    function handle_catchable_fatal($errno, $error, $file, $line) {
137        static $errcodes = array();
138        if (empty($errcodes)) {
139            $constants = get_defined_constants();
140            foreach ($constants as $name => $value) {
141                if (substr($name, 0, 2) == "E_")
142                    $errcodes[$value] = $name;
143            }
144        }
145        printf("[%s] %s in %s on line %s\n",
146            (isset($errcodes[$errno])) ? $errcodes[$errno] : $errno,
147             $error, $file, $line);
148
149        return true;
150    }
151?>
152