Symfony2:在PHPUnit中创建模式只能一次运行一个命令 [英] Symfony2: Creating Schema in PHPUnit only runs one command at a time

查看:101
本文介绍了Symfony2:在PHPUnit中创建模式只能一次运行一个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony2和PHPUnit与Doctrine并编写一个插件包。我有一个扩展Symfony的WebTestCase的TestCase类。

I'm using Symfony2 and PHPUnit with Doctrine and writing a plugin bundle. I've got a TestCase class extending Symfony's WebTestCase.

在setUp方法中,我需要创建一个用于测试和加载某些灯具的模式。

During the setUp method, I need to create a schema for testing and load some fixtures.

我的代码(下图)正在构建一个应用内核,启动一个应用程序,然后运行几个命令来放置一个模式,如果它在那里,重新创建它并加载一些数据夹具。

My code (below) is building an app kernel, booting an application and then running several commands to drop a schema if it's there, re-create it and load some data fixtures.

我遇到的问题是,当在应用程序上使用run()方法时,它只运行第一个命令,然后测试停止,标记为成功,没有错误或消息。

The problem I'm getting is that when using the run() method on the Application, it's only running the first command, then the test stops, marking as successful with no errors or messages.

为了执行下一个命令,我需要表示第一个命令,再次运行测试。

In order to execute the next command, I need to commend out the first one and run the test again.

显然,我想要的结果是每个方法都可以顺序运行。

Obviously, my desired result is for each method to run sequentially.

/**
 * setUp
 *
 * @return void
 **/
public function setUp()
{
    parent::setUp();

    $this->appKernel = $this->createKernel();
    $this->appKernel->boot();

    $this->application = new Application($this->appKernel);

    $this->em = $this->appKernel->getContainer()->get('doctrine')->getManager();

    $this->buildDb();
}

/**
 * buildDb
 * Builds the DB from the Entities in Acme\TestBundle\Entity
 * @return void
 **/
private function buildDb()
{
    $this->application->run(new ArrayInput(array(
        'doctrine:schema:drop',
        '--force' => true
    )));

    $this->application->run(new ArrayInput(array(
        'doctrine:schema:create'
    )));

    $this->application->run(new ArrayInput(array(
        'doctrine:fixtures:load',
        '--fixtures' => 'tests/SupportFiles/bundles/Acme/TestBundle/DataFixtures/Test'
    )));

}


推荐答案

鼓励您查看 ICBaseTestBundle ,它可以轻松地为每个测试用例创建一个干净的数据库,并加载适当的数据固定装置,而不是编写你自己的版本。

I would encourage you to check out ICBaseTestBundle which can handle easily creating a clean database for each test case, and loading appropriate data fixtures, instead of writing your own version of doing it.

这篇关于Symfony2:在PHPUnit中创建模式只能一次运行一个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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