use*_*982 5 php mysql codeception phinx
我在客户端和服务器端JavaScript应用程序开发方面有一些经验.但现在我在php上设计我的第一个Web应用程序并寻找最佳的开发工具堆栈.我使用phinx在测试,开发和生产环境之间共享我的数据库结构.我将使用codeception进行数据库操作测试.
问题是代码期望我将表创建sql命令放入tests/_data/dump.sql并删除我在phinx迁移文件中创建的所有表.我可以设置cleanup: false在codeception.yml,但我将不得不在这种情况下,每个测试之前清理数据库表.我不知道怎么做.在代码中的每次测试之前,我没有发现手动清理db的能力.
我如何获得代码和phinx协调?
PS:我发现有关在代码中使用迁移的讨论,似乎并不是每个人都能看到它的好处.
使用 Codeception,您可以为任何您想要的事情创建帮助程序,包括迁移加载。
这是一个在每次测试之前加载数据库迁移的帮助程序。我没有机会测试这段代码,但这里的主要思想应该很清楚。
代码接收助手:
namespace Codeception\Module;
use Codeception\Module;
use Codeception\TestInterface;
use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\NullOutput;
class FixtureHelper extends Module
{
/**
* Run database migrations before each test if database population enabled.
*
* @param TestInterface $test
*/
public function _before(TestInterface $test)
{
$populate = $this->getModule('Db')->_getConfig('populate');
if ($populate) {
$this->migrateDatabase();
}
}
/**
* Run the database migrations.
*/
public function migrateDatabase()
{
// Run Phinx console application.
$app = new PhinxApplication();
$app->setAutoExit(false);
$output = new NullOutput();
//$output = new ConsoleOutput();
// Run database migrations for test environment.
$input = new StringInput('migrate -e test');
$app->run($input, $output);
// ... you also can load the fixtures here
//$input = new StringInput('seed:run -s <my-seeds> -e test');
//$app->run($input, $output);
}
}
Run Code Online (Sandbox Code Playgroud)
Codeception配置(用于功能测试):
actor: FunctionalTester
modules:
enabled:
- ... your modules
- FunctionalHelper
- FixtureHelper
config:
Db:
dsn: '... dsn'
user: '%DB_USER%'
password: '%DB_PASSWORD%'
dump: 'tests/_data/dump.sql'
populate: true
cleanup: true
FixtureHelper:
depends: Db
Run Code Online (Sandbox Code Playgroud)
数据库转储(tests/_data/dump.sql):
-- Dump should not be empty because cleanup will not work.
-- So at least any silly sql query.
SELECT 1+2 AS veryComplicatedCalculations;
Run Code Online (Sandbox Code Playgroud)
Phinx 配置 ( phinx.yml) 必须位于与 Codeception 配置 ( codeception.yml) 相同的目录中,否则您必须确保PhinxApplication加载您的配置。
希望这有帮助!