xref: /PHP-5.4/ext/mysqli/tests/connect.inc (revision cc24c303)
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 my_get_charsets($link) {
133
134			/* Those tree are set by SET NAMES */
135			$charsets = array(
136				'client' 		=> NULL,
137				'results'		=> NULL,
138				'connection'	=> NULL,
139			);
140
141			if (!($res = mysqli_query($link, "SHOW VARIABLES LIKE '%character%'"))) {
142				printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
143				return $charsets;
144			}
145
146			$names = array();
147			while ($row = mysqli_fetch_assoc($res)) {
148				$names[$row['Variable_name']] = $row['Value'];
149			}
150			mysqli_free_result($res);
151
152			if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_client']))) ||
153				!($details = mysqli_fetch_assoc($res))) {
154				printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
155				return $charsets;
156			}
157			mysqli_free_result($res);
158
159			$charsets['client'] = array(
160				'charset' 	=> $details['Charset'],
161				'desc'		=> $details['Description'],
162				'collation'	=> $details['Default collation'],
163				'maxlen'	=> $details['Maxlen'],
164				'nr'		=> NULL,
165			);
166
167			if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
168				!($collation = mysqli_fetch_assoc($res))) {
169				printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
170				return $charsets;
171			}
172			mysqli_free_result($res);
173			$charsets['client']['nr'] = $collation['Id'];
174
175			if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_results']))) ||
176				!($details = mysqli_fetch_assoc($res))) {
177				printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
178				return $charsets;
179			}
180			mysqli_free_result($res);
181
182			$charsets['results'] = array(
183				'charset' 	=> $details['Charset'],
184				'desc'		=> $details['Description'],
185				'collation'	=> $details['Default collation'],
186				'maxlen'	=> $details['Maxlen'],
187				'nr'		=> NULL,
188			);
189
190			if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
191				!($collation = mysqli_fetch_assoc($res))) {
192				printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
193				return $charsets;
194			}
195			mysqli_free_result($res);
196			$charsets['results']['nr'] = $collation['Id'];
197
198
199			if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_connection']))) ||
200				!($details = mysqli_fetch_assoc($res))) {
201				printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
202				return $charsets;
203			}
204			mysqli_free_result($res);
205
206			$charsets['connection'] = array(
207				'charset' 	=> $details['Charset'],
208				'desc'		=> $details['Description'],
209				'collation'	=> $details['Default collation'],
210				'maxlen'	=> $details['Maxlen'],
211				'nr'		=> NULL,
212			);
213
214			if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
215				!($collation = mysqli_fetch_assoc($res))) {
216				printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
217				return $charsets;
218			}
219			mysqli_free_result($res);
220			$charsets['connection']['nr'] = $collation['Id'];
221
222			return $charsets;
223		}
224
225		function have_innodb($link) {
226		  if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'")) &&
227				($row = $res->fetch_row()) &&
228				!empty($row)) {
229				if ($row[1] == "DISABLED" || $row[1] == "NO") {
230					return false;
231				}
232				return true;
233		  } else {
234				/* MySQL 5.6.1+ */
235				if ($res = $link->query("SHOW ENGINES")) {
236					while ($row = $res->fetch_assoc()) {
237						if (!isset($row['Engine']) || !isset($row['Support']))
238							return false;
239
240						if (('InnoDB' == $row['Engine']) &&
241							(('YES' == $row['Support']) || ('DEFAULT' == $row['Support']))
242							) {
243							return true;
244						}
245					}
246					return false;
247				} else {
248					return false;
249				}
250		  }
251		  return false;
252		}
253
254	} else {
255		printf("skip Eeeek/BUG/FIXME - connect.inc included twice! skipif bug?\n");
256	}
257
258	function handle_catchable_fatal($errno, $error, $file, $line) {
259		static $errcodes = array();
260		if (empty($errcodes)) {
261			$constants = get_defined_constants();
262			foreach ($constants as $name => $value) {
263				if (substr($name, 0, 2) == "E_")
264					$errcodes[$value] = $name;
265			}
266		}
267		printf("[%s] %s in %s on line %s\n",
268			(isset($errcodes[$errno])) ? $errcodes[$errno] : $errno,
269			 $error, $file, $line);
270
271		return true;
272	}
273?>
274