How can a SQLite database be used for testing in PHP projects?

When testing PHP projects, it is common to use a SQLite in-memory database to create a lightweight and isolated environment for testing database interactions. This allows for faster test execution and ensures that the tests do not affect the actual database used in production. By utilizing SQLite for testing, developers can easily set up, populate, and tear down the database as needed for each test case.

// Create a SQLite in-memory database connection for testing
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Create a table for testing
$pdo->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");

// Insert test data into the table
$pdo->exec("INSERT INTO users (name) VALUES ('John Doe')");
$pdo->exec("INSERT INTO users (name) VALUES ('Jane Smith')");

// Perform testing operations using the SQLite database connection
// ...

// Clean up by dropping the table and closing the database connection
$pdo->exec("DROP TABLE users");
$pdo = null;