2014-02-20 112 views
1

我有一個關於Symfony2的功能測試。它創建了一些測試實體,在測試完成後立即進行測試並刪除臨時實體。Symfony2功能測試後執行

[CREATE TEMP ENTITIES] => [RUN TESTS] => [DELETE TEMP ENTITIES] 

問題是當斷言失敗時,臨時實體不會被刪除。

請參閱下面的代碼(我評論這是不執行的行):

<?php 
public function testChangePassword() 
    { 
     $enduser = $this->logIn(); 
     $enduserSalt = $enduser->getSalt(); 
     $successMsg = $this->translator->trans('change_pwd.success'); 
     $submitButtonText = $this->translator->trans('change_pwd.submit'); 

     /** 
     * Test Case 1: Try to change password with incorrect old password 
     */ 
     $url = $this->router->generate('userpanel_change_password'); 
     $crawler = $this->client->request('GET', $url); 

     $form = $crawler->selectButton($submitButtonText)->form(
      [ 
       'password_change[old_password]' => 'xaxaxaxa', 
       'password_change[password][password]' => '123456789', 
       'password_change[password][confirm]' => '123456789' 
      ] 
     ); 

     $this->client->followRedirects(); 
     $crawler = $this->client->submit($form); 

     $this->assertTrue($crawler->filter('html:contains("' . $successMsg . '")')->count() == 2); 


     /** 
     * Following line isn't executed when above assertion fails. 
     */ 
     $this->removeTestUser(); 

     /** 
     * Test Case 2: Change password success 
     */ 

     $form = $crawler->selectButton($submitButtonText)->form(
      [ 
       'password_change[old_password]' => $this->testUserInfo['password'], 
       'password_change[password][password]' => '123456789', 
       'password_change[password][confirm]' => '123456789' 
      ] 
     ); 

     $this->client->followRedirects(); 
     $crawler = $this->client->submit($form); 

     $this->assertTrue($crawler->filter('html:contains("' . $successMsg . '")')->count() > 0); 
     $this->removeTestUser(); 
    } 

    private function logIn() 
    { 
     $this->removeTestUser(); 
     $enduser = $this->createTestUser(); 

     $session = $this->client->getContainer()->get('session'); 

     $firewall = 'main'; 
     $token = new UsernamePasswordToken($enduser, null, $firewall, array('ROLE_USER')); 
     $session->set('_security_' . $firewall, serialize($token)); 
     $session->save(); 

     $cookie = new Cookie($session->getName(), $session->getId()); 
     $this->client->getCookieJar()->set($cookie); 

     return $enduser; 
    } 

    private function createTestUser($salt = null) 
    { 
     $this->removeTestUser(); 

     $newUser = new Enduser(); 
     $newUser->setName($this->testUserInfo['name']); 
     $newUser->setEmail($this->testUserInfo['email']); 

     $factory = $this->client->getContainer()->get('security.encoder_factory'); 
     $encoder = $factory->getEncoder($newUser); 
     $password = $encoder->encodePassword($this->testUserInfo['password'], $newUser->getSalt()); 
     $newUser->setPassword($password); 

     if (!is_null($salt)) { 
      $newUser->setSalt($salt); 
     } 

     $this->em->persist($newUser); 
     $this->em->flush(); 

     return $newUser; 
    } 

    private function removeTestUser() 
    { 
     $this->em->getRepository('LabsCoreBundle:Enduser')->createQueryBuilder('E') 
      ->delete() 
      ->where('E.email = :email') 
      ->setParameter('email', $this->testUserInfo['email']) 
      ->getQuery() 
      ->execute(); 
    } 

,首先會出現這樣的方法,「總是」測試運行後,就像一個symfony1 postExecute(如關機的方法可能)?

由於我的每個測試都以刪除臨時實體結束,所以我想在類上實現一個機制來刪除這些實體。

我試圖用try/catch塊封裝斷言檢查,但它沒有任何意義。

回答

1

您可以使用您的測試類的刪除()方法,該方法將在每次測試後執行,或者如果你想在所有測試的

例子,在剛剛結束一次運行的方法使用tearDownAfterClass() http://phpunit.de/manual/3.7/en/fixtures.html