【问题标题】:How to test across multiple mysql schemas with PHPUnit?如何使用 PHPUnit 跨多个 mysql 模式进行测试?
【发布时间】:2011-08-18 18:53:26
【问题描述】:

我想测试一个使用 PHPUnit 的 PHPUnit_Extensions_Database_TestCase 类跨多个模式进行查询的过程。我已经翻阅了文档、SO 和源代码,但似乎我必须使用以下内容设置我的数据库:

protected function getConnection()
{
    $this->pdo = new PDO('mysql:host=localhost;dbname=unit_test_schema', 'xxxx', 'yyyy');
    return $this->createDefaultDBConnection($this->pdo, 'unit_test_schema');    
}

protected function getDataSet()
{
    return $this->createXMLDataSet(DB_SETUP_DIR.'/schema.xml');
}

有没有办法以某种方式使用多个架构,以便我可以测试如下查询:

SELECT *
FROM   schema1.tableA
JOIN   schema2.tableB
USING  (id)

编辑: 明确地说,我要解决的问题是我只能弄清楚如何将架构设置文件传递到一个数据库。我想找到一种方法来表示“在 schema1 中创建 tables1.xml,在 schema2 中创建 tables2.xml”。在不同的 xml 文件中引用的表将在每次测试中被填充和终止。

【问题讨论】:

    标签: php mysql unit-testing phpunit


    【解决方案1】:

    我所做的一件事是为每个模式创建一个定义,然后在单元测试时覆盖该定义

    define('schema1', 'schema1_prod');
    define('schema2', 'schema2_prod');
    

    然后进行单元测试

    define('schema1', 'unit_tests');
    define('schema2', 'unit_tests');
    

    如果您在多个架构中有类似的表名,这仍然会中断,但如果您不这样做,它应该会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-01-28
      • 2011-10-10
      • 2016-06-04
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 2019-07-12
      相关资源
      最近更新 更多