2014-02-20 3 views
1

Symfony2에 대한 기능 테스트가 있습니다. 테스트 엔티티를 만들고 테스트를 수행 한 후 테스트가 완료된 직후 임시 엔티티를 제거합니다.Symfony2 기능 테스트 게시 실행

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

어설 션이 실패 할 때 임시 엔티티가 삭제되지 않는 문제가 있습니다.

코드는 아래를 참조하시기 바랍니다 (I 실행되지 않습니다 라인 주석) :

모든
<?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

당신은 모든 테스트 후 실행됩니다 테스트 클래스에서의 tearDown() 메소드를 사용하거나 모든 테스트의 끝

예에에 한 번만 방법을 실행하려면 tearDownAfterClass()를 사용할 수 있습니다 http://phpunit.de/manual/3.7/en/fixtures.html