同时使用两个数据库进行共同欺骗


Using two databases at the same time in codeception

如何同时使用两个数据库?我的PHP应用程序使用SQLite数据库,但也与另一个使用MySQL数据库的应用程序连接。

此刻我有这个在我的codeception。yml文件:

modules:
  config:
    Db:
        dsn: 'sqlite:db.sqlite'
        dump: tests/_data/dump.sql
        populate: true
        cleanup: true

这样,数据库每次都用测试数据填充,并在测试结束时自动清理自己。我怎么能添加一个MySQL数据库现在做同样的?

此外,在可能的情况下,在一些测试中,我使用"seeInDatabase"函数。我如何指定它要查看哪个数据库?

看看我贡献的一个模块,叫做Codeception MultiDb。

实际上,通过使用Codeception扩展类,我们可以相当容易地实现这一点。我们将对Extensions做如下处理:

在我们的扩展中钩入"before test"answers"after test"事件重新配置Db模块以指向我们的web服务,重新初始化模块并执行对每个web服务重复此步骤要开始,我们需要首先在验收测试中启用Db模块。按照说明设置Db模块。这里重要的是,我们将populate和cleanup设置为false,并且转储指向一个有效文件。我们将populate和cleanup设置为false,因为我们不希望在每次测试后填充和清理Db模块。好吧,我们有点这样做,但默认情况下,Db模块只与一个数据库通信,而我们需要多个数据库。

其次,按照创建基本Codeception扩展的说明进行操作。在设置好类、配置好并将其包含在引导程序中之后,可以使用以下代码作为指导:

class YourExtensionClass extends 'Codeception'Platform'Extension {
// events to listen on
static $events = array(
'test.before' => 'beforeTest',
'test.after' => 'afterTest',
);
function beforeTest('CodeCeption'Event'Test $e)
{
// get the test and groups
$test = $e->getTest();
$groups = $test->getScenario()->getGroups();
// only restore if annotated to do so
if (in_array('api', $groups)) {
// get the Db module
$db = $this->getModule('Db');
// re-initialize with web service one api config and execute
$webserviceOneConfig = $this->getWebServiceOneConfig($this->config);
$db->_reconfigure($webserviceOneConfig);
$db->_initialize();
$db->_before($test);
// re-initialize with web service two api config and execute
$webserviceTwoConfig = $this->getWebServiceTwoConfig($this->config);
$db->_reconfigure($webserviceTwoConfig);
$db->_initialize();
$db->_before($test);
}
}
function afterTest('CodeCeption'Event'Test $e)
{
// get the test and groups
$test = $e->getTest();
$groups = $test->getScenario()->getGroups();
// only restore if annotated to do so
if (in_array('api', $groups)) {
// get the Db module
$db = $this->getModule('Db');
// re-initialize with web service one api config and execute
$webserviceOneConfig = $this->getWebServiceOneConfig($this->config);
$db->_reconfigure($webserviceOneConfig);
$db->_initialize();
$db->_after($test);
// re-initialize with web service two api config and execute
$webserviceTwoConfig = $this->getWebServiceTwoConfig($this->config);
$db->_reconfigure($webserviceTwoConfig);
$db->_initialize();
$db->_after($test);
}
}
private function getWebServiceOneConfig($config)
{
return array(
'dsn' => 'your first webservice db dsn',
'dump' => '/path/to/your/first/dump/file',
'populate' => true,
'cleanup' => true,
);
}
private function getWebServiceTwoConfig($config)
{
return array(
'dsn' => 'your second webservice db dsn',
'dump' => '/path/to/your/second/dump/file',
'populate' => true,
'cleanup' => true,
);
}

如果有我的扩展设置,只有在给定的测试被正确注释时才会触发,这是:

// in a Cest
/**
* @group api
*/
public function hereIsSomeTest(WebGuy $I)
{
...
}
// in a Cept
$scenario->group('api');
$I = new WebGuy($scenario);

我设置了扩展以坚持"api"注释,因此我不必在每个测试上设置和拆除我的api数据库,只有那些处理数据的数据库。但是,您可以很容易地修改以满足您的需要。