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