使用Symfony2进行功能测试时如何回滚事务 [英] How to rollback transactions when doing functional testing with Symfony2

查看:130
本文介绍了使用Symfony2进行功能测试时如何回滚事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Symfony2中的项目编写功能测试。我想测试用户是否可以访问页面,填写表格并提交。
我正在尝试找到一种将数据库还原到测试前状态的方法。
我在 https://gist.github.com/Vp3n / 5472509 扩展了WebTestCase以及重载setUp和tearDown方法。
这是我为了使它起作用而进行的修改:

I'm trying to write a functional test for my project in Symfony2. I'd like to test if a user can access a page, fill up a form and submit it. I'm trying to find a way to rollback the database to the state it was before the test. I found a helper class I slightly modified at https://gist.github.com/Vp3n/5472509 which extends WebTestCase and overload setUp and tearDown methods. Here are the mods I did in order to try to make it works:

 /**
 * Before each test we start a new transaction
 * everything done in the test will be canceled ensuring isolation et speed
 */
protected function setUp()
{
    parent::setUp();
    $this->client = $this->createClient();
    $this->em = static::$kernel->getContainer()
        ->get('doctrine')
        ->getManager();
    $this->em->getConnection()->beginTransaction();
    $this->em->getConnection()->setAutoCommit(false);
}
/**
 * After each test, a rollback reset the state of 
 * the database
 */
protected function tearDown()
{
    parent::tearDown();
    if($this->em->getConnection()->isTransactionActive()){
        echo 'existing transactions';
        $this->em->getConnection()->rollback();
        $this->em->close();
    }
}

运行测试时,它会确认现有事务,但回滚失败

When I run the tests, it acknowledges for existing transactions but the rollback fails and modifications are persisted.

测试日志:

Runtime:       PHP 5.6.15

.existing transactions.      2/2 (100%)existing transactions                                                                                      

Time: 5.47 seconds, Memory: 24.50MB

OK (2 tests, 5 assertions)

我在做什么错?

EDIT

这对我有用:

abstract class DatabaseWebTest extends WebTestCase {
    /**
     * helper to acccess EntityManager
     */
    protected $em;
    /**
     * Helper to access test Client
     */
    protected $client;
    /**
     * Before each test we start a new transaction
     * everything done in the test will be canceled ensuring isolation et speed
     */
    protected function setUp()
    {
        parent::setUp();
        
        $this->client = $this->createClient(['environment' => 'test'], array(
            'PHP_AUTH_USER' => 'user',
            'PHP_AUTH_PW'   => 'password',
            ));
        $this->client->disableReboot();
        $this->em = $this->client->getContainer()->get('doctrine.orm.entity_manager');        
        $this->em->beginTransaction();
        $this->em->getConnection()->setAutoCommit(false);
    }
    /**
     * After each test, a rollback reset the state of 
     * the database
     */
    protected function tearDown()
    {
        parent::tearDown();

        if($this->em->getConnection()->isTransactionActive()) {
            $this->em->rollback();
        }        
    }
}


推荐答案

您是否与客户端进行多个请求?如果是这样,您的问题可能是客户端在执行一个请求后关闭了内核。但是您可以使用 $ this-> client-> disableReboot()禁用它,因此此代码段应该是幂等的:

Are you doing more than one request with the Client? If so, you problem might be that the client shutdowns the kernel after one request was performed. But you can disable that with $this->client->disableReboot() So this snippet snippet should be idempotent:

public function setUp()
{
    $this->client = $this->createClient(['environment' => 'test']);
    $this->client->disableReboot();
    $this->em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
    $this->em->beginTransaction();
}

public function tearDown()
{
    $this->em->rollback();
}

public function testCreateNewEntity()
{
    $this->client->request('GET', '/create/entity/form');
    $this->client->request('POST', '/create/entity/unique/123');
}

这篇关于使用Symfony2进行功能测试时如何回滚事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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