PHPUnit 4.8模拟mysql数据库接口(interface)

标签 php mysql unit-testing phpunit

下午好

我有一堆遗留代码使用旧的 mysql 库(例如 mysql_query($sql)),我正在尝试用 PHPUnit 测试它(4.8 是由于各种原因将在服务器上运行的最新版本)。

有谁知道如何模拟这个数据库连接,以便它可以在运行预定查询时返回预定结果?我尝试使用 getConnection()(根据此处的文档:http://devdocs.io/phpunit~4/database)无济于事。

例如我有这个类:

class Raffle {
    ...
    public static function loadAll($filter=""){
        //$filter = mysql_real_escape_string($filter); // protect against SQL injection
        $raffles = []; // the array to return

        $sql = "SELECT * FROM raffles $filter;"; // include the user filter in the query
        $query = mysql_query($sql);

        //echo mysql_error();

        while($row = mysql_fetch_assoc($query)){
            $raffles[] = Raffle::loadFromRow($row); // generate the raffe instance and add it to the array
        }
        return $raffles; // return the array
    }
    ...
}

(mysql_connect() 调用是在名为 db.php 的文件中完成的,该文件加载到需要它的每个页面上,而不是在类文件本身中。 )

提前致谢。

最佳答案

对于陷入这种情况的任何其他人,我发现为 PDO 构建一个模拟,其中包含对功能 api 的回退,允许我将代码迁移到基于 Testable OO 的模型,同时仍然使用旧的 mysql 引擎盖下的图书馆。

例如:

// the basic interfaces (function names taken from PDO
interface DBConnAdapter {
    public function query($sql);
}
// since PDO splits connections and statements the adapter should do the same
interface DBQueryAdapter {
    public function num_rows();
    public function fetch_assoc();
}
...
class DBAdapter implements DBConnAdapter {

    public function query($sql){
        $query = mysql_query($sql); // run the query using the legacy api
        return $query ? new QueryAdapter($query) : false; // return a new query adapter or false.
    }

}
...
// an example of a basic mock object to test sql queries being sent to the server (inspired by mockjax :-) ) 
class DBMock implements DBConnAdapter {

    public $queries = []; // array of queries already executed
    public $results = []; // array of precomputed results. (key is SQL and value is the returned result (nested array))

    public function query($sql) {
        if($this->results[$sql]){
            $query = new DBQueryMock($sql, $this->results[$sql]); // a mock of PDOStatement that takes the sql it ran and the results to return
            $queries[] = $query; // add the query to the array
            return $query; // return the query
        }
        return false; // we do not know the statement so lets pretend it failed
    }
    // add a result to the list
    public function add_single_result($sql, $result){
        // check if the index was set, if not make an array there
        if(!isset($this->results[$sql])) $this->results[$sql] = [];
        // add the result to the end of the array
        $this->results[$sql][] = $result; 
        // return its index
        return count($this->results[$sql]) - 1;
    }
}

不可否认,这不是一个理想的解决方案,因为它需要修改代码以支持适配器对象并删除一些功能(例如 mysql_real_escape_string),但它确实有效......

如果您有更好的解决方案,请分享 :-) 谢谢!

关于PHPUnit 4.8模拟mysql数据库接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47164949/

相关文章:

php - 在 PHP 中使用 PDO 的正确方法是什么?

php - 如何将所有这些 MySQL 查询合并为一个?

mysql - MySQL 如何解释 VARCHAR 字段大小?

c# - TFS 构建测试结果

java - JMockit - 期望与 MockUp<T> 为什么一个有效而另一个无效?

java - Mockito 时和验证方法的区别

php - Jquery注入(inject)html不起作用。 ajax调用不稳定

javascript - 如何在 javascript 中使用 PHP 数组或 json 响应?

mysql - 查找到此时为止的所有记录 - MySQL

php - 如何处理 Facebook 的新 UID 大小?