gpt4 book ai didi

symfony - 在 Symfony 2 WebTestCase 上创建测试数据库和加载固定装置的最佳方法?

转载 作者:行者123 更新时间:2023-12-03 05:35:35 26 4
gpt4 key购买 nike

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

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

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

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 字符来确认操作.

我该如何解决这个问题?

最佳答案

如果你想使用doctrine:fixtures:load,你可以使用--append选项来避免用户确认。由于每次都重新创建数据库,因此不需要清除。我曾经单独使用 Doctrine 固定装置进行测试,但后来改用固定装置 & LiipFunctionalTestBundle以避免干燥。该 bundle 使灯具更易于管理。

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

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

关于symfony - 在 Symfony 2 WebTestCase 上创建测试数据库和加载固定装置的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14752930/

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