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