xref: /PHP-8.4/ext/mysqli/tests/connect.inc (revision 7fa2dbf9)
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    if ($socket) {
20        ini_set('mysqli.default_socket', $socket);
21    }
22
23    function get_environment_connection_flags(): int {
24        static $connect_flags = null;
25        if ($connect_flags === null) {
26            $connect_flags = (int)getenv("MYSQL_TEST_CONNECT_FLAGS") ?: 0;
27        }
28        return $connect_flags;
29    }
30
31    /**
32    * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
33    *
34    * @param bool $enable_env_flags Enable setting of connection flags through 	env(MYSQL_TEST_CONNECT_FLAGS)?
35    */
36    function my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
37        $flags = $enable_env_flags? get_environment_connection_flags():0;
38        if ($flags !== 0) {
39            $link = mysqli_init();
40            if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags))
41                $link = false;
42        } else {
43            $link = mysqli_connect($host, $user, $passwd, $db, $port, $socket);
44        }
45
46        return $link;
47    }
48
49    /**
50    * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
51    *
52    * @param bool $enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)
53    */
54    function my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags = 0, $enable_env_flags = true) {
55        if ($enable_env_flags) {
56            $flags = $flags | get_environment_connection_flags();
57        }
58
59        return mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags);
60    }
61
62    class my_mysqli extends mysqli {
63        public function __construct($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
64            $flags = ($enable_env_flags) ? get_environment_connection_flags() : 0;
65
66            // Because the tests are meant to test both error modes, they can set the report_mode to a different value,
67            // which we do not want to override. However, we want to make sure that if a connection cannot be made,
68            // the constuctor will throw an exception. We store current report_mode in variable and restore it later.
69            $driver = new mysqli_driver;
70            $report_mode = $driver->report_mode;
71            $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
72
73            if ($flags !== 0) {
74                parent::__construct();
75                $this->real_connect($host, $user, $passwd, $db, $port, $socket, $flags);
76            } else {
77                parent::__construct($host, $user, $passwd, $db, $port, $socket);
78            }
79
80            // Restore error mode
81            $driver->report_mode = $report_mode;
82        }
83    }
84
85    function have_innodb($link) {
86        if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'"))
87            && ($row = $res->fetch_row())
88            && !empty($row)
89        ) {
90            return !($row[1] == 'DISABLED' || $row[1] == 'NO');
91        }
92        // MySQL 5.6.1+
93        if ($res = $link->query('SHOW ENGINES')) {
94            while ($row = $res->fetch_assoc()) {
95                if (!isset($row['Engine']) || !isset($row['Support'])) {
96                    return false;
97                }
98
99                if (($row['Engine'] == 'InnoDB')
100                    && (($row['Support'] == 'YES') || ($row['Support'] == 'DEFAULT'))
101                ) {
102                    return true;
103                }
104            }
105        }
106        return false;
107    }
108