'GET', 'HTTP_HOST' => 'localhost', 'SERVER_NAME' => 'localhost', 'SERVER_PORT' => '80', 'REQUEST_URI' => '/', 'SCRIPT_NAME' => '/index.php', 'PHP_SELF' => '/index.php', 'REMOTE_ADDR' => '127.0.0.1', 'HTTP_USER_AGENT' => 'PHPUnit/Test' ]; } // Initialize PDO for in-memory SQLite testing class TestDatabaseHelper { private static $pdo = null; public static function getTestPdo() { if (self::$pdo === null) { self::$pdo = new PDO('sqlite::memory:'); self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // Enable WAL mode for better testing performance self::$pdo->exec('PRAGMA journal_mode=WAL'); self::$pdo->exec('PRAGMA synchronous=NORMAL'); } return self::$pdo; } public static function setupTestSchema() { $pdo = self::getTestPdo(); // Create tables for testing $sql = file_get_contents(__DIR__ . '/../migrations/001_create_tables.sql'); $pdo->exec($sql); // Insert test data if needed self::insertTestData($pdo); } private static function insertTestData($pdo) { // Insert some test users $pdo->exec("INSERT INTO users (user_id, nickname, ip_address, session_id, last_seen) VALUES ('test_user_1', 'TestUser1', '192.168.1.100', 'session_123', datetime('now'))"); $pdo->exec("INSERT INTO users (user_id, nickname, ip_address, session_id, last_seen) VALUES ('test_user_2', 'TestUser2', '192.168.1.101', 'session_456', datetime('now'))"); // Insert test messages $pdo->exec("INSERT INTO chat_messages (user_id, nickname, message, is_admin, ip_address, time_formatted) VALUES ('test_user_1', 'TestUser1', 'Hello from test user 1', 0, '192.168.1.100', '12:00')"); $pdo->exec("INSERT INTO chat_messages (user_id, nickname, message, is_admin, ip_address, time_formatted) VALUES ('test_user_2', 'TestUser2', 'Hello from test user 2', 0, '192.168.1.101', '12:01')"); // Insert test active viewers $pdo->exec("INSERT INTO active_viewers (user_id, nickname, ip_address, session_id, is_admin, last_seen) VALUES ('test_user_1', 'TestUser1', '192.168.1.100', 'session_123', 0, datetime('now'))"); } public static function teardown() { self::$pdo = null; } } // Clean up any existing test artifacts function cleanupTestEnvironment() { // Clear test session data $_SESSION = []; // Remove any test files $testFiles = [ __DIR__ . '/../logs/app.log', __DIR__ . '/../active_viewers.json.backup', __DIR__ . '/../chat_messages.json.backup' ]; foreach ($testFiles as $file) { if (file_exists($file)) { @unlink($file); } } } // Set up test environment cleanupTestEnvironment(); // Register shutdown function to clean up register_shutdown_function(function() { TestDatabaseHelper::teardown(); cleanupTestEnvironment(); });