When performing database-related unit tests, we sometimes need to have a deeper understanding of what exactly is happening, especially when the test fails and involves complex SQL operations. PHP's mysqli extension provides a very practical debugging function mysqli::debug() , which can output the underlying operation logs to a file. This article will introduce how to combine mysqli::debug() to implement the debugging function of database operations when using PHPUnit for testing.
mysqli::debug(string $debug_options) is a method in the mysqli class that can be used to enable client debug logs. Its parameters are a string that describes the configuration of debug information, such as the output file path and log level. It should be noted that it does not automatically enable logging, and it also requires support for debugging in the compilation options of the MySQL server.
mysqli::debug("d:t:o,/tmp/client.trace");
in:
d stands for debugging
t means containing thread information
o means output to file
/tmp/client.trace is the path to the log file
When you use PHPUnit for database-related tests, you can enable debugging in the setUp() or setUpBeforeClass() method of the test. In this way, debug information will be recorded every time the test is run to help you track database operations.
Here is an actual example:
use PHPUnit\Framework\TestCase;
class DatabaseTest extends TestCase
{
private static $mysqli;
public static function setUpBeforeClass(): void
{
// Enable debug output
mysqli::debug("d:t:o,/tmp/php-mysqli-debug.log");
self::$mysqli = new mysqli("localhost", "user", "password", "testdb");
if (self::$mysqli->connect_error) {
die("Connection failed: " . self::$mysqli->connect_error);
}
}
public function testInsertData()
{
$stmt = self::$mysqli->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $email);
$name = "Test the user";
$email = "test@m66.net";
$this->assertTrue($stmt->execute());
}
public static function tearDownAfterClass(): void
{
self::$mysqli->close();
}
}
Log file permissions : Ensure that PHP has permission to write to the specified log file path, such as /tmp/php-mysqli-debug.log .
Use caution in production environment : mysqli::debug() is mainly used in the development and debugging stages. It is not recommended to enable it in the production environment to avoid leakage of sensitive information.
There are many contents of debugging files : debugging files may become large quickly, especially when running a large number of database tests, please plan the file path reasonably and clean the logs regularly.
After opening the /tmp/php-mysqli-debug.log file, you will see detailed debugging information, including:
Establishment and closing of each connection
Query statement execution status
Parameter binding and execution process
Thread information, etc.
This information can help you have a clearer understanding of how your PHP program communicates with MySQL, especially when locating problems when tests fail.
Using mysqli::debug() with PHPUnit is a very practical debugging method, especially when developing database drivers, ORMs, or performing integration testing. If you are often doing database-related tests and debugging, you might as well try this method, it can help you save a lot of time in troubleshooting problems.
To learn more about the usage of mysqli::debug() , please refer to the official PHP document: https://www.php.net/manual/zh/mysqli.debug.php