1--TEST--
2mysqli_debug() - append to trace file
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8
9if (!function_exists('mysqli_debug'))
10    die("skip: mysqli_debug() not available");
11
12if (!defined('MYSQLI_DEBUG_TRACE_ENABLED'))
13    die("skip: can't say for sure if mysqli_debug works");
14
15if (defined('MYSQLI_DEBUG_TRACE_ENABLED') && !MYSQLI_DEBUG_TRACE_ENABLED)
16    die("skip: debug functionality not enabled");
17
18if (!$IS_MYSQLND)
19    die("SKIP Libmysql feature not sufficiently spec'd in MySQL C API documentation");
20
21if (substr(PHP_OS, 0, 3) == 'WIN') die("skip this test is not for Windows platforms");
22?>
23--FILE--
24<?php
25    require_once('connect.inc');
26
27    if (true !== ($tmp = mysqli_debug(sprintf('d:t:O,%s/mysqli_debug_phpt.trace', sys_get_temp_dir()))))
28        printf("[001] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
29
30    // table.inc will create a database connection and run some SQL queries, therefore
31    // the debug file should have entries
32    require_once('table.inc');
33
34    clearstatcache();
35    $trace_file = sprintf('%s/mysqli_debug_phpt.trace', sys_get_temp_dir());
36    if (!file_exists($trace_file))
37        printf("[002] Trace file '%s' has not been created\n", $trace_file);
38    if (filesize($trace_file) < 50)
39        printf("[003] Trace file '%s' is very small. filesize() reports only %d bytes. Please check.\n",
40            $trace_file,
41            filesize($trace_file));
42
43    // will mysqli_debug() mind if the trace file gets removed?
44    unlink($trace_file);
45    clearstatcache();
46
47    if (!$fp = fopen($trace_file, 'w')) {
48        printf("[004] Cannot create trace file to test append mode\n");
49    } else {
50
51        if (!fwrite($fp, 'mysqli_debug.phpt test line'))
52            printf("[005] Cannot write to trace file.\n");
53        fclose($fp);
54
55        if (true !== ($tmp = mysqli_debug(sprintf('d:a,%s', $trace_file))))
56            printf("[006] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
57
58        if (!$res = mysqli_query($link, 'SELECT * FROM test'))
59            printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
60        else
61            mysqli_free_result($res);
62
63        $trace = file_get_contents($trace_file);
64        if (!strstr($trace, 'mysqli_debug.phpt test line'))
65            printf("[008] Cannot find original file content any more. Seems that the trace file got overwritten and not appended. Please check.");
66
67        if (true !== ($tmp = mysqli_debug(sprintf('d:A,%s', $trace_file))))
68            printf("[009] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
69
70        if (!$res = mysqli_query($link, 'SELECT * FROM test'))
71            printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
72        else
73            mysqli_free_result($res);
74
75        if (!strstr(file_get_contents($trace_file), $trace))
76            printf("[011] Cannot find original file content any more. Seems that the trace file got overwritten and not appended. Please check.");
77    }
78
79    // what will happen if we create new trace entries...?
80    unlink($trace_file);
81    clearstatcache();
82    if (file_exists($trace_file))
83        printf("[012] Could not remove trace file '%s'.\n", $trace_file);
84
85    mysqli_close($link);
86    print "done";
87    if ($IS_MYSQLND)
88        print "libmysql/DBUG package prints some debug info here."
89?>
90--CLEAN--
91<?php
92    require_once("clean_table.inc");
93?>
94--EXPECTF--
95done%s
96