gpt4 book ai didi

symfony - 使用 Symfony2 进行功能测试时如何回滚事务

转载 作者:行者123 更新时间:2023-11-28 19:50:53 25 4
gpt4 key购买 nike

我正尝试在 Symfony2 中为我的项目编写功能测试。我想测试用户是否可以访问页面、填写表格并提交。我试图找到一种方法将数据库回滚到测试前的状态。我在 https://gist.github.com/Vp3n/5472509 找到了一个我稍微修改过的辅助类它扩展了 WebTestCase 并重载了 setUp 和 tearDown 方法。以下是我为使其正常工作所做的修改:

 /**
* 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();
}
}

当我运行测试时,它会确认现有事务,但回滚失败并保留修改。

测试日志:

Runtime:       PHP 5.6.15

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

Time: 5.47 seconds, Memory: 24.50MB

OK (2 tests, 5 assertions)

我做错了什么?这甚至是最佳做法吗?

编辑

这对我有用:

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

最佳答案

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

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

关于symfony - 使用 Symfony2 进行功能测试时如何回滚事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42295526/

25 4 0