集成测试PHPUnit和Phinx

dwe*_*ees 7 php phpunit database-migration

我正在创建一个PHP REST api,使用PHPUnit进行单元测试和集成测试.我希望将phinx集成到数据库迁移中(而不是自己构建迁移代码).

我实际上有两个问题:

  • 我如何使用Phinx进行数据库设置?Phinx通常用作命令行工具,但我需要一些方法来调用我的单元测试类中的setup方法.

  • 我将如何进行集成测试我编写的迁移类?我想要一些验证,在每次迁移步骤后,我的数据库处于某种预期状态(可能包括在每次迁移期间应该保持一致的一些样本数据)

Jed*_*nch 4

这是一个解决方案。

<?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");
}

?>
Run Code Online (Sandbox Code Playgroud)

简短而甜蜜。