diff options
author | Ulf Wendel <uw@php.net> | 2007-10-10 09:51:45 +0000 |
---|---|---|
committer | Ulf Wendel <uw@php.net> | 2007-10-10 09:51:45 +0000 |
commit | dd9292e225dd92b1caef1bc97d0f740495d37aaf (patch) | |
tree | bf7f9d4a5936950ba60300cc6b8e5d005fd90971 /ext/mysql | |
parent | 15a201d6a7b943b44795c295feda212dec5d9af9 (diff) | |
download | php-git-dd9292e225dd92b1caef1bc97d0f740495d37aaf.tar.gz |
Adding new tests.
Diffstat (limited to 'ext/mysql')
22 files changed, 2547 insertions, 0 deletions
diff --git a/ext/mysql/tests/mysql_affected_rows.phpt b/ext/mysql/tests/mysql_affected_rows.phpt new file mode 100644 index 0000000000..64b2d161d2 --- /dev/null +++ b/ext/mysql/tests/mysql_affected_rows.phpt @@ -0,0 +1,117 @@ +--TEST-- +mysql_affected_rows() +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php +include_once("connect.inc"); + +$tmp = NULL; +$link = NULL; + +if (false !== ($tmp = @mysql_affected_rows())) + printf("[001] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = @mysql_affected_rows($link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if (!is_null($tmp = @mysql_affected_rows($link, $link))) + printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket)) + printf("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", + $host, $user, $db, $port, $socket); + +if (-1 !== ($tmp = mysql_affected_rows($link))) + printf("[005] Expecting int/-1, got %s/%s. [%d] %s\n", + gettype($tmp), $tmp, mysql_errno($link), mysql_error($link)); + +if (!mysql_query('DROP TABLE IF EXISTS test', $link)) + printf("[006] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +if (!mysql_query('CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine, $link)) + printf("[007] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +if (!mysql_query('INSERT INTO test(id, label) VALUES (1, "a")', $link)) + printf("[008] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +if (1 !== ($tmp = mysql_affected_rows($link))) + printf("[010] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp); + +// ignore INSERT error, NOTE: command line returns 0, affected_rows returns -1 as documented +@mysql_query('INSERT INTO test(id, label) VALUES (1, "a")', $link); +if (-1 !== ($tmp = mysql_affected_rows($link))) + printf("[011] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp); + +if (!mysql_query('INSERT INTO test(id, label) VALUES (1, "a") ON DUPLICATE KEY UPDATE id = 4', $link)) + printf("[012] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +if (2 !== ($tmp = mysql_affected_rows($link))) + printf("[013] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp); + +if (!mysql_query("INSERT INTO test(id, label) VALUES (2, 'b'), (3, 'c')", $link)) + printf("[014] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +if (2 !== ($tmp = mysql_affected_rows($link))) + printf("[015] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp); + +if (!mysql_query("INSERT IGNORE INTO test(id, label) VALUES (1, 'a')", $link)) { + printf("[016] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +if (1 !== ($tmp = mysql_affected_rows($link))) + printf("[017] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp); + +if (!mysql_query("INSERT INTO test(id, label) SELECT id + 10, label FROM test", $link)) + printf("[018] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +if (4 !== ($tmp = mysql_affected_rows($link))) + printf("[019] Expecting int/4, got %s/%s\n", gettype($tmp), $tmp); + +if (!mysql_query("REPLACE INTO test(id, label) values (4, 'd')", $link)) + printf("[020] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +if (2 !== ($tmp = mysql_affected_rows($link))) + printf("[021] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp); + +if (!mysql_query("REPLACE INTO test(id, label) values (5, 'e')", $link)) + printf("[022] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +if (1 !== ($tmp = mysql_affected_rows($link))) + printf("[023] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp); + +if (!mysql_query("UPDATE test SET label = 'a' WHERE id = 2", $link)) + printf("[024] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +if (1 !== ($tmp = mysql_affected_rows($link))) + printf("[025] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp); + +if (!mysql_query("UPDATE test SET label = 'a' WHERE id = 2", $link)) { + printf("[025] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +if (0 !== ($tmp = mysql_affected_rows($link))) + printf("[026] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp); + +if (!mysql_query("UPDATE test SET label = 'a' WHERE id = 100", $link)) { + printf("[025] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +if (0 !== ($tmp = mysql_affected_rows($link))) + printf("[026] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp); + +if (0 !== ($tmp = mysql_affected_rows())) + printf("[027] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp); + +if (!mysql_query('DROP TABLE IF EXISTS test', $link)) + printf("[028] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +mysql_close($link); + +if (false !== ($tmp = @mysql_affected_rows($link))) + printf("[029] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +print "done!"; +?> +--EXPECTF-- +done! diff --git a/ext/mysql/tests/mysql_client_encoding.phpt b/ext/mysql/tests/mysql_client_encoding.phpt new file mode 100644 index 0000000000..fe94490d06 --- /dev/null +++ b/ext/mysql/tests/mysql_client_encoding.phpt @@ -0,0 +1,67 @@ +--TEST-- +mysql_client_encoding() +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php +include_once "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket)) + printf("[002] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", + $host, $user, $db, $port, $socket); + +$default_link_enc = mysql_client_encoding(); +$link_enc = mysql_client_encoding($link); + +if ($default_link_enc !== $link_enc) + printf("[003] %s != %s, [%d] %s\n", $default_link_enc, $link_enc, mysql_errno($link), mysql_error($link)); + +if (!$res = mysql_query('SELECT version() AS server_version', $link)) + printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link)); +$tmp = mysql_fetch_assoc($res); +mysql_free_result($res); +$version = explode('.', $tmp['server_version']); +if (empty($version)) + printf("[005] Cannot determine server version, need MySQL Server 4.1+ for the test!\n"); + +if ($version[0] <= 4 && $version[1] < 1) + printf("[006] Need MySQL Server 4.1+ for the test!\n"); + +if (!$res = mysql_query('SELECT @@character_set_connection AS charset, @@collation_connection AS collation', $link)) + printf("[007] [%d] %s\n", mysql_errno($link), mysql_error($link)); +$tmp = mysql_fetch_assoc($res); +mysql_free_result($res); +if (!$tmp['charset']) + printf("[008] Cannot determine current character set and collation\n"); + +if ($link_enc !== $tmp['charset']) { + if ($link_enc === $tmp['collation']) { + printf("[009] Known bug, collation instead of chatset returned, http://bugs.mysql.com/bug.php?id=7923\n"); + } else { + printf("[009] Check manually, watch out for unicode and others\n"); + var_dump($link_enc); + var_dump($tmp); + } +} + +if (ini_get('unicode.semantics') && function_exists('is_unicode')) { +// unicode mode + if (!is_unicode($default_link_enc) || !is_unicode($link_enc)) { + printf("[010] No unicode returned!\n"); + var_dump($default_link_enc); + var_dump($link_enc); + } +} + +mysql_close($link); + +if (false !== ($tmp = @mysql_client_encoding($link))) + printf("[012] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +print "done!"; +?> +--EXPECTF-- +done! diff --git a/ext/mysql/tests/mysql_close.phpt b/ext/mysql/tests/mysql_close.phpt new file mode 100644 index 0000000000..19b13b74a4 --- /dev/null +++ b/ext/mysql/tests/mysql_close.phpt @@ -0,0 +1,36 @@ +--TEST-- +mysql_close() +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (false !== ($tmp = @mysql_close())) + printf("[001] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if (NULL !== ($tmp = @mysql_close($link, $link))) + printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket)) + printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", + $host, $user, $db, $port, $socket); + +$tmp = @mysql_close(NULL); +if (false !== $tmp) + printf("[004] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +$tmp = mysql_close($link); +if (true !== $tmp) + printf("[005] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = @mysql_query("SELECT 1", $link))) + printf("[006] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +print "done!\n"; +?> +--EXPECTF-- +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_connect.phpt b/ext/mysql/tests/mysql_connect.phpt new file mode 100644 index 0000000000..ef4010ffc0 --- /dev/null +++ b/ext/mysql/tests/mysql_connect.phpt @@ -0,0 +1,98 @@ +--TEST-- +mysql_connect() +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php +include_once "connect.inc"; + +$tmp = NULL; +$link = NULL; + +// mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] ) +if (NULL !== ($tmp = @mysql_connect($link, $link, $link, $link, $link, $link))) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +$myhost = (is_null($socket)) ? ((is_null($port)) ? $host : $host . ':' . $port) : $host . ':' . $socket; +if (!$link = mysql_connect($myhost, $user, $passwd, true)) + printf("[002] Cannot connect to the server using host=%s/%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", + $host, $myhost, $user, $db, $port, $socket); + +mysql_close($link); + +if ($link = mysql_connect($myhost, $user . 'unknown_really', $passwd . 'non_empty', true)) + printf("[003] Can connect to the server using host=%s/%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n", + $host, $myhost, $user . 'unknown_really', $db, $port, $socket); + +if (false !== $link) +printf("[004] Expecting boolean/false, got %s/%s\n", gettype($link), $link); + +// Run the following tests without an anoynmous MySQL user and use a password for the test user! +ini_set('mysql.default_socket', $socket); +if (!is_resource($link = mysql_connect($host, $user, $passwd, true))) { + printf("[005] Usage of mysql.default_socket failed\n"); +} else { + mysql_close($link); +} + +if (!ini_get('sql.safe_mode')) { + + ini_set('mysql.default_port', $port); + if (!is_resource($link = mysql_connect($host, $user, $passwd, true))) { + printf("[006] Usage of mysql.default_port failed\n"); + } else { + mysql_close($link); + } + + ini_set('mysql.default_password', $passwd); + if (!is_resource($link = mysql_connect($myhost, $user))) { + printf("[007] Usage of mysql.default_password failed\n"); + } else { + mysql_close($link); + } + ini_set('mysql.default_user', $user); + if (!is_resource($link = mysql_connect($myhost))) { + printf("[008] Usage of mysql.default_user failed\n"); + } else { + mysql_close($link); + } + + ini_set('mysql.default_host', $myhost); + if (!is_resource($link = mysql_connect())) { + printf("[009] Usage of mysql.default_host failed\n") ; + } else { + mysql_close($link); + } + + if (!stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'netware')) { + ini_set('mysql.default_port', -1); + if (putenv(sprintf('MYSQL_TCP_PORT=%d', $port))) { + if (!is_resource($link = mysql_connect())) { + printf("[010] Usage of env MYSQL_TCP_PORT failed\n") ; + } else { + mysql_close($link); + } + } else if (putenv(sprintf('MYSQL_TCP_PORT=%d', $port + 1))) { + if (!is_resource($link = mysql_connect())) { + printf("[011] Usage of env MYSQL_TCP_PORT=%d should have failed\n", $port + 1) ; + mysql_close($link); + } + } + } +} + +/* we don't care if we can connect or not, this is just to test the different connect syntax variants */ +$myhost = sprintf('%s:%d', $host, $port - 100); +if ($link = mysql_connect($myhost, $user, $passwd, true)) + mysql_close($link); + +/* interesting, isn't it? */ +$myhost = sprintf('%s:%d:%s', $host, $port - 100, $socket); +if ($link = mysql_connect($myhost, $user, $passwd, true)) + mysql_close($link); + +print "done!"; +?> +--EXPECTF-- +Warning: mysql_connect(): Access denied for user '%s'@'%s' (using password: YES) in %s on line %d +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_constants.phpt b/ext/mysql/tests/mysql_constants.phpt new file mode 100644 index 0000000000..e20d32a346 --- /dev/null +++ b/ext/mysql/tests/mysql_constants.phpt @@ -0,0 +1,62 @@ +--TEST-- +Constants exported by ext/mysql +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php +require("connect.inc"); +require("table.inc"); + +$constants = get_defined_constants(true); +sort($constants); + +$expected_constants = array( + 'MYSQL_ASSOC' => true, + 'MYSQL_NUM' => true, + 'MYSQL_BOTH' => true, + 'MYSQL_CLIENT_COMPRESS' => true, + 'MYSQL_CLIENT_INTERACTIVE' => true, + 'MYSQL_CLIENT_IGNORE_SPACE' => true, +); + +$version = mysql_get_server_info($link); +if (!preg_match('@(\d+)\.(\d+)\.(\d+)@ism', $version, $matches)) + printf("[001] Cannot get server version\n"); +$version = ($matches[1] * 100) + ($matches[2] * 10) + $matches[3]; + +if ($version > 400) { + $expected_constants = array_merge($expected_constants, array( + "MYSQL_CLIENT_SSL" => true, + )); +} + + +$unexpected_constants = array(); + +foreach ($constants as $group => $consts) { + foreach ($consts as $name => $value) { + if (stristr($name, 'mysql') && !stristr($name, 'mysqli')) { + $name = strtoupper($name); + if (isset($expected_constants[$name])) { + unset($expected_constants[$name]); + } else { + $unexpected_constants[$name] = $name; + } + } + } +} + +if (!empty($unexpected_constants)) { + printf("Dumping list of unexpected constants\n"); + var_dump($unexpected_constants); +} + +if (!empty($expected_constants)) { + printf("Dumping list of missing constants\n"); + var_dump($expected_constants); +} + +print "done!"; +?> +--EXPECTF-- +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_create_db.phpt b/ext/mysql/tests/mysql_create_db.phpt new file mode 100644 index 0000000000..0a5d30e97a --- /dev/null +++ b/ext/mysql/tests/mysql_create_db.phpt @@ -0,0 +1,44 @@ +--TEST-- +mysql_create_db() +--SKIPIF-- +<?php +require_once('skipif.inc'); +if (!function_exists('mysql_create_db')) + die("Skip mysql_create_db() exists only in old versions of the libmysql."); +?> +--FILE-- +<?php +include "connect.inc"; + +$link = NULL; +$tmp = null; + +if (false !== ($tmp = mysql_create_db())) + printf("[001] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = mysql_create_db($link, $link, $link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if ($link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket)) + printf("[003] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n", + $host, $user . 'unknown_really', $db, $port, $socket); + +if (!mysql_query("CREATE DATABASE mysqlcreatedb", $link)) + die(sprintf("[004] Cannot create database, aborting test, [%d] %s\n", mysql_errno($link), mysql_error($link))); + +if (!mysql_query("DROP DATABASE mysqlcreatedb", $link)) + printf("[005] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +if (true !== ($tmp = mysql_create_db("mysqlcreatedb", $link))) + printf("[006] Expecting boolean/true, got %s/%s, [%d] %s\n", gettype($tmp), $tmp, mysql_errno($link), mysql_error($link)); + +if (false !== ($tmp = mysql_create_db("mysqlcreatedb", $link))) + printf("[007] Expecting boolean/false, got %s/%s, [%d] %s\n", gettype($tmp), $tmp, mysql_errno($link), mysql_error($link)); + +if (!mysql_query("DROP DATABASE mysqlcreatedb", $link)) + printf("[008] [%d] %s\n", mysql_errno($link), mysql_error($link)); + +print "done!"; +?> +--EXPECTF-- +done! diff --git a/ext/mysql/tests/mysql_errno.phpt b/ext/mysql/tests/mysql_errno.phpt new file mode 100644 index 0000000000..288376ae82 --- /dev/null +++ b/ext/mysql/tests/mysql_errno.phpt @@ -0,0 +1,56 @@ +--TEST-- +mysql_errno() +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (false !== ($tmp = @mysql_errno())) + printf("[001] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = @mysql_errno($link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if (!is_null($tmp = @mysql_errno($link, 'too many args'))) + printf("[002b] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket)) { + printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", + $host, $user, $db, $port, $socket); +} +var_dump(mysql_errno($link)); + +if (!mysql_query('DROP TABLE IF EXISTS test', $link)) { + printf("[004] Failed to drop old test table: [%d] %s\n", mysql_errno($link), mysql_errno($link)); +} + +mysql_query('SELECT * FROM test', $link); +var_dump(mysql_errno($link)); + +mysql_close($link); + +var_dump(mysql_errno($link)); + +if ($link = @mysql_connect($host . '_unknown', $user . '_unknown', $passwd, true)) { + printf("[005] Can connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", + $host . '_unknown', $user . '_unknown', $db, $port, $socket); +} else { + $errno = mysql_errno(); + if (!is_int($errno)) + printf("[006] Expecting int/any (e.g 1046, 2005) got %s/%s\n", gettype($errno), $errno); + +} + +print "done!"; +?> +--EXPECTF-- +int(0) +int(%d) + +Warning: mysql_errno(): %d is not a valid MySQL-Link resource in %s on line %d +bool(false) +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_error.phpt b/ext/mysql/tests/mysql_error.phpt new file mode 100644 index 0000000000..afdd90c338 --- /dev/null +++ b/ext/mysql/tests/mysql_error.phpt @@ -0,0 +1,60 @@ +--TEST-- +mysql_error() +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (false !== ($tmp = @mysql_error())) + printf("[001] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = @mysql_error($link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if (!is_null($tmp = @mysql_error($link, 'too many args'))) + printf("[002b] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket)) { + printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", + $host, $user, $db, $port, $socket); +} + +$tmp = mysql_error($link); +if (!is_string($tmp) || ('' !== $tmp)) + printf("[004] Expecting string/empty, got %s/%s. [%d] %s\n", gettype($tmp), $tmp, mysql_errno($link), mysql_error($link)); + +if (!mysql_query('DROP TABLE IF EXISTS test', $link)) { + printf("[005] Failed to drop old test table: [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +mysql_query('SELECT * FROM test', $link); +$tmp = mysql_error($link); +if (!is_string($tmp) || !preg_match("/Table '\w*\.test' doesn't exist/su", $tmp)) + printf("[006] Expecting string/[Table... doesn't exit], got %s/%s. [%d] %s\n", gettype($tmp), $tmp, mysql_errno($link), mysql_error($link)); + +if (ini_get('unicode.semantics') && !is_unicode($tmp)) { + printf("[007] Expecting Unicode error message!\n"); + var_inspect($tmp); +} + +mysql_close($link); + +var_dump(mysql_error($link)); + +if ($link = @mysql_connect($host . '_unknown', $user . '_unknown', $passwd, true)) { + printf("[008] Can connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", + $host . '_unknown', $user . '_unknown', $db, $port, $socket); +} +if ('' == mysql_error()) + printf("[009] Connect error should have been set\n"); + +print "done!"; +?> +--EXPECTF-- +Warning: mysql_error(): %d is not a valid MySQL-Link resource in %s on line %d +bool(false) +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_escape_string.phpt b/ext/mysql/tests/mysql_escape_string.phpt new file mode 100644 index 0000000000..1badea8903 --- /dev/null +++ b/ext/mysql/tests/mysql_escape_string.phpt @@ -0,0 +1,42 @@ +--TEST-- +mysql_escape_string() +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (NULL !== ($tmp = @mysql_escape_string())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +var_dump(mysql_escape_string("Am I a unicode string in PHP 6?")); +var_dump(mysql_escape_string('\\')); +var_dump(mysql_escape_string('"')); +var_dump(mysql_escape_string("'")); +var_dump(mysql_escape_string("\n")); +var_dump(mysql_escape_string("\r")); +var_dump(mysql_escape_string("foo" . chr(0) . "bar")); + +print "done!"; +?> +--EXPECTF-- +string(31) "Am I a unicode string in PHP 6?" +string(2) "\\" +string(2) "\"" +string(2) "\'" +string(2) "\n" +string(2) "\r" +string(8) "foo\0bar" +done! +--UEXPECTF-- +unicode(31) "Am I a unicode string in PHP 6?" +unicode(2) "\\" +unicode(2) "\"" +unicode(2) "\'" +unicode(2) "\n" +unicode(2) "\r" +unicode(8) "foo\0bar" +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_fetch_array.phpt b/ext/mysql/tests/mysql_fetch_array.phpt new file mode 100644 index 0000000000..f197ae9494 --- /dev/null +++ b/ext/mysql/tests/mysql_fetch_array.phpt @@ -0,0 +1,431 @@ +--TEST-- +mysql_fetch_array() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include_once "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (NULL !== ($tmp = @mysql_fetch_array())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = @mysql_fetch_array($link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT * FROM test ORDER BY id LIMIT 5", $link)) { + printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +print "[005]\n"; +var_dump(mysql_fetch_array($res)); + +print "[006]\n"; +var_dump(mysql_fetch_array($res, MYSQL_NUM)); + +print "[007]\n"; +var_dump(mysql_fetch_array($res, MYSQL_BOTH)); + +print "[008]\n"; +var_dump(mysql_fetch_array($res, MYSQL_ASSOC)); + +print "[009]\n"; +var_dump(mysql_fetch_array($res)); + +mysql_free_result($res); + +if (!$res = mysql_query("SELECT 1 AS a, 2 AS a, 3 AS c, 4 AS C, NULL AS d, true AS e", $link)) { + printf("[010] Cannot run query, [%d] %s\n", mysql_errno($link), mysql_error($link)); +} +print "[011]\n"; +var_dump(mysql_fetch_array($res, MYSQL_BOTH)); + +mysql_free_result($res); +if (!$res = mysql_query("SELECT 1 AS a, 2 AS b, 3 AS c, 4 AS C", $link)) { + printf("[012] Cannot run query, [%d] %s\n", + mysql_errno($link), $mysql_error($link)); +exit(1); +} + +do { + $illegal_mode = mt_rand(0, 10000); +} while (in_array($illegal_mode, array(MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH))); +$tmp = @mysql_fetch_array($res, $illegal_mode); +if (!is_array($tmp)) + printf("[013] Expecting array, got %s/%s. [%d] %s\n", + gettype($tmp), $tmp, mysql_errno($link), mysql_error($link)); + +$tmp = @mysql_fetch_array($res, $illegal_mode); +if (false !== $tmp) + printf("[014] Expecting boolean/false, got %s/%s. [%d] %s\n", + gettype($tmp), $tmp, mysql_errno($link), mysql_error($link)); + +mysql_free_result($res); + +function func_mysql_fetch_array($link, $engine, $sql_type, $sql_value, $php_value, $offset, $regexp_comparison = NULL, $binary_type = false) { + + if (!mysql_query("DROP TABLE IF EXISTS test", $link)) { + printf("[%04d] [%d] %s\n", $offset, mysql_errno($link), mysql_error($link)); + return false; + } + + if (!mysql_query($sql = sprintf("CREATE TABLE test(id INT NOT NULL, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine), $link)) { + print $sql; + // don't bail, engine might not support the datatype + return false; + } + + if (is_null($php_value) && !mysql_query($sql = sprintf("INSERT INTO test(id, label) VALUES (1, NULL)"), $link)) { + printf("[%04d] [%d] %s\n", $offset + 1, mysql_errno($link), mysql_error($link)); + return false; + } + + if (!is_null($php_value)) { + if (is_int($sql_value) && !mysql_query(sprintf("INSERT INTO test(id, label) VALUES (1, '%d')", $sql_value), $link)) { + printf("[%04d] [%d] %s\n", $offset + 1, mysql_errno($link), mysql_error($link)); + return false; + } else if (!is_int($sql_value) && !mysql_query(sprintf("INSERT INTO test(id, label) VALUES (1, '%s')", $sql_value), $link)) { + printf("[%04d] [%d] %s\n", $offset + 1, mysql_errno($link), mysql_error($link)); + return false; + } + } + + if (!$res = mysql_query("SELECT id, label FROM test", $link)) { + printf("[%04d] [%d] %s\n", $offset + 2, mysql_errno($link), mysql_error($link)); + return false; + } + + if (!$row = mysql_fetch_array($res, MYSQL_BOTH)) { + printf("[%04d] [%d] %s\n", $offset + 3, mysql_errno($link), mysql_error($link)); + return false; + } + + if ($regexp_comparison) { + if (!preg_match($regexp_comparison, (string)$row['label']) || !preg_match($regexp_comparison, (string)$row[1])) { + printf("[%04d] Expecting %s/%s [reg exp = %s], got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4, + gettype($php_value), $php_value, $regexp_comparison, + gettype($row[1]), $row[1], + gettype($row['label']), $row['label'], mysql_errno($link), mysql_error($link)); + return false; + } + } else if ((gettype($php_value) == 'unicode') && $binary_type) { + // Unicode is on and we are told that the MySQL column type is a binary type. + // Don't expect a unicode value from the database, you'll get binary string + if (($row['label'] != $php_value) || ($row[1] != $php_value)) { + printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 5, + gettype($php_value), $php_value, + gettype($row[1]), $row[1], + gettype($row['label']), $row['label'], mysql_errno($link), mysql_error($link)); + return false; + } + if (gettype($row['label']) == 'unicode') { + printf("[%04d] SQL Type: '%s', binary columns are supposed to return binary string and not unicode\n", + $offset + 6, $sql_type); + return false; + } + } else { + if (($row['label'] !== $php_value) || ($row[1] != $php_value)) { + printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 7, + gettype($php_value), $php_value, + gettype($row[1]), $row[1], + gettype($row['label']), $row['label'], mysql_errno($link), mysql_error($link)); + return false; + } + } + + return true; +} + +function func_mysql_fetch_array_make_string($len) { + + $ret = ''; + for ($i = 0; $i < $len; $i++) + $ret .= chr(mt_rand(65, 90)); + + return $ret; +} + +func_mysql_fetch_array($link, $engine, "TINYINT", -11, "-11", 20); +func_mysql_fetch_array($link, $engine, "TINYINT", NULL, NULL, 30); +func_mysql_fetch_array($link, $engine, "TINYINT UNSIGNED", 1, "1", 40); +func_mysql_fetch_array($link, $engine, "TINYINT UNSIGNED", NULL, NULL, 50); + +func_mysql_fetch_array($link, $engine, "BOOL", 1, "1", 60); +func_mysql_fetch_array($link, $engine, "BOOL", NULL, NULL, 70); +func_mysql_fetch_array($link, $engine, "BOOLEAN", 0, "0", 80); +func_mysql_fetch_array($link, $engine, "BOOLEAN", NULL, NULL, 90); + +func_mysql_fetch_array($link, $engine, "SMALLINT", -32768, "-32768", 100); +func_mysql_fetch_array($link, $engine, "SMALLINT", 32767, "32767", 110); +func_mysql_fetch_array($link, $engine, "SMALLINT", NULL, NULL, 120); +func_mysql_fetch_array($link, $engine, "SMALLINT UNSIGNED", 65535, "65535", 130); +func_mysql_fetch_array($link, $engine, "SMALLINT UNSIGNED", NULL, NULL, 140); + +func_mysql_fetch_array($link, $engine, "MEDIUMINT", -8388608, "-8388608", 150); +func_mysql_fetch_array($link, $engine, "MEDIUMINT", 8388607, "8388607", 160); +func_mysql_fetch_array($link, $engine, "MEDIUMINT", NULL, NULL, 170); +func_mysql_fetch_array($link, $engine, "MEDIUMINT UNSIGNED", 16777215, "16777215", 180); +func_mysql_fetch_array($link, $engine, "MEDIUMINT UNSIGNED", NULL, NULL, 190); + +func_mysql_fetch_array($link, $engine, "INTEGER", -2147483648, "-2147483648", 200); +func_mysql_fetch_array($link, $engine, "INTEGER", 2147483647, "2147483647", 210); +func_mysql_fetch_array($link, $engine, "INTEGER", NULL, NULL, 220); +func_mysql_fetch_array($link, $engine, "INTEGER UNSIGNED", 4294967295, "4294967295", 230); +func_mysql_fetch_array($link, $engine, "INTEGER UNSIGNED", NULL, NULL, 240); + +// func_mysql_fetch_array($link, $engine, "BIGINT", -9223372036854775808, "-9.22337e+018", 250, "/-9\.22337e\+[0]?18/iu"); +func_mysql_fetch_array($link, $engine, "BIGINT", NULL, NULL, 260); +// func_mysql_fetch_array($link, $engine, "BIGINT UNSIGNED", 18446744073709551615, "1.84467e+019", 270, "/1\.84467e\+[0]?19/iu"); +func_mysql_fetch_array($link, $engine, "BIGINT UNSIGNED", NULL, NULL, 280); + +func_mysql_fetch_array($link, $engine, "FLOAT", -9223372036854775808 - 1.1, "-9.22337e+18", 290, "/-9\.22337e\+[0]?18/iu"); +func_mysql_fetch_array($link, $engine, "FLOAT", NULL, NULL, 300); +func_mysql_fetch_array($link, $engine, "FLOAT UNSIGNED", 18446744073709551615 + 1.1, "1.84467e+19", 310, "/1\.84467e\+[0]?19/iu"); +func_mysql_fetch_array($link, $engine, "FLOAT UNSIGNED ", NULL, NULL, 320); + +func_mysql_fetch_array($link, $engine, "DOUBLE(10,2)", -99999999.99, "-99999999.99", 330); +func_mysql_fetch_array($link, $engine, "DOUBLE(10,2)", NULL, NULL, 340); +func_mysql_fetch_array($link, $engine, "DOUBLE(10,2) UNSIGNED", 99999999.99, "99999999.99", 350); +func_mysql_fetch_array($link, $engine, "DOUBLE(10,2) UNSIGNED", NULL, NULL, 360); + +func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", -99999999.99, "-99999999.99", 370); +func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", NULL, NULL, 380); +func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", 99999999.99, "99999999.99", 390); +func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", NULL, NULL, 400); + +// don't care about date() strict TZ warnings... +func_mysql_fetch_array($link, $engine, "DATE", @date('Y-m-d'), @date('Y-m-d'), 410); +func_mysql_fetch_array($link, $engine, "DATE NOT NULL", @date('Y-m-d'), @date('Y-m-d'), 420); +func_mysql_fetch_array($link, $engine, "DATE", NULL, NULL, 430); + +func_mysql_fetch_array($link, $engine, "DATETIME", @date('Y-m-d H:i:s'), @date('Y-m-d H:i:s'), 440); +func_mysql_fetch_array($link, $engine, "DATETIME NOT NULL", @date('Y-m-d H:i:s'), @date('Y-m-d H:i:s'), 450); +func_mysql_fetch_array($link, $engine, "DATETIME", NULL, NULL, 460); + +func_mysql_fetch_array($link, $engine, "TIMESTAMP", @date('Y-m-d H:i:s'), @date('Y-m-d H:i:s'), 470); + +func_mysql_fetch_array($link, $engine, "TIME", @date('H:i:s'), @date('H:i:s'), 480); +func_mysql_fetch_array($link, $engine, "TIME NOT NULL", @date('H:i:s'), @date('H:i:s'), 490); +func_mysql_fetch_array($link, $engine, "TIME", NULL, NULL, 500); + +func_mysql_fetch_array($link, $engine, "YEAR", @date('Y'), @date('Y'), 510); +func_mysql_fetch_array($link, $engine, "YEAR NOT NULL", @date('Y'), @date('Y'), 520); +func_mysql_fetch_array($link, $engine, "YEAR", NULL, NULL, 530); + +$string255 = func_mysql_fetch_array_make_string(255); + +func_mysql_fetch_array($link, $engine, "CHAR(1)", "a", "a", 540); +func_mysql_fetch_array($link, $engine, "CHAR(255)", $string255, $string255, 550); +func_mysql_fetch_array($link, $engine, "CHAR(1) NOT NULL", "a", "a", 560); +func_mysql_fetch_array($link, $engine, "CHAR(1)", NULL, NULL, 570); + +$string65k = func_mysql_fetch_array_make_string(65535); + +func_mysql_fetch_array($link, $engine, "VARCHAR(1)", "a", "a", 580); +func_mysql_fetch_array($link, $engine, "VARCHAR(255)", $string255, $string255, 590); +func_mysql_fetch_array($link, $engine, "VARCHAR(65635)", $string65k, $string65k, 600); +func_mysql_fetch_array($link, $engine, "VARCHAR(1) NOT NULL", "a", "a", 610); +func_mysql_fetch_array($link, $engine, "VARCHAR(1)", NULL, NULL, 620); + +func_mysql_fetch_array($link, $engine, "BINARY(1)", "a", "a", 630, null , true); +func_mysql_fetch_array($link, $engine, "BINARY(1) NOT NULL", "b", "b", 650, null , true); +func_mysql_fetch_array($link, $engine, "BINARY(1)", NULL, NULL, 660, null , true); + +func_mysql_fetch_array($link, $engine, "VARBINARY(1)", "a", "a", 670, null , true); +func_mysql_fetch_array($link, $engine, "VARBINARY(1) NOT NULL", "b", "b", 690, null , true); +func_mysql_fetch_array($link, $engine, "VARBINARY(1)", NULL, NULL, 700, null , true); + +func_mysql_fetch_array($link, $engine, "TINYBLOB", "a", "a", 710, null , true); +func_mysql_fetch_array($link, $engine, "TINYBLOB NOT NULL", "b", "b", 730, null , true); +func_mysql_fetch_array($link, $engine, "TINYBLOB", NULL, NULL, 740, null , true); + +func_mysql_fetch_array($link, $engine, "TINYTEXT", "a", "a", 750); +func_mysql_fetch_array($link, $engine, "TINYTEXT NOT NULL", "a", "a", 760); +func_mysql_fetch_array($link, $engine, "TINYTEXT", NULL, NULL, 770); + +func_mysql_fetch_array($link, $engine, "BLOB", "a", "a", 780, null , true); +func_mysql_fetch_array($link, $engine, "BLOB", NULL, NULL, 790, null , true); + +func_mysql_fetch_array($link, $engine, "TEXT", "a", "a", 800); +func_mysql_fetch_array($link, $engine, "TEXT", NULL, NULL, 820); + +func_mysql_fetch_array($link, $engine, "MEDIUMBLOB", "a", "a", 830, null , true); +func_mysql_fetch_array($link, $engine, "MEDIUMBLOB", NULL, NULL, 850, null , true); + +func_mysql_fetch_array($link, $engine, "MEDIUMTEXT", "a", "a", 860); +func_mysql_fetch_array($link, $engine, "MEDIUMTEXT", NULL, NULL, 880); + +func_mysql_fetch_array($link, $engine, "LONGBLOB", "a", "a", 890, null , true); +func_mysql_fetch_array($link, $engine, "LONGBLOB", NULL, NULL, 910, null , true); + +func_mysql_fetch_array($link, $engine, "ENUM('a', 'b')", "a", "a", 920); +func_mysql_fetch_array($link, $engine, "ENUM('a', 'b')", NULL, NULL, 930); + +func_mysql_fetch_array($link, $engine, "SET('a', 'b')", "a", "a", 940); +func_mysql_fetch_array($link, $engine, "SET('a', 'b')", NULL, NULL, 950); + +mysql_close($link); + +if (false !== ($tmp = mysql_fetch_array($res, MYSQL_ASSOC))) +printf("[015] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +print "done!"; +?> +--EXPECTF-- +[005] +array(4) { + [0]=> + string(1) "1" + ["id"]=> + string(1) "1" + [1]=> + string(1) "a" + ["label"]=> + string(1) "a" +} +[006] +array(2) { + [0]=> + string(1) "2" + [1]=> + string(1) "b" +} +[007] +array(4) { + [0]=> + string(1) "3" + ["id"]=> + string(1) "3" + [1]=> + string(1) "c" + ["label"]=> + string(1) "c" +} +[008] +array(2) { + ["id"]=> + string(1) "4" + ["label"]=> + string(1) "d" +} +[009] +array(4) { + [0]=> + string(1) "5" + ["id"]=> + string(1) "5" + [1]=> + string(1) "e" + ["label"]=> + string(1) "e" +} +[011] +array(11) { + [0]=> + string(1) "1" + ["a"]=> + string(1) "2" + [1]=> + string(1) "2" + [2]=> + string(1) "3" + ["c"]=> + string(1) "3" + [3]=> + string(1) "4" + ["C"]=> + string(1) "4" + [4]=> + NULL + ["d"]=> + NULL + [5]=> + string(1) "1" + ["e"]=> + string(1) "1" +} + +Warning: mysql_fetch_array(): %d is not a valid MySQL result resource in %s on line %d +done! +--UEXPECTF-- +[005] +array(4) { + [0]=> + unicode(1) "1" + [u"id"]=> + unicode(1) "1" + [1]=> + unicode(1) "a" + [u"label"]=> + unicode(1) "a" +} +[006] +array(2) { + [0]=> + unicode(1) "2" + [1]=> + unicode(1) "b" +} +[007] +array(4) { + [0]=> + unicode(1) "3" + [u"id"]=> + unicode(1) "3" + [1]=> + unicode(1) "c" + [u"label"]=> + unicode(1) "c" +} +[008] +array(2) { + [u"id"]=> + unicode(1) "4" + [u"label"]=> + unicode(1) "d" +} +[009] +array(4) { + [0]=> + unicode(1) "5" + [u"id"]=> + unicode(1) "5" + [1]=> + unicode(1) "e" + [u"label"]=> + unicode(1) "e" +} +[011] +array(11) { + [0]=> + unicode(1) "1" + [u"a"]=> + unicode(1) "2" + [1]=> + unicode(1) "2" + [2]=> + unicode(1) "3" + [u"c"]=> + unicode(1) "3" + [3]=> + unicode(1) "4" + [u"C"]=> + unicode(1) "4" + [4]=> + NULL + [u"d"]=> + NULL + [5]=> + unicode(1) "1" + [u"e"]=> + unicode(1) "1" +} + +Warning: mysql_fetch_array(): %d is not a valid MySQL result resource in %s on line %d +done! diff --git a/ext/mysql/tests/mysql_fetch_assoc.phpt b/ext/mysql/tests/mysql_fetch_assoc.phpt new file mode 100644 index 0000000000..172917b6fd --- /dev/null +++ b/ext/mysql/tests/mysql_fetch_assoc.phpt @@ -0,0 +1,138 @@ +--TEST-- +mysql_fetch_assoc() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +// Note: no SQL type tests, internally the same function gets used as for mysql_fetch_array() which does a lot of SQL type test + +if (!is_null($tmp = @mysql_fetch_assoc())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = @mysql_fetch_assoc($link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 1", $link)) { + printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +print "[005]\n"; +var_dump(mysql_fetch_assoc($res)); + +print "[006]\n"; +var_dump(mysql_fetch_assoc($res)); + +mysql_free_result($res); + +if (!$res = mysql_query("SELECT 1 AS a, 2 AS a, 3 AS c, 4 AS C, NULL AS d, true AS e", $link)) { + printf("[007] Cannot run query, [%d] %s\n", mysql_errno($link), $mysql_error($link)); +} +print "[008]\n"; +var_dump(mysql_fetch_assoc($res)); + +mysql_free_result($res); + +if (false !== ($tmp = mysql_fetch_assoc($res))) + printf("[008] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +mysql_close($link); + +include('table.inc'); +if (!$res = mysql_query("SELECT id, label, id AS _id, CONCAT(label, 'a') _label, NULL as _foo FROM test _test ORDER BY id ASC LIMIT 1", $link)) { + printf("[009] [%d] %s\n", mysql_errno($link), $mysql_error($link)); +} +print "[010]\n"; +var_dump(mysql_fetch_assoc($res)); +mysql_free_result($res); + +mysql_close($link); + +print "done!"; +?> +--EXPECTF-- +[005] +array(2) { + ["id"]=> + string(1) "1" + ["label"]=> + string(1) "a" +} +[006] +bool(false) +[008] +array(5) { + ["a"]=> + string(1) "2" + ["c"]=> + string(1) "3" + ["C"]=> + string(1) "4" + ["d"]=> + NULL + ["e"]=> + string(1) "1" +} + +Warning: mysql_fetch_assoc(): %d is not a valid MySQL result resource in %s on line %d +[010] +array(5) { + ["id"]=> + string(1) "1" + ["label"]=> + string(1) "a" + ["_id"]=> + string(1) "1" + ["_label"]=> + string(2) "aa" + ["_foo"]=> + NULL +} +done! +--UEXPECTF-- +[005] +array(2) { + [u"id"]=> + unicode(1) "1" + [u"label"]=> + unicode(1) "a" +} +[006] +bool(false) +[008] +array(5) { + [u"a"]=> + unicode(1) "2" + [u"c"]=> + unicode(1) "3" + [u"C"]=> + unicode(1) "4" + [u"d"]=> + NULL + [u"e"]=> + unicode(1) "1" +} + +Warning: mysql_fetch_assoc(): %d is not a valid MySQL result resource in %s on line %d +[010] +array(5) { + [u"id"]=> + unicode(1) "1" + [u"label"]=> + unicode(1) "a" + [u"_id"]=> + unicode(1) "1" + [u"_label"]=> + unicode(2) "aa" + [u"_foo"]=> + NULL +} +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_fetch_field.phpt b/ext/mysql/tests/mysql_fetch_field.phpt new file mode 100644 index 0000000000..7a50e23ca0 --- /dev/null +++ b/ext/mysql/tests/mysql_fetch_field.phpt @@ -0,0 +1,373 @@ +--TEST-- +mysql_fetch_field() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php + include "connect.inc"; + + $tmp = NULL; + $link = NULL; + + // Note: no SQL type tests, internally the same function gets used as for mysql_fetch_array() which does a lot of SQL type test + if (!is_null($tmp = @mysql_fetch_field())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + + if (false !== ($tmp = @mysql_fetch_field($link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + + require('table.inc'); + + $version = mysql_get_server_info($link); + if (!preg_match('@(\d+)\.(\d+)\.(\d+)@ism', $version, $matches)) + printf("[003] Cannot get server version\n"); + $version = ($matches[1] * 100) + ($matches[2] * 10) + $matches[3]; + + if (!$res = mysql_query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1", $link)) { + printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link)); + } + + while ($tmp = mysql_fetch_field($res)) + var_dump($tmp); + var_dump($tmp); + + mysql_free_result($res); + + if (!$res = mysql_query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1", $link)) { + printf("[005] [%d] %s\n", mysql_errno($link), mysql_error($link)); + } + if (false !== ($tmp = mysql_fetch_field($res, PHP_INT_MAX * 2))) + printf("[006] Expecting boolean/false got %s/%s\n", gettype($tmp), $tmp); + + mysql_free_result($res); + + if (false !== ($tmp = mysql_fetch_field($res))) + printf("[007] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + + $types = array( + 'BIT' => array(1, 'int'), + 'TINYINT' => array(1, 'int'), + 'BOOL' => array('true', 'int'), + 'SMALLINT' => array(32767, 'int'), + 'MEDIUMINT' => array(8388607, 'int'), + 'INT' => array(100, 'int'), + 'BIGINT' => array(100, 'int'), + 'FLOAT' => array(100, 'real'), + 'DOUBLE' => array(100, 'real'), + 'DECIMAL' => array(100, 'real'), + 'DATE' => array(@date('Y-m-d'), 'date'), + 'DATETIME' => array(@date('Y-m-d H:i:s'), 'datetime'), + 'TIMESTAMP' => array(@date('Y-m-d H:i:s'), 'timestamp'), + 'TIME' => array(@date('H:i:s'), 'time'), + 'YEAR' => array(@date('Y'), 'year'), + 'CHAR(1)' => array('a', 'string'), + 'VARCHAR(1)' => array('a', 'string'), + 'BINARY(1)' => array('a', 'string'), + 'VARBINARY(1)' => array('a', 'string'), + 'TINYBLOB' => array('a', 'blob'), + 'TINYTEXT' => array('a', 'blob'), + 'BLOB' => array('a', 'blob'), + 'TEXT' => array('a', 'blob'), + 'MEDIUMBLOB' => array('a', 'blob'), + 'MEDIUMTEXT' => array('a', 'blob'), + 'LONGBLOB' => array('a', 'blob'), + 'LONGTEXT' => array('a', 'blob'), + 'ENUM("a", "b")' => array('a', 'string'), /* !!! */ + 'SET("a", "b")' => array('a', 'string'), /* !!! */ + ); + + foreach ($types as $type_name => $type_desc) { + if (!mysql_query("DROP TABLE IF EXISTS test", $link)) + printf("[008/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link)); + if (!mysql_query(sprintf("CREATE TABLE test(id INT, label %s) ENGINE = %s", $type_name, $engine), $link)) { + // server and/or engine might not support the data type + continue; + } + if (!mysql_query(sprintf("INSERT INTO test(id, label) VALUES (1, '%s')", $type_desc[0]), $link)) { + printf("[009/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link)); + continue; + } + if (!$res = mysql_query("SELECT id, label FROM test", $link)) { + printf("[010/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link)); + continue; + } + if (!$tmp = mysql_fetch_field($res, 1)) { + printf("[011/%s] [%d] %s\n", $type_name, mysql_errno($link), mysql_error($link)); + } + + if ($type_desc[1] != $tmp->type) { + printf("[012/%s] Expecting type '%s' got '%s'\n", $type_name, $type_desc[1], $tmp->type); + } + mysql_free_result($res); + } + + if (!mysql_query("DROP TABLE IF EXISTS test", $link)) + printf("[013] [%d] %s\n", mysql_errno($link), mysql_error($link)); + + if (!mysql_query("CREATE TABLE test(id INT DEFAULT 1)")) + printf("[014] [%d] %s\n", mysql_errno($link), mysql_error($link)); + + if (!mysql_query("INSERT INTO test(id) VALUES (2)")) + printf("[015] [%d] %s\n", mysql_errno($link), mysql_error($link)); + + if (!$res = mysql_query("SELECT id FROM test", $link)) { + printf("[016] [%d] %s\n", mysql_errno($link), mysql_error($link)); + } + + var_dump(mysql_fetch_field($res)); + mysql_free_result($res); + + if (!$res = mysql_query("SELECT id FROM test", $link)) { + printf("[017] [%d] %s\n", mysql_errno($link), mysql_error($link)); + } + $res = mysql_list_fields($db, 'test'); + while ($tmp = mysql_fetch_field($res)) + if ($tmp->name == 'id') + var_dump($tmp); + + mysql_free_result($res); + + mysql_close($link); + print "done!"; +?> +--EXPECTF-- +object(stdClass)#%d (13) { + ["name"]=> + string(2) "ID" + ["table"]=> + string(4) "TEST" + ["def"]=> + string(0) "" + ["max_length"]=> + int(1) + ["not_null"]=> + int(1) + ["primary_key"]=> + int(1) + ["multiple_key"]=> + int(0) + ["unique_key"]=> + int(0) + ["numeric"]=> + int(1) + ["blob"]=> + int(0) + ["type"]=> + string(3) "int" + ["unsigned"]=> + int(0) + ["zerofill"]=> + int(0) +} +object(stdClass)#%d (13) { + ["name"]=> + string(5) "label" + ["table"]=> + string(4) "TEST" + ["def"]=> + string(0) "" + ["max_length"]=> + int(1) + ["not_null"]=> + int(0) + ["primary_key"]=> + int(0) + ["multiple_key"]=> + int(0) + ["unique_key"]=> + int(0) + ["numeric"]=> + int(0) + ["blob"]=> + int(0) + ["type"]=> + string(6) "string" + ["unsigned"]=> + int(0) + ["zerofill"]=> + int(0) +} +bool(false) + +Warning: mysql_fetch_field(): Bad field offset in %s on line %d + +Warning: mysql_fetch_field(): %d is not a valid MySQL result resource in %s on line %d +object(stdClass)#%d (13) { + ["name"]=> + string(2) "id" + ["table"]=> + string(4) "test" + ["def"]=> + string(0) "" + ["max_length"]=> + int(1) + ["not_null"]=> + int(0) + ["primary_key"]=> + int(0) + ["multiple_key"]=> + int(0) + ["unique_key"]=> + int(0) + ["numeric"]=> + int(1) + ["blob"]=> + int(0) + ["type"]=> + string(3) "int" + ["unsigned"]=> + int(0) + ["zerofill"]=> + int(0) +} +object(stdClass)#%d (13) { + ["name"]=> + string(2) "id" + ["table"]=> + string(4) "test" + ["def"]=> + string(1) "1" + ["max_length"]=> + int(0) + ["not_null"]=> + int(0) + ["primary_key"]=> + int(0) + ["multiple_key"]=> + int(0) + ["unique_key"]=> + int(0) + ["numeric"]=> + int(1) + ["blob"]=> + int(0) + ["type"]=> + string(3) "int" + ["unsigned"]=> + int(0) + ["zerofill"]=> + int(0) +} +done! +--UEXPECTF-- +object(stdClass)#%d (13) { + [u"name"]=> + unicode(2) "ID" + [u"table"]=> + unicode(4) "TEST" + [u"def"]=> + unicode(0) "" + [u"max_length"]=> + int(1) + [u"not_null"]=> + int(1) + [u"primary_key"]=> + int(1) + [u"multiple_key"]=> + int(0) + [u"unique_key"]=> + int(0) + [u"numeric"]=> + int(1) + [u"blob"]=> + int(0) + [u"type"]=> + unicode(3) "int" + [u"unsigned"]=> + int(0) + [u"zerofill"]=> + int(0) +} +object(stdClass)#%d (13) { + [u"name"]=> + unicode(5) "label" + [u"table"]=> + unicode(4) "TEST" + [u"def"]=> + unicode(0) "" + [u"max_length"]=> + int(1) + [u"not_null"]=> + int(0) + [u"primary_key"]=> + int(0) + [u"multiple_key"]=> + int(0) + [u"unique_key"]=> + int(0) + [u"numeric"]=> + int(0) + [u"blob"]=> + int(0) + [u"type"]=> + unicode(6) "string" + [u"unsigned"]=> + int(0) + [u"zerofill"]=> + int(0) +} +bool(false) + +Warning: mysql_fetch_field(): Bad field offset in %s on line %d + +Warning: mysql_fetch_field(): %d is not a valid MySQL result resource in %s on line %d +object(stdClass)#%d (13) { + [u"name"]=> + unicode(2) "id" + [u"table"]=> + unicode(4) "test" + [u"def"]=> + unicode(0) "" + [u"max_length"]=> + int(1) + [u"not_null"]=> + int(0) + [u"primary_key"]=> + int(0) + [u"multiple_key"]=> + int(0) + [u"unique_key"]=> + int(0) + [u"numeric"]=> + int(1) + [u"blob"]=> + int(0) + [u"type"]=> + unicode(3) "int" + [u"unsigned"]=> + int(0) + [u"zerofill"]=> + int(0) +} +object(stdClass)#%d (13) { + [u"name"]=> + unicode(2) "id" + [u"table"]=> + unicode(4) "test" + [u"def"]=> + unicode(1) "1" + [u"max_length"]=> + int(0) + [u"not_null"]=> + int(0) + [u"primary_key"]=> + int(0) + [u"multiple_key"]=> + int(0) + [u"unique_key"]=> + int(0) + [u"numeric"]=> + int(1) + [u"blob"]=> + int(0) + [u"type"]=> + unicode(3) "int" + [u"unsigned"]=> + int(0) + [u"zerofill"]=> + int(0) +} +done! diff --git a/ext/mysql/tests/mysql_fetch_lengths.phpt b/ext/mysql/tests/mysql_fetch_lengths.phpt new file mode 100644 index 0000000000..32c6ea9094 --- /dev/null +++ b/ext/mysql/tests/mysql_fetch_lengths.phpt @@ -0,0 +1,48 @@ +--TEST-- +mysql_fetch_lengths() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!is_null($tmp = @mysql_fetch_lengths())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = @mysql_fetch_lengths($link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 1", $link)) { + printf("[003] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +while ($row = mysql_fetch_assoc($res)) + var_dump(mysql_fetch_lengths($res)); +var_dump(mysql_fetch_lengths($res)); + +mysql_free_result($res); + +var_dump(mysql_fetch_lengths($res)); + +mysql_close($link); +print "done!"; +?> +--EXPECTF-- +array(2) { + [0]=> + int(1) + [1]=> + int(1) +} +bool(false) + +Warning: mysql_fetch_lengths(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_fetch_object.phpt b/ext/mysql/tests/mysql_fetch_object.phpt new file mode 100644 index 0000000000..cc20f738b0 --- /dev/null +++ b/ext/mysql/tests/mysql_fetch_object.phpt @@ -0,0 +1,217 @@ +--TEST-- +mysql_fetch_object() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!is_null($tmp = @mysql_fetch_object())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = @mysql_fetch_object($link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5", $link)) { + printf("[003] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +var_dump(mysql_fetch_object($res)); + +class mysql_fetch_object_test { + + public $a = null; + public $b = null; + + public function toString() { + var_dump($this); + } +} + +var_dump(mysql_fetch_object($res, 'mysql_fetch_object_test')); + +class mysql_fetch_object_construct extends mysql_fetch_object_test { + + public function __construct($a, $b) { + $this->a = $a; + $this->b = $b; + } + +} + +var_dump(mysql_fetch_object($res, 'mysql_fetch_object_construct', null)); +var_dump(mysql_fetch_object($res, 'mysql_fetch_object_construct', array('a'))); +var_dump(mysql_fetch_object($res, 'mysql_fetch_object_construct', array('a', 'b'))); +var_dump(mysql_fetch_object($res, 'mysql_fetch_object_construct', array('a', 'b', 'c'))); +var_dump(mysql_fetch_object($res, 'mysql_fetch_object_construct', "no array and not null")); +var_dump(mysql_fetch_object($res)); +var_dump(mysql_fetch_object($res, 'mysql_fetch_object_construct', array('a', 'b'))); + +class mysql_fetch_object_private_construct { + private function __construct($a, $b) { + var_dump($a); + } +} +var_dump(mysql_fetch_object($res, 'mysql_fetch_object_private_construct', array('a', 'b'))); + +mysql_free_result($res); + +if (!$res = mysql_query("SELECT id AS ID, label FROM test AS TEST", $link)) { + printf("[009] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +mysql_free_result($res); + +var_dump(mysql_fetch_object($res)); + +// Fatal error, script execution will end +var_dump(mysql_fetch_object($res, 'this_class_does_not_exist')); + +mysql_close($link); +print "done!"; +?> +--EXPECTF-- +object(stdClass)#%d (2) { + ["ID"]=> + string(1) "1" + ["label"]=> + string(1) "a" +} +object(mysql_fetch_object_test)#%d (4) { + ["a"]=> + NULL + ["b"]=> + NULL + ["ID"]=> + string(1) "2" + ["label"]=> + string(1) "b" +} + +Warning: Missing argument 1 for mysql_fetch_object_construct::__construct() in %s on line %d + +Warning: Missing argument 2 for mysql_fetch_object_construct::__construct() in %s on line %d + +Notice: Undefined variable: a in %s on line %d + +Notice: Undefined variable: b in %s on line %d +object(mysql_fetch_object_construct)#%d (4) { + ["a"]=> + NULL + ["b"]=> + NULL + ["ID"]=> + string(1) "3" + ["label"]=> + string(1) "c" +} + +Warning: Missing argument 2 for mysql_fetch_object_construct::__construct() in %s on line %d + +Notice: Undefined variable: b in %s on line %d +object(mysql_fetch_object_construct)#%d (4) { + ["a"]=> + string(1) "a" + ["b"]=> + NULL + ["ID"]=> + string(1) "4" + ["label"]=> + string(1) "d" +} +object(mysql_fetch_object_construct)#%d (4) { + ["a"]=> + string(1) "a" + ["b"]=> + string(1) "b" + ["ID"]=> + string(1) "5" + ["label"]=> + string(1) "e" +} +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) + +Warning: mysql_fetch_object(): %d is not a valid MySQL result resource in %s on line %d +bool(false) + +Fatal error: Class 'this_class_does_not_exist' not found in %s on line %d +--UEXPECTF-- +object(stdClass)#%d (2) { + [u"ID"]=> + unicode(1) "1" + [u"label"]=> + unicode(1) "a" +} +object(mysql_fetch_object_test)#%d (4) { + [u"a"]=> + NULL + [u"b"]=> + NULL + [u"ID"]=> + unicode(1) "2" + [u"label"]=> + unicode(1) "b" +} + +Warning: Missing argument 1 for mysql_fetch_object_construct::__construct() in %s on line %d + +Warning: Missing argument 2 for mysql_fetch_object_construct::__construct() in %s on line %d + +Notice: Undefined variable: a in %s on line %d + +Notice: Undefined variable: b in %s on line %d +object(mysql_fetch_object_construct)#%d (4) { + [u"a"]=> + NULL + [u"b"]=> + NULL + [u"ID"]=> + unicode(1) "3" + [u"label"]=> + unicode(1) "c" +} + +Warning: Missing argument 2 for mysql_fetch_object_construct::__construct() in %s on line %d + +Notice: Undefined variable: b in %s on line %d +object(mysql_fetch_object_construct)#%d (4) { + [u"a"]=> + unicode(1) "a" + [u"b"]=> + NULL + [u"ID"]=> + unicode(1) "4" + [u"label"]=> + unicode(1) "d" +} +object(mysql_fetch_object_construct)#%d (4) { + [u"a"]=> + unicode(1) "a" + [u"b"]=> + unicode(1) "b" + [u"ID"]=> + unicode(1) "5" + [u"label"]=> + unicode(1) "e" +} +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) + +Warning: mysql_fetch_object(): %d is not a valid MySQL result resource in %s on line %d +bool(false) + +Fatal error: Class 'this_class_does_not_exist' not found in %s on line %d diff --git a/ext/mysql/tests/mysql_fetch_row.phpt b/ext/mysql/tests/mysql_fetch_row.phpt new file mode 100644 index 0000000000..b98f29c4da --- /dev/null +++ b/ext/mysql/tests/mysql_fetch_row.phpt @@ -0,0 +1,80 @@ +--TEST-- +mysql_fetch_row() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!is_null($tmp = @mysql_fetch_row())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = @mysql_fetch_row($link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 1", $link)) { + printf("[003] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +print "[004]\n"; +var_dump(mysql_fetch_row($res)); + +print "[005]\n"; +var_dump(mysql_fetch_row($res)); + +mysql_free_result($res); + +var_dump(mysql_fetch_row($res)); + +mysql_close($link); +print "done!"; +?> +--EXPECTF-- +[004] +array(2) { + [0]=> + string(1) "1" + [1]=> + string(1) "a" +} +[005] +bool(false) + +Warning: mysql_fetch_row(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done! +--EXPECTF-- +[004] +array(2) { + [0]=> + string(1) "1" + [1]=> + string(1) "a" +} +[005] +bool(false) + +Warning: mysql_fetch_row(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done! +--UEXPECTF-- +[004] +array(2) { + [0]=> + unicode(1) "1" + [1]=> + unicode(1) "a" +} +[005] +bool(false) + +Warning: mysql_fetch_row(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_field_flags.phpt b/ext/mysql/tests/mysql_field_flags.phpt new file mode 100644 index 0000000000..4a63c907fc --- /dev/null +++ b/ext/mysql/tests/mysql_field_flags.phpt @@ -0,0 +1,147 @@ +--TEST-- +mysql_field_flags() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!is_null($tmp = @mysql_field_flags())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (null !== ($tmp = @mysql_field_flags($link))) + printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 2", $link)) { + printf("[003] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +if (NULL !== ($tmp = mysql_field_flags($res))) + printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = mysql_field_flags($res, -1))) + printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if (!is_string($tmp = mysql_field_flags($res, 0)) || empty($tmp)) + printf("[006] Expecting non empty string, got %s/%s\n", gettype($tmp), $tmp); + +if (ini_get('unicode.semantics') && !is_unicode($tmp)) { + printf("[007] Check the unicode support!\n"); + var_inspect($tmp); +} + +if (false !== ($tmp = mysql_field_flags($res, 2))) + printf("[008] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +mysql_free_result($res); + +$version = mysql_get_server_info($link); +if (!preg_match('@(\d+)\.(\d+)\.(\d+)@ism', $version, $matches)) + printf("[009] Cannot get server version\n"); +$version = ($matches[1] * 100) + ($matches[2] * 10) + $matches[3]; + +$tables = array( + 'label INT, UNIQUE KEY (label)' => array( + array('label', '1'), + 'label' => array(($version < 500) ? 'multiple_key' : 'unique_key') + ), + 'labela INT, label2 CHAR(1), KEY keyname (labela, label2)' => array( + array('labela, label2', '1, "a"'), + 'labela' => array('multiple_key'), + ), + 'label1 BLOB' => array( + array('label1', '"blob"'), + 'label1' => array('blob', 'binary'), + ), + 'label1 INT UNSIGNED' => array( + array('label1', '100'), + 'label1' => array('unsigned'), + ), + 'label1 INT UNSIGNED NOT NULL AUTO INCREMENT' => array( + array('label1', '100'), + 'label1' => array('auto_increment', + 'unsigned'), + ), + 'label1 ENUM("a", "b")' => array( + array('label1', '"a"'), + 'label1' => array('enum'), + ), + 'label1 SET("a", "b")' => array( + array('label1', '"a"'), + 'label1' => array('set'), + ), + 'label1 TIMESTAMP' => array( + array('label1', sprintf('"%s"', @date("Y-m-d H:i:s"))), + 'label1' => array( + 'timestamp', + 'unsigned', + 'zerofill', + 'binary', + 'not_null'), + ), +); + +foreach ($tables as $columns => $expected) { + if (!mysql_query("DROP TABLE IF EXISTS test", $link)) { + printf("[010/%s] [%d] %s\n", $columns, mysql_errno($link), mysql_error($link)); + continue; + } + $sql = sprintf("CREATE TABLE test(id INT, %s) ENGINE = %s", $columns, $engine); + if (!@mysql_query($sql, $link)) { + // server or engine might not support this + continue; + } + + reset($expected); + list($k, $values) = each($expected); + $sql = sprintf("INSERT INTO test(id, %s) VALUES (1, %s)", $values[0], $values[1]); + if (!mysql_query($sql, $link)) { + printf("[011/%s] '%s', [%d] %s\n", $columns, $sql, mysql_errno($link), mysql_error($link)); + continue; + } + + if (!$res = mysql_query(sprintf("SELECT id, %s FROM test", $values[0]), $link)) { + printf("[012/%s] [%d] %s\n", $columns, mysql_errno($link), mysql_error($link)); + continue; + } + + $i = 1; + while (list($field, $flags) = each($expected)) { + $tmp = mysql_field_flags($res, $i++); + + foreach ($flags as $k => $flag) { + if (!preg_match(sprintf('@\s*%s\s*@ismU', $flag), $tmp)) { + printf("[013/%s] Field '%s', flag '%s' not found, [%d] %s\n", $columns, $field, $flag, mysql_errno($link), mysql_error($link)); + } + } + foreach ($flags as $k => $flag) { + $tmp = preg_replace(sprintf('@\s*%s\s*@ismU', $flag), '', $tmp); + } + if ('' != $tmp) + printf("[014/%s] Field '%s', unexpected flags '%s' found, [%d] %s\n", $columns, $field, $tmp, mysql_errno($link), mysql_error($link)); + } + mysql_free_result($res); +} + +var_dump(mysql_field_flags($res, 0)); + +mysql_close($link); +print "done!"; +?> +--EXPECTF-- +Warning: Wrong parameter count for mysql_field_flags() in %s on line %d + +Warning: mysql_field_flags(): Field -1 is invalid for MySQL result index %d in %s on line %d + +Warning: mysql_field_flags(): Field 2 is invalid for MySQL result index %d in %s on line %d + +Warning: mysql_field_flags(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_field_len.phpt b/ext/mysql/tests/mysql_field_len.phpt new file mode 100644 index 0000000000..76c298112c --- /dev/null +++ b/ext/mysql/tests/mysql_field_len.phpt @@ -0,0 +1,54 @@ +--TEST-- +mysql_field_len() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!is_null($tmp = @mysql_field_len())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (null !== ($tmp = @mysql_field_len($link))) + printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 2", $link)) { + printf("[003] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +if (NULL !== ($tmp = mysql_field_len($res))) +printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = mysql_field_len($res, -1))) + printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +if (!is_int($tmp = mysql_field_len($res, 0)) || empty($tmp)) + printf("[006] Expecting non empty integer, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = mysql_field_len($res, 2))) + printf("[008] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +mysql_free_result($res); + +var_dump(mysql_field_len($res, 0)); + +mysql_close($link); +print "done!"; +?> +--EXPECTF-- +Warning: Wrong parameter count for mysql_field_len() in %s on line %d + +Warning: mysql_field_len(): Field -1 is invalid for MySQL result index %d in %s on line %d + +Warning: mysql_field_len(): Field 2 is invalid for MySQL result index %d in %s on line %d + +Warning: mysql_field_len(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_field_name.phpt b/ext/mysql/tests/mysql_field_name.phpt new file mode 100644 index 0000000000..c37ba6eb04 --- /dev/null +++ b/ext/mysql/tests/mysql_field_name.phpt @@ -0,0 +1,65 @@ +--TEST-- +mysql_field_name() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!is_null($tmp = @mysql_field_name())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (null !== ($tmp = @mysql_field_name($link))) + printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 2", $link)) { + printf("[003] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +if (NULL !== ($tmp = mysql_field_name($res))) + printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = mysql_field_name($res, -1))) + printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +var_dump(mysql_field_name($res, 0)); + +if (false !== ($tmp = mysql_field_name($res, 2))) + printf("[008] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +mysql_free_result($res); + +var_dump(mysql_field_name($res, 0)); + +mysql_close($link); +print "done!"; +?> +--EXPECTF-- +Warning: Wrong parameter count for mysql_field_name() in %s on line %d + +Warning: mysql_field_name(): Field -1 is invalid for MySQL result index %d in %s on line %d +string(2) "id" + +Warning: mysql_field_name(): Field 2 is invalid for MySQL result index %d in %s on line %d + +Warning: mysql_field_name(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done! +--UEXPECTF-- +Warning: Wrong parameter count for mysql_field_name() in %s on line %d + +Warning: mysql_field_name(): Field -1 is invalid for MySQL result index %d in %s on line %d +unicode(2) "id" + +Warning: mysql_field_name(): Field 2 is invalid for MySQL result index %d in %s on line %d + +Warning: mysql_field_name(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_field_seek.phpt b/ext/mysql/tests/mysql_field_seek.phpt new file mode 100644 index 0000000000..e5105d984d --- /dev/null +++ b/ext/mysql/tests/mysql_field_seek.phpt @@ -0,0 +1,235 @@ +--TEST-- +mysql_field_seek() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!is_null($tmp = @mysql_field_seek())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (!is_null($tmp = @mysql_field_seek($link))) + printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 1", $link)) { + printf("[003] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +var_dump(mysql_field_seek($res, -1)); +var_dump(mysql_fetch_field($res)); +var_dump(mysql_field_seek($res, 0)); +var_dump(mysql_fetch_field($res)); +var_dump(mysql_field_seek($res, 1)); +var_dump(mysql_fetch_field($res)); +var_dump(mysql_field_seek($res, 2)); +var_dump(mysql_fetch_field($res)); + +mysql_free_result($res); + +var_dump(mysql_field_seek($res, 0)); + +mysql_close($link); +print "done!"; +?> +--EXPECTF-- +Warning: mysql_field_seek(): Field -1 is invalid for MySQL result index %d in %s on line %d +bool(false) +object(stdClass)#%d (13) { + ["name"]=> + string(2) "id" + ["table"]=> + string(4) "test" + ["def"]=> + string(0) "" + ["max_length"]=> + int(1) + ["not_null"]=> + int(1) + ["primary_key"]=> + int(1) + ["multiple_key"]=> + int(0) + ["unique_key"]=> + int(0) + ["numeric"]=> + int(1) + ["blob"]=> + int(0) + ["type"]=> + string(3) "int" + ["unsigned"]=> + int(0) + ["zerofill"]=> + int(0) +} +bool(true) +object(stdClass)#%d (13) { + ["name"]=> + string(2) "id" + ["table"]=> + string(4) "test" + ["def"]=> + string(0) "" + ["max_length"]=> + int(1) + ["not_null"]=> + int(1) + ["primary_key"]=> + int(1) + ["multiple_key"]=> + int(0) + ["unique_key"]=> + int(0) + ["numeric"]=> + int(1) + ["blob"]=> + int(0) + ["type"]=> + string(3) "int" + ["unsigned"]=> + int(0) + ["zerofill"]=> + int(0) +} +bool(true) +object(stdClass)#%d (13) { + ["name"]=> + string(5) "label" + ["table"]=> + string(4) "test" + ["def"]=> + string(0) "" + ["max_length"]=> + int(1) + ["not_null"]=> + int(0) + ["primary_key"]=> + int(0) + ["multiple_key"]=> + int(0) + ["unique_key"]=> + int(0) + ["numeric"]=> + int(0) + ["blob"]=> + int(0) + ["type"]=> + string(6) "string" + ["unsigned"]=> + int(0) + ["zerofill"]=> + int(0) +} + +Warning: mysql_field_seek(): Field %d is invalid for MySQL result index %d in %s on line %d +bool(false) +bool(false) + +Warning: mysql_field_seek(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done! +--UEXPECTF-- +Warning: mysql_field_seek(): Field -1 is invalid for MySQL result index %d in %s on line %d +bool(false) +object(stdClass)#%d (13) { + [u"name"]=> + unicode(2) "id" + [u"table"]=> + unicode(4) "test" + [u"def"]=> + unicode(0) "" + [u"max_length"]=> + int(1) + [u"not_null"]=> + int(1) + [u"primary_key"]=> + int(1) + [u"multiple_key"]=> + int(0) + [u"unique_key"]=> + int(0) + [u"numeric"]=> + int(1) + [u"blob"]=> + int(0) + [u"type"]=> + unicode(3) "int" + [u"unsigned"]=> + int(0) + [u"zerofill"]=> + int(0) +} +bool(true) +object(stdClass)#%d (13) { + [u"name"]=> + unicode(2) "id" + [u"table"]=> + unicode(4) "test" + [u"def"]=> + unicode(0) "" + [u"max_length"]=> + int(1) + [u"not_null"]=> + int(1) + [u"primary_key"]=> + int(1) + [u"multiple_key"]=> + int(0) + [u"unique_key"]=> + int(0) + [u"numeric"]=> + int(1) + [u"blob"]=> + int(0) + [u"type"]=> + unicode(3) "int" + [u"unsigned"]=> + int(0) + [u"zerofill"]=> + int(0) +} +bool(true) +object(stdClass)#%d (13) { + [u"name"]=> + unicode(5) "label" + [u"table"]=> + unicode(4) "test" + [u"def"]=> + unicode(0) "" + [u"max_length"]=> + int(1) + [u"not_null"]=> + int(0) + [u"primary_key"]=> + int(0) + [u"multiple_key"]=> + int(0) + [u"unique_key"]=> + int(0) + [u"numeric"]=> + int(0) + [u"blob"]=> + int(0) + [u"type"]=> + unicode(6) "string" + [u"unsigned"]=> + int(0) + [u"zerofill"]=> + int(0) +} + +Warning: mysql_field_seek(): Field %d is invalid for MySQL result index %d in %s on line %d +bool(false) +bool(false) + +Warning: mysql_field_seek(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done! diff --git a/ext/mysql/tests/mysql_field_table.phpt b/ext/mysql/tests/mysql_field_table.phpt new file mode 100644 index 0000000000..e82ac13abc --- /dev/null +++ b/ext/mysql/tests/mysql_field_table.phpt @@ -0,0 +1,65 @@ +--TEST-- +mysql_field_table() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!is_null($tmp = @mysql_field_table())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (null !== ($tmp = @mysql_field_table($link))) + printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 2", $link)) { + printf("[003] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +if (NULL !== ($tmp = mysql_field_table($res))) + printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = mysql_field_table($res, -1))) + printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +var_dump(mysql_field_table($res, 0)); + +if (false !== ($tmp = mysql_field_table($res, 2))) + printf("[008] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +mysql_free_result($res); + +var_dump(mysql_field_table($res, 0)); + +mysql_close($link); +print "done!"; +?> +--EXPECTF-- +Warning: Wrong parameter count for mysql_field_table() in %s on line %d + +Warning: mysql_field_table(): Field -1 is invalid for MySQL result index %d in %s on line %d +string(4) "test" + +Warning: mysql_field_table(): Field 2 is invalid for MySQL result index %d in %s on line %d + +Warning: mysql_field_table(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done! +--UEXPECTF-- +Warning: Wrong parameter count for mysql_field_table() in %s on line %d + +Warning: mysql_field_table(): Field -1 is invalid for MySQL result index %d in %s on line %d +unicode(4) "test" + +Warning: mysql_field_table(): Field 2 is invalid for MySQL result index %d in %s on line %d + +Warning: mysql_field_table(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_field_type.phpt b/ext/mysql/tests/mysql_field_type.phpt new file mode 100644 index 0000000000..61d8425fe7 --- /dev/null +++ b/ext/mysql/tests/mysql_field_type.phpt @@ -0,0 +1,65 @@ +--TEST-- +mysql_field_type() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!is_null($tmp = @mysql_field_type())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (null !== ($tmp = @mysql_field_type($link))) + printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 2", $link)) { + printf("[003] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +if (NULL !== ($tmp = mysql_field_type($res))) + printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = mysql_field_type($res, -1))) + printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +var_dump(mysql_field_type($res, 0)); + +if (false !== ($tmp = mysql_field_type($res, 2))) + printf("[008] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +mysql_free_result($res); + +var_dump(mysql_field_type($res, 0)); + +mysql_close($link); +print "done!"; +?> +--EXPECTF-- +Warning: Wrong parameter count for mysql_field_type() in %s on line %d + +Warning: mysql_field_type(): Field -1 is invalid for MySQL result index %d in %s on line %d +string(3) "int" + +Warning: mysql_field_type(): Field 2 is invalid for MySQL result index %d in %s on line %d + +Warning: mysql_field_type(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done! +--UEXPECTF-- +Warning: Wrong parameter count for mysql_field_type() in %s on line %d + +Warning: mysql_field_type(): Field -1 is invalid for MySQL result index %d in %s on line %d +unicode(3) "int" + +Warning: mysql_field_type(): Field 2 is invalid for MySQL result index %d in %s on line %d + +Warning: mysql_field_type(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done!
\ No newline at end of file diff --git a/ext/mysql/tests/mysql_free_result.phpt b/ext/mysql/tests/mysql_free_result.phpt new file mode 100644 index 0000000000..95e5656a4f --- /dev/null +++ b/ext/mysql/tests/mysql_free_result.phpt @@ -0,0 +1,47 @@ +--TEST-- +mysql_free_result() +--SKIPIF-- +<?php +require_once('skipif.inc'); +require_once('skipifconnectfailure.inc'); +?> +--FILE-- +<?php +include "connect.inc"; + +$tmp = NULL; +$link = NULL; + +if (!is_null($tmp = @mysql_free_result())) + printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); + +if (false !== ($tmp = @mysql_free_result($link))) + printf("[002] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); + +require('table.inc'); +if (!$res = mysql_query("SELECT id FROM test ORDER BY id LIMIT 1", $link)) { + printf("[003] [%d] %s\n", mysql_errno($link), mysql_error($link)); +} + +var_dump(mysql_free_result($res)); +var_dump(mysql_free_result($res)); + +if ($tmp = sys_get_temp_dir()) { + $tmpname = tempnam($tmp, 'free_result'); + if ($fp = fopen($tmpname, 'w')) { + if (false !== ($tmp = @mysql_free_result($fp))) + printf("[004] Expecting boolean/false got %s/%s\n", gettype($tmp), $tmp); + fclose($fp); + @unlink($tmpname); + } +} + +mysql_close($link); +print "done!"; +?> +--EXPECTF-- +bool(true) + +Warning: mysql_free_result(): %d is not a valid MySQL result resource in %s on line %d +bool(false) +done!
\ No newline at end of file |