gpt4 book ai didi

php - 集成测试 PHPUnit 和 Phinx

转载 作者:可可西里 更新时间:2023-11-01 12:56:45 24 4
gpt4 key购买 nike

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

其实我有两个问题:

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

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

最佳答案

这是一个解决方案。

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

?>

简短而甜美。

关于php - 集成测试 PHPUnit 和 Phinx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25343227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com