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