Cakephp 3测试:如何从数据库加载记录?

问题描述 投票:2回答:2

我需要测试一些报告,有一个复杂的数据结构和大量的数据影响这些报告,因此为此目的创建固定装置将是非常繁琐的工作。我想使用现有的数据库(实际上是它的副本)来测试报告。

我怎样才能在cakephp 3测试中实现这一目标?是否可以从cakephp 3中的数据库加载夹具(不是表格结构)的记录?

php cakephp testing cakephp-3.0
2个回答
4
投票

您可以使用Bake实用程序从表中的现有记录创建夹具:

--records, -r     Generate a fixture with records from the non-test
                  database. Used with --count and --conditions to limit
                  which records are added to the fixture.
--count, -n       When using generated data, the number of records to
                  include in the fixture(s). (default:
                  1)

例如:

cake bake fixture you_table_name -r -c 100


0
投票

由于PHPUnit从每个测试之间有权访问的数据库中删除所有内容,因此您可能不希望授予它对生产数据库的访问权限。

您正在寻找的解决方案可能是编写每个fixture的init()函数,以根据从生产数据库中提取的数据来构建其$records属性。

像这样(未经证实):

public function init()
{
    $thingsTable = TableRegistry::get('Things');
    $this->records = $thingsTable->find()->toArray();
    parent::init();
}

请注意,这确实会减慢您的测试速度,但缓存结果会减轻这种影响。

© www.soinside.com 2019 - 2024. All rights reserved.