在PHPUnit测试中从SQL文件加载模式

时间:2021-10-11 13:55:57

I'm trying to build a test case that uses the database extension of PHPUnit. I've followed the official documentation and this post of Mike Lively, but PHPUnit keeps complaining:

我正在尝试构建一个使用PHPUnit的数据库扩展的测试用例。我已经按照官方文档和Mike Lively的这篇文章,但PHPUnit一直在抱怨:

1) ClearanceProfileTest::testFetchPrivileges
PHPUnit_Extensions_Database_Operation_Exception: COMPOSITE[TRUNCATE] operation failed on query:
            DELETE FROM "profile_privilege_mappings"
         using args: Array
(
)
[SQLSTATE[HY000]: General error: 1 no such table: profile_privilege_mappings]

I believe that the error is caused because my database schema wasn't set, but I can't find a way to load it. I tried loading my .sql file contents, and executing it as PDO query, but it just did nothing.

我认为错误是由于我的数据库架构没有设置引起的,但我找不到加载它的方法。我尝试加载我的.sql文件内容,并将其作为PDO查询执行,但它什么也没做。

Here is my testcase code:

这是我的测试用例代码:

class ClearanceProfileTest extends PHPUnit_Extensions_Database_TestCase
{
  /**
   * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
   */
  public function getConnection() {
    $pdo = new PDO('sqlite::memory:');
    return $this->createDefaultDBConnection($pdo, ':memory:');
  }

  /**
   * @return PHPUnit_Extensions_Database_DataSet_IDataSet
   */
  protected function getDataSet() {
    $yaml = dirname(__FILE__) . "/ClearanceProfileTestDataset.yml";
    $dataset = new PHPUnit_Extensions_Database_DataSet_YamlDataSet($yaml);
    return $dataset;
  }
  public function testFetchPrivileges()  {
    $cp = ClearanceProfile::retrieve_one('id = ?', 1);
    $privileges = $cp->fetchPrivileges();
    $this->assertEquals(count($privileges), 1);
  }
}

1 个解决方案

#1


8  

In a default implementation the database extension will perform a ‘clean insert’ operation on your database using the the dataset returned by getDataSet. The clean insert operation will truncate any tables in the data set (not all tables in the database) and then insert the rows in the dataset. The solution is to create the table before running the test.

在默认实现中,数据库扩展将使用getDataSet返回的数据集对数据库执行“清理插入”操作。干净插入操作将截断数据集中的所有表(不是数据库中的所有表),然后在数据集中插入行。解决方案是在运行测试之前创建表。

Additionally you can modify the default functionality by overriding the getSetUpOperation() and getTearDownOperation() methods

此外,您可以通过重写getSetUpOperation()和getTearDownOperation()方法来修改默认功能

#1


8  

In a default implementation the database extension will perform a ‘clean insert’ operation on your database using the the dataset returned by getDataSet. The clean insert operation will truncate any tables in the data set (not all tables in the database) and then insert the rows in the dataset. The solution is to create the table before running the test.

在默认实现中,数据库扩展将使用getDataSet返回的数据集对数据库执行“清理插入”操作。干净插入操作将截断数据集中的所有表(不是数据库中的所有表),然后在数据集中插入行。解决方案是在运行测试之前创建表。

Additionally you can modify the default functionality by overriding the getSetUpOperation() and getTearDownOperation() methods

此外,您可以通过重写getSetUpOperation()和getTearDownOperation()方法来修改默认功能