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

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

问题描述

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



我想在 setUp PHPUnit的方法,创建与我的主数据库相同的测试数据库,并将其加载到其中。



我正在做一些解决方法并执行一些控制台命令像这样:

  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');
}

保护静态函数runCommand($ command)
{
$ command = sprintf('%s --quiet',$ command);

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

保护静态函数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 char来确认该操作。 / p>

如何解决?

解决方案

如果要使用 doctrine:fixtures:load ,您可以使用 - 追加选项来避免用户确认。由于您每次都在重新创建数据库,所以无需进行清除。我以前一直在使用教学装置进行测试,但是从那时候开始使用灯具 LiipFunctionalTestBundle 以避免DRY。这个捆绑使得灯具更容易管理。



编辑:David Jacquel的答案是加载Doctrine灯具的正确答案:

  doctrine:fixtures:load --no-interaction 

doctrine:fixtures:load -n
pre>

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

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;
    }
}

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.

How can I solve that?

解决方案

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.

EDIT: 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天全站免登陆