在 Symfony 2 WebTestCase 上创建测试数据库和加载夹具的最佳方法? [英] Best way to create a test database and load fixtures on Symfony 2 WebTestCase?

查看:20
本文介绍了在 Symfony 2 WebTestCase 上创建测试数据库和加载夹具的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在我的应用程序中执行一些基本路由的 WebTestCase.

I have a WebTestCase that executes some basic routes in my application.

我想在 PHPUnit 的 setUp 方法上创建一个与我的主数据库相同的测试数据库,并将夹具加载到其中.

I want to, on the setUp method of PHPUnit, create a test database identical to my main database, and load fixtures into it.

我目前正在做一些解决方法并执行一些控制台命令,如下所示:

I'm currently doing some workaround and executing some console commands, something like this:

class FixturesWebTestCase extends WebTestCase
{
    protected static $application;

    protected function setUp()
    {
        self::runCommand('doctrine:database:create');
        self::runCommand('doctrine:schema:update --force');
        self::runCommand('doctrine:fixtures:load --purge-with-truncate');
    }

    protected static function runCommand($command)
    {
        $command = sprintf('%s --quiet', $command);

        return self::getApplication()->run(new StringInput($command));
    }

    protected static function getApplication()
    {
        if (null === self::$application) {
            $client = static::createClient();

            self::$application = new Application($client->getKernel());
            self::$application->setAutoExit(false);
        }

        return self::$application;
    }
}

但我很确定这不是最好的方法,特别是因为 doctrine:fixtures:load 期望用户点击 Y 字符来确认操作.

But I'm quite sure this is not the best approach, especially because the doctrine:fixtures:load expects the user to hit a Y char to confirm the action.

我该如何解决?

推荐答案

如果要使用doctrine:fixtures:load,可以使用--append选项避免用户确认.由于您每次都在重新创建数据库,因此不需要清除.我曾经单独使用教义夹具进行测试,但后来改用夹具和LiipFunctionalTestBundle 避免 DRY.此捆绑包使灯具更易于管理.

If you want to use doctrine:fixtures:load, you can use the --append option to avoid the user confirmation. Since you are recreating the database every time, purging is unnecessary. I used to use doctrine fixtures alone for testing, but have since switched to using fixtures & LiipFunctionalTestBundle to avoid DRY. This bundle makes fixtures easier to manage.

David Jacquel 的答案是加载 Doctrine Fixtures 的正确答案:

David Jacquel's answer is the correct one for loading Doctrine Fixtures:

doctrine:fixtures:load --no-interaction 
or
doctrine:fixtures:load -n

这篇关于在 Symfony 2 WebTestCase 上创建测试数据库和加载夹具的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆