xref: /PHP-7.2/ext/mysqli/tests/connect.inc (revision 6d51b7b2)
1<?php
2	/*
3	Default values are "localhost", "root",
4	database "stest" 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")     ? getenv("MYSQL_TEST_HOST") : "localhost";
12	$port      = getenv("MYSQL_TEST_PORT")     ? getenv("MYSQL_TEST_PORT") : 3306;
13	$user      = getenv("MYSQL_TEST_USER")     ? getenv("MYSQL_TEST_USER") : "root";
14	$passwd    = getenv("MYSQL_TEST_PASSWD")   ? getenv("MYSQL_TEST_PASSWD") : "";
15	$db        = getenv("MYSQL_TEST_DB")       ? getenv("MYSQL_TEST_DB") : "test";
16	$engine    = getenv("MYSQL_TEST_ENGINE")   ? getenv("MYSQL_TEST_ENGINE") : "MyISAM";
17	$socket    = getenv("MYSQL_TEST_SOCKET")   ? getenv("MYSQL_TEST_SOCKET") : null;
18	$skip_on_connect_failure  = getenv("MYSQL_TEST_SKIP_CONNECT_FAILURE") ? getenv("MYSQL_TEST_SKIP_CONNECT_FAILURE") : true;
19	$connect_flags = getenv("MYSQL_TEST_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	if (!$IS_MYSQLND) {
31		$MYSQLND_VERSION = NULL;
32	} else {
33		/*
34		The formatting of the version reported by mysqli_get_client_info()
35		has changed significantly in the past. To get tests working properly
36		with PHP 5.3.0 and up, we set everything that looks like prior to
37		PHP 5.3.0 to version 5.0.4 = 5 * 10000 + 0 * 100 + 4 = 50004.
38		PHP 5.3.0	reports mysqlnd 5.0.5 dev (= 5 * 10000 + 0 * 100 + 5 = 50005.
39		*/
40		if (preg_match('@Revision:\s+(\d+)\s*\$@ism', mysqli_get_client_info(), $matches)) {
41			/* something prior to PHP 5.3.0 */
42			$MYSQLND_VERSION = 50004;
43		} else if (preg_match('@^mysqlnd (\d+)\.(\d+)\.(\d+).*@ism', mysqli_get_client_info(), $matches)) {
44			/* formatting schema used by PHP 5.3.0 */
45			$MYSQLND_VERSION = (int)$matches[1] * 10000 + (int)$matches[2] * 100 + (int)$matches[3];
46		} else if (preg_match('@^mysqlnd/PHP 6.0.0-dev@ism', mysqli_get_client_info(), $matches)) {
47			/*
48				PHP 6.0 at the time of the first PHP 5.3.0 release.
49				HEAD and 5.3 have been in sync when 5.3.0 was released.
50				It is at least 5.0.5-dev.
51			*/
52			$MYSQLND_VERSION = 50005;
53		} else {
54			/* unknown */
55			$MYSQLND_VERSION = -1;
56		}
57
58	}
59
60	if (!function_exists('sys_get_temp_dir')) {
61		function sys_get_temp_dir() {
62
63			if (!empty($_ENV['TMP']))
64				return realpath( $_ENV['TMP'] );
65			if (!empty($_ENV['TMPDIR']))
66				return realpath( $_ENV['TMPDIR'] );
67			if (!empty($_ENV['TEMP']))
68				return realpath( $_ENV['TEMP'] );
69
70			$temp_file = tempnam(md5(uniqid(rand(), TRUE)), '');
71			if ($temp_file) {
72				$temp_dir = realpath(dirname($temp_file));
73				unlink($temp_file);
74				return $temp_dir;
75			}
76			return FALSE;
77		}
78	}
79
80	if (!function_exists('my_mysqli_connect')) {
81
82		/**
83		* Whenever possible, please use this wrapper to make testing ot MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
84		*
85		* @param enable_env_flags Enable setting of connection flags through 	env(MYSQL_TEST_CONNECT_FLAGS)?
86		*/
87		function my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
88			global $connect_flags;
89
90			$flags = $enable_env_flags? $connect_flags:0;
91			if ($flags !== 0) {
92				$link = mysqli_init();
93				if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags))
94					$link = false;
95			} else {
96				$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket);
97			}
98
99			return $link;
100		}
101
102		/**
103		* Whenever possible, please use this wrapper to make testing ot MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
104		*
105		* @param enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)
106		*/
107		function my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags = 0, $enable_env_flags = true) {
108			global $connect_flags;
109
110			if ($enable_env_flags)
111				$flags = $flags | $connect_flags;
112
113			return mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags);
114		}
115
116		class my_mysqli extends mysqli {
117			public function __construct($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
118				global $connect_flags;
119
120				$flags = ($enable_env_flags) ? $connect_flags : 0;
121
122				if ($flags !== false) {
123					parent::init();
124					$this->real_connect($host, $user, $passwd, $db, $port, $socket, $flags);
125				} else {
126					parent::__construct($host, $user, $passwd, $db, $port, $socket);
127				}
128			}
129		}
130
131		function have_innodb($link) {
132		  if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'")) &&
133				($row = $res->fetch_row()) &&
134				!empty($row)) {
135				if ($row[1] == "DISABLED" || $row[1] == "NO") {
136					return false;
137				}
138				return true;
139		  } else {
140				/* MySQL 5.6.1+ */
141				if ($res = $link->query("SHOW ENGINES")) {
142					while ($row = $res->fetch_assoc()) {
143						if (!isset($row['Engine']) || !isset($row['Support']))
144							return false;
145
146						if (('InnoDB' == $row['Engine']) &&
147							(('YES' == $row['Support']) || ('DEFAULT' == $row['Support']))
148							) {
149							return true;
150						}
151					}
152					return false;
153				} else {
154					return false;
155				}
156		  }
157		  return false;
158		}
159
160	} else {
161		printf("skip Eeeek/BUG/FIXME - connect.inc included twice! skipif bug?\n");
162	}
163
164	function handle_catchable_fatal($errno, $error, $file, $line) {
165		static $errcodes = array();
166		if (empty($errcodes)) {
167			$constants = get_defined_constants();
168			foreach ($constants as $name => $value) {
169				if (substr($name, 0, 2) == "E_")
170					$errcodes[$value] = $name;
171			}
172		}
173		printf("[%s] %s in %s on line %s\n",
174			(isset($errcodes[$errno])) ? $errcodes[$errno] : $errno,
175			 $error, $file, $line);
176
177		return true;
178	}
179?>
180