Phergie_Plugin_TestCase::getMockDatabase PHP Method

getMockDatabase() public method

Creates an in-memory copy of a specified SQLite database file and returns a connection to it.
public getMockDatabase ( string $path ) : PDO
$path string Path to the SQLite file to copy
return PDO Connection to the database copy
    public function getMockDatabase($path)
    {
        $original = new PDO('sqlite:' . $path);
        $copy = new PDO('sqlite::memory:');
        $result = $original->query('SELECT sql FROM sqlite_master');
        while ($sql = $result->fetchColumn()) {
            $copy->exec($sql);
        }
        $tables = array();
        $result = $original->query('SELECT name FROM sqlite_master WHERE type = "table"');
        while ($table = $result->fetchColumn()) {
            $tables[] = $table;
        }
        foreach ($tables as $table) {
            $result = $original->query('SELECT * FROM ' . $table);
            $insert = null;
            $copy->beginTransaction();
            while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
                $columns = array_keys($row);
                if (empty($insert)) {
                    $insert = $copy->prepare('INSERT INTO "' . $table . '" (' . '"' . implode('", "', $columns) . '"' . ') VALUES (' . ':' . implode(', :', $columns) . ')');
                }
                $insert->execute($row);
            }
            $copy->commit();
            unset($insert);
        }
        return $copy;
    }