我想在Zend Framework应用程序中使用PHP单元。我需要在测试中进行多个数据库写入操作。 我想在setUpBeforeClass()方法中启动一个MySQL事务。这是可能的,但如果我尝试在tearDownAfterClass()方法中回滚事务,他会抛出一个异常,并显示消息'There no no active transaction'。测试方法在数据库中执行写入操作。 但是,如果我在测试方法本身开始交易。它像我想要的那样工作。 我不明白为什么它会这样反应。知道任何人的解释?Zend Framework,PHPUnit和交易
<?php
class ConferenceControllerTest
extends PHPUnit_Framework_TestCase
{
/**
* A database connection.
* @var Zend_Db_Adapter_Pdo_Mysql
*/
protected static $hostDb = null;
public static function setUpBeforeClass()
{
static::$hostDb = Zend_Registry::get('db_host');
static::$hostDb->beginTransaction();
// The transaction for the Adapter is activated. But not inside the tests anymore.
}
public function testTest1()
{
// At this position teh transaction is not setted anymor? Why?
static::$hostDb->beginTransaction();
$sql = 'INSERT INTO test(test) VALUES(5);';
static::$hostDb->exec($sql);
}
public static function tearDownAfterClass()
{
try
{
static::$hostDb->rollBack();
}
catch(Exception $exception)
{
$message = $exception->getMessage();
Zend_Debug::dump($message);
}
}
}