Annotation of embedaddon/php/ext/mysqli/tests/mysqli_report.phpt, revision 1.1

1.1     ! misho       1: --TEST--
        !             2: mysqli_report()
        !             3: --SKIPIF--
        !             4: <?php
        !             5: require_once('skipif.inc');
        !             6: require_once('skipifemb.inc');
        !             7: require_once('skipifconnectfailure.inc');
        !             8: ?>
        !             9: --FILE--
        !            10: <?php
        !            11:        require_once("connect.inc");
        !            12: 
        !            13:        $tmp    = NULL;
        !            14:        $link   = NULL;
        !            15: 
        !            16:        if (NULL !== ($tmp = @mysqli_report()))
        !            17:                printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
        !            18: 
        !            19:        if (true !== ($tmp = mysqli_report(-1)))
        !            20:                printf("[002] Expecting boolean/true even for invalid flags, got %s/%s\n", gettype($tmp), $tmp);
        !            21: 
        !            22:        if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_ERROR)))
        !            23:                printf("[003] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
        !            24: 
        !            25:        if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_STRICT)))
        !            26:                printf("[004] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
        !            27: 
        !            28:        if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_INDEX)))
        !            29:                printf("[005] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
        !            30: 
        !            31:        if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_ALL)))
        !            32:                printf("[007] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
        !            33: 
        !            34:        if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_OFF)))
        !            35:                printf("[008] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
        !            36: 
        !            37:        require('table.inc');
        !            38: 
        !            39:        /*
        !            40:        Internal macro MYSQL_REPORT_ERROR
        !            41:        */
        !            42:        mysqli_report(MYSQLI_REPORT_ERROR);
        !            43: 
        !            44:        mysqli_multi_query($link, "BAR; FOO;");
        !            45:        mysqli_query($link, "FOO");
        !            46:        /* This might work if you accept anonymous users in your setup */
        !            47:        mysqli_change_user($link, "0123456789-10-456789-20-456789-30-456789-40-456789-50-456789-60-456789-70-456789-80-456789-90-456789", "password", $db);
        !            48:        mysqli_kill($link, -1);
        !            49: 
        !            50:        // mysqli_ping() cannot be tested, because one would need to cause an error inside the C function to test it
        !            51:        mysqli_prepare($link, "FOO");
        !            52:        mysqli_real_query($link, "FOO");
        !            53:        if (@mysqli_select_db($link, "Oh lord, let this be an unknown database name"))
        !            54:                printf("[009] select_db should have failed\n");
        !            55:        // mysqli_store_result() and mysqli_use_result() cannot be tested, because one would need to cause an error inside the C function to test it
        !            56: 
        !            57: 
        !            58:        // Check that none of the above would have caused any error messages if MYSQL_REPORT_ERROR would
        !            59:        // not have been set. If that would be the case, the test would be broken.
        !            60:        mysqli_report(MYSQLI_REPORT_OFF);
        !            61: 
        !            62:        mysqli_multi_query($link, "BAR; FOO;");
        !            63:        mysqli_query($link, "FOO");
        !            64:        mysqli_change_user($link, "This might work if you accept anonymous users in your setup", "password", $db);
        !            65:        mysqli_kill($link, -1);
        !            66:        mysqli_prepare($link, "FOO");
        !            67:        mysqli_real_query($link, "FOO");
        !            68:        mysqli_select_db($link, "Oh lord, let this be an unknown database name");
        !            69: 
        !            70:        /*
        !            71:        Internal macro MYSQL_REPORT_STMT_ERROR
        !            72:        */
        !            73: 
        !            74:        mysqli_report(MYSQLI_REPORT_ERROR);
        !            75: 
        !            76:        $stmt = mysqli_stmt_init($link);
        !            77:        mysqli_stmt_prepare($stmt, "FOO");
        !            78: 
        !            79:        $stmt = mysqli_stmt_init($link);
        !            80:        mysqli_stmt_prepare($stmt, "SELECT id FROM test WHERE id > ?");
        !            81:        $id = 1;
        !            82:        mysqli_kill($link, mysqli_thread_id($link));
        !            83:        mysqli_stmt_bind_param($stmt, "i", $id);
        !            84:        mysqli_stmt_close($stmt);
        !            85:        mysqli_close($link);
        !            86: 
        !            87:        /* mysqli_stmt_execute() = mysql_stmt_execute cannot be tested from PHP */
        !            88:        if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
        !            89:                        printf("[008] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
        !            90:        $stmt = mysqli_stmt_init($link);
        !            91:        mysqli_stmt_prepare($stmt, "SELECT id FROM test WHERE id > ?");
        !            92:        $id = 1;
        !            93:        mysqli_stmt_bind_param($stmt, "i", $id);
        !            94:        // mysqli_kill($link, mysqli_thread_id($link));
        !            95:        mysqli_stmt_execute($stmt);
        !            96:        mysqli_stmt_close($stmt);
        !            97:        mysqli_close($link);
        !            98: 
        !            99:        /* mysqli_kill() "trick" does not work for any of the following because of an E_COMMANDS_OUT_OF_SYNC */
        !           100:        /* mysqli_stmt_bind_result() = mysql_stmt_bind_result() cannot be tested from PHP */
        !           101:        /* mysqli_stmt_fetch() = mysql_stmt_fetch() cannot be tested from PHP */
        !           102:        /* mysqli_stmt_result_metadata() = mysql_stmt_result_metadata() cannot be tested from PHP */
        !           103:        /* mysqli_stmt_store_result() = mysql_stmt_store_result() cannot be tested from PHP */
        !           104: 
        !           105:        // Check
        !           106:        mysqli_report(MYSQLI_REPORT_OFF);
        !           107: 
        !           108:        if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
        !           109:                printf("[010] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
        !           110:        $stmt = mysqli_stmt_init($link);
        !           111:        mysqli_stmt_prepare($stmt, "FOO");
        !           112: 
        !           113:        $stmt = mysqli_stmt_init($link);
        !           114:        mysqli_stmt_prepare($stmt, "SELECT id FROM test WHERE id > ?");
        !           115:        $id = 1;
        !           116:        mysqli_kill($link, mysqli_thread_id($link));
        !           117:        mysqli_stmt_bind_param($stmt, "i", $id);
        !           118:        mysqli_stmt_close($stmt);
        !           119:        mysqli_close($link);
        !           120: 
        !           121:        if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
        !           122:                printf("[011] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
        !           123:        $stmt = mysqli_stmt_init($link);
        !           124:        mysqli_stmt_prepare($stmt, "SELECT id FROM test WHERE id > ?");
        !           125:        $id = 1;
        !           126:        mysqli_stmt_bind_param($stmt, "i", $id);
        !           127:        mysqli_kill($link, mysqli_thread_id($link));
        !           128:        mysqli_stmt_execute($stmt);
        !           129:        mysqli_stmt_close($stmt);
        !           130:        mysqli_close($link);
        !           131: 
        !           132:        /*
        !           133:        MYSQLI_REPORT_STRICT
        !           134: 
        !           135:        MYSQLI_REPORT_STRICT --->
        !           136:        php_mysqli_report_error() ->
        !           137:                MYSQLI_REPORT_MYSQL_ERROR,
        !           138:                MYSQLI_REPORT_STMT_ERROR ->
        !           139:                        already tested
        !           140: 
        !           141:        php_mysqli_throw_sql_exception() ->
        !           142:                my_mysqli_real_connect()
        !           143:                my_mysqli_connect()
        !           144: 
        !           145:        can't be tested: mysqli_query() via mysql_use_result()/mysql_store_result()
        !           146:        */
        !           147:        mysqli_report(MYSQLI_REPORT_OFF);
        !           148:        mysqli_report(MYSQLI_REPORT_STRICT);
        !           149: 
        !           150:        try {
        !           151: 
        !           152:                if ($link = my_mysqli_connect($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket))
        !           153:                        printf("[012] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
        !           154:                                $host, $user . 'unknown_really', $db, $port, $socket);
        !           155:                mysqli_close($link);
        !           156: 
        !           157:        } catch (mysqli_sql_exception $e) {
        !           158:                printf("[013] %s\n", $e->getMessage());
        !           159:        }
        !           160: 
        !           161:        try {
        !           162:                if (!$link = mysqli_init())
        !           163:                        printf("[014] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
        !           164: 
        !           165:                if ($link = my_mysqli_real_connect($link, $host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket))
        !           166:                        printf("[015] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
        !           167:                                $host, $user . 'unknown_really', $db, $port, $socket);
        !           168:                mysqli_close($link);
        !           169:        } catch (mysqli_sql_exception $e) {
        !           170:                printf("[016] %s\n", $e->getMessage());
        !           171:        }
        !           172: 
        !           173:        /*
        !           174:        MYSQLI_REPORT_INDEX --->
        !           175:        mysqli_query()
        !           176:        mysqli_stmt_execute()
        !           177:        mysqli_prepare()
        !           178:        mysqli_real_query()
        !           179:        mysqli_store_result()
        !           180:        mysqli_use_result()
        !           181: 
        !           182:        No test, because of to many prerequisites:
        !           183:                - Server needs to be started with and
        !           184:                        --log-slow-queries --log-queries-not-using-indexes
        !           185:                - query must cause the warning on all MySQL versions
        !           186: 
        !           187:        TODO:
        !           188:        */
        !           189:        $log_slow_queries = false;
        !           190:        $log_queries_not_using_indexes = false;
        !           191:        mysqli_report(MYSQLI_REPORT_OFF);
        !           192:        mysqli_report(MYSQLI_REPORT_INDEX);
        !           193: 
        !           194:        if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
        !           195:                printf("[017] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
        !           196: 
        !           197:        // this might cause a warning - no index used
        !           198:        if (!$res = @mysqli_query($link, "SHOW VARIABLES LIKE 'log_slow_queries'"))
        !           199:                printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           200: 
        !           201:        if (!$row = mysqli_fetch_assoc($res))
        !           202:                printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           203: 
        !           204:        $log_slow_query = ('ON' == $row['Value']);
        !           205: 
        !           206:        if (mysqli_get_server_version($link) >= 51011) {
        !           207:                // this might cause a warning - no index used
        !           208:                if (!$res = @mysqli_query($link, "SHOW VARIABLES LIKE 'log_queries_not_using_indexes'"))
        !           209:                        printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           210: 
        !           211:                if (!$row = mysqli_fetch_assoc($res))
        !           212:                        printf("[021] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           213: 
        !           214:                $log_queries_not_using_indexes = ('ON' == $row['Value']);
        !           215: 
        !           216:                if ($log_slow_queries && $log_queries_not_using_indexes) {
        !           217: 
        !           218:                        for ($i = 100; $i < 20000; $i++) {
        !           219:                                if (!mysqli_query($link, "INSERT INTO test(id, label) VALUES ($i, 'z')"))
        !           220:                                        printf("[022 - %d] [%d] %s\n", $i - 99, mysqli_errno($link), mysqli_error($link));
        !           221:                        }
        !           222: 
        !           223:                        // this might cause a warning - no index used
        !           224:                        if (!$res = @mysqli_query($link, "SELECT id, label FROM test WHERE id = 1323"))
        !           225:                                printf("[023] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           226: 
        !           227:                        mysqli_free_result($res);
        !           228:                }
        !           229:        }
        !           230: 
        !           231:        // Maybe we've provoked an index message, maybe not.
        !           232:        // All we can do is make a few dummy calls to ensure that all codes gets executed which
        !           233:        // checks the flag. Functions to check: mysqli_query() - done above,
        !           234:        // mysqli_stmt_execute(), mysqli_prepare(), mysqli_real_query(), mysqli_store_result()
        !           235:        // mysqli_use_result(), mysqli_thread_safe(), mysqli_thread_id()
        !           236:        mysqli_report(MYSQLI_REPORT_OFF);
        !           237:        mysqli_close($link);
        !           238:        if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
        !           239:                printf("[024] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
        !           240: 
        !           241:        if (!$stmt = mysqli_stmt_init($link))
        !           242:                printf("[025] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           243: 
        !           244:        if (!mysqli_stmt_prepare($stmt, 'SELECT id, label FROM test'))
        !           245:                printf("[026] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           246: 
        !           247:        if (!mysqli_stmt_execute($stmt))
        !           248:                printf("[027] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           249: 
        !           250:        mysqli_stmt_close($stmt);
        !           251: 
        !           252:        if (!mysqli_real_query($link, 'SELECT label, id FROM test'))
        !           253:                printf("[028] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           254: 
        !           255:        if (!$res = mysqli_use_result($link))
        !           256:                printf("[029] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           257: 
        !           258:        mysqli_free_result($res);
        !           259: 
        !           260:        if (!mysqli_real_query($link, 'SELECT label, id FROM test'))
        !           261:                printf("[030]  [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           262: 
        !           263:        if (!$res = mysqli_store_result($link))
        !           264:                printf("[031]  [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           265: 
        !           266:        mysqli_free_result($res);
        !           267: 
        !           268:        if (!$stmt = mysqli_prepare($link, 'SELECT id * 3 FROM test'))
        !           269:                printf("[032] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           270:        else
        !           271:                mysqli_stmt_close($stmt);
        !           272: 
        !           273:        if (!mysqli_query($link, "INSERT INTO test(id, label) VALUES (100, 'z')", MYSQLI_USE_RESULT) ||
        !           274:                        !mysqli_query($link, 'DELETE FROM test WHERE id > 50', MYSQLI_USE_RESULT))
        !           275:                printf("[033] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
        !           276: 
        !           277:        $tmp = mysqli_thread_safe($link);
        !           278:        $tmp = mysqli_thread_id($link);
        !           279: 
        !           280:        mysqli_close($link);
        !           281:        print "done!";
        !           282: ?>
        !           283: --CLEAN--
        !           284: <?php
        !           285:        require_once("clean_table.inc");
        !           286: ?>
        !           287: --EXPECTF--
        !           288: Warning: mysqli_multi_query(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BAR; FOO' at line 1 in %s on line %d
        !           289: 
        !           290: Warning: mysqli_query(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOO' at line 1 in %s on line %d
        !           291: 
        !           292: Warning: mysqli_change_user(): (%d/%d): Access denied for user '%s'@'%s' (using password: %s) in %s on line %d
        !           293: 
        !           294: Warning: mysqli_kill(): processid should have positive value in %s on line %d
        !           295: 
        !           296: Warning: mysqli_prepare(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOO' at line 1 in %s on line %d
        !           297: 
        !           298: Warning: mysqli_real_query(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOO' at line 1 in %s on line %d
        !           299: 
        !           300: Warning: mysqli_kill(): processid should have positive value in %s on line %d
        !           301: 
        !           302: Warning: mysqli_stmt_prepare(): (%d/%d): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOO' at line 1 in %s on line %d
        !           303: [013] Access denied for user '%s'@'%s' (using password: YES)
        !           304: [016] Access denied for user '%s'@'%s' (using password: YES)
        !           305: done!

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>