在代码中同时使用两个数据库

Iva*_*van 11 php codeception

如何在代码中同时使用两个数据库?我的PHP应用程序使用SQLite数据库,但也连接到另一个使用MySQL数据库的应用程序.

目前我在我的codeception.yml文件中有这个:

modules:
  config:
    Db:
        dsn: 'sqlite:db.sqlite'
        dump: tests/_data/dump.sql
        populate: true
        cleanup: true
Run Code Online (Sandbox Code Playgroud)

这样,数据库每次都会填充测试数据,并在测试结束时自动清理.现在如何添加一个MySQL数据库呢?

此外,如果可能,在某些测试中我使用"seeInDatabase"函数.我如何指定它必须看哪个数据库?

小智 1

实际上,我们可以通过使用 Codeception 扩展类相当轻松地完成此任务。我们将使用扩展进行如下操作:

使用我们的扩展挂钩“测试前”和“测试后”事件重新配置 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,
);
}
Run Code Online (Sandbox Code Playgroud)

如果我的扩展设置为仅在给定测试正确注释时才触发,即:

// in a Cest
/**
* @group api
*/
public function hereIsSomeTest(WebGuy $I)
{
...
}

// in a Cept
$scenario->group('api');
$I = new WebGuy($scenario);
Run Code Online (Sandbox Code Playgroud)

我将扩展设置为遵循“api”注释,因此我不必在每个测试中设置和拆除 API 数据库,只需处理数据的数据库即可。但是,您可以很容易地进行修改以满足您的需求。