xref: /PHP-5.5/ext/mysqli/tests/connect.inc (revision 294e7c29)
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 : false;
91
92			if ($flags !== false) {
93				$link = mysqli_init();
94				if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags))
95					$link = false;
96			} else {
97				$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket);
98			}
99
100			return $link;
101		}
102
103		/**
104		* Whenever possible, please use this wrapper to make testing ot MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
105		*
106		* @param enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)
107		*/
108		function my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags = 0, $enable_env_flags = true) {
109			global $connect_flags;
110
111			if ($enable_env_flags)
112				$flags & $connect_flags;
113
114			return mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags);
115		}
116
117		class my_mysqli extends mysqli {
118			public function __construct($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
119				global $connect_flags;
120
121				$flags = ($enable_env_flags) ? $connect_flags : false;
122
123				if ($flags !== false) {
124					parent::init();
125					$this->real_connect($host, $user, $passwd, $db, $port, $socket, $flags);
126				} else {
127					parent::__construct($host, $user, $passwd, $db, $port, $socket);
128				}
129			}
130		}
131
132		function have_innodb($link) {
133		  if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'")) &&
134				($row = $res->fetch_row()) &&
135				!empty($row)) {
136				if ($row[1] == "DISABLED" || $row[1] == "NO") {
137					return false;
138				}
139				return true;
140		  } else {
141				/* MySQL 5.6.1+ */
142				if ($res = $link->query("SHOW ENGINES")) {
143					while ($row = $res->fetch_assoc()) {
144						if (!isset($row['Engine']) || !isset($row['Support']))
145							return false;
146
147						if (('InnoDB' == $row['Engine']) &&
148							(('YES' == $row['Support']) || ('DEFAULT' == $row['Support']))
149							) {
150							return true;
151						}
152					}
153					return false;
154				} else {
155					return false;
156				}
157		  }
158		  return false;
159		}
160
161	} else {
162		printf("skip Eeeek/BUG/FIXME - connect.inc included twice! skipif bug?\n");
163	}
164
165	function handle_catchable_fatal($errno, $error, $file, $line) {
166		static $errcodes = array();
167		if (empty($errcodes)) {
168			$constants = get_defined_constants();
169			foreach ($constants as $name => $value) {
170				if (substr($name, 0, 2) == "E_")
171					$errcodes[$value] = $name;
172			}
173		}
174		printf("[%s] %s in %s on line %s\n",
175			(isset($errcodes[$errno])) ? $errcodes[$errno] : $errno,
176			 $error, $file, $line);
177
178		return true;
179	}
180?>
181