我正在创建一个PHP REST api,使用PHPUnit进行单元测试和集成测试。我正在寻求集成phinx进行数据库迁移(而不是自己构建迁移代码)。
我实际上有两个问题:
如何使用Phinx进行数据库设置?Phinx通常用作命令行工具,但是我需要某种方式从单元测试类的setup方法中调用。
我将如何集成测试我编写的Migration类?我想进行某种验证,以确保在每个迁移步骤之后我的数据库都处于预期状态(也许包括一些在每次迁移期间都应保持一致的示例数据)
这是一个解决方案。
<?php
use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
use Phinx\Wrapper\TextWrapper;
class ExampleTest extends TestCase
{
private static $T;
public function setUp(){
$app = new PhinxApplication();
$app->setAutoExit(false);
$app->run(new StringInput(' '), new NullOutput());
self::$T = new TextWrapper($app);
self::$T->getMigrate("testing");
}
public function tearDown(){
self::$T->getRollback("testing");
}
?>
简短而甜美。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句