2017-02-17 23 views
1

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

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

}

回答

3

您是否在使用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'); 
} 
+0

对于最近的回复很抱歉。事实上,问题在于,由于内核在每次测试之间复位,事务并不是共享的。所以disableReboot()与setAutoCommit(false)一起制作了这个技巧。感谢您的回复和答复。 – EtienneDh

1

你可以使用一个奉献的测试数据库用于测试

另一种选择正在使用Codeception。这是一个适用于Symfony的单元测试包。 如果您使用此功能,则可以将其配置为使用测试数据库,然后在每个测试周期后“清除”。
这样做的例子yaml configuartion会是这样的,它是cleanup: true做你想做的事情;

class_name: UnitTester 
modules: 
    enabled: 
     - Asserts 
     - Symfony2: 
      app_path: '../../app' 
      var_path: '../../app' 
      environment: 'test' 
     - Doctrine2: 
      depends: Symfony2 
      cleanup: true 
     - \AppBundle\Helper\Unit 
+0

谢谢,我会看看这个。虽然我不确定我被允许使用这个项目的第一个地方不允许使用的软件包 – EtienneDh

相关问题