2017-04-13 25 views
0

我有一個Symfony 3.2項目與後端。每個實體都有其CRUD控制器,視圖等。我準備了一個 abstract class AbstractControllerTest extends WebTestCase這是每個實體測試的基礎。對於每個實體,我使用一個簡單的測試來確定該列表,顯示,編輯和新的返回HTTP 200爲什麼PHPUnitTest WebTestCase考慮到以前的測試?

所以當我運行所有測試它的測試列表,顯示等每個實體。問題是,在列表控制器中,我使用默認順序的KNPPaginator。該控制器工作正常,但是當我運行測試,並得到第二個實體時,我得到500錯誤,因爲缺少實體字段。事實證明,測試採用了來自之前測試的尋呼機列表查詢。 所以實體A在默認情況下與位置字段一起排序。實體B沒有位置字段並導致錯誤。所以當PHPUnit去測試一個實體時,它是可以的,然後它移動到測試B實體,然後出現錯誤。 我不知道發生了什麼,因爲排序不會保存在會話中,所以PHPUnit無法從前一個實體的會話中獲取查詢。 任何想法正在發生什麼?

AbstractControllerTest

abstract class AbstractControllerTest extends WebTestCase 
{ 
    /** @var Client $client */ 
    public $client = null; 

    protected $user = ''; 
    protected $prefix = ''; 
    protected $section = ''; 
    protected $entityId = ''; 

    public function setUp() 
    { 
     $this->client = $this->createAuthorizedClient(); 
    } 

    /** 
    * @return Client 
    */ 
    protected function createAuthorizedClient() 
    { 
     $client = static::createClient(); 
     $client->setServerParameter('HTTP_HOST', $client->getContainer()->getParameter('test_info_domain')); 
     $client->setServerParameter('HTTPS', true); 
     $client->followRedirects(); 
     $container = $client->getContainer(); 

     $session = $container->get('session'); 
     /** @var $userManager \FOS\UserBundle\Doctrine\UserManager */ 
     $userManager = $container->get('fos_user.user_manager'); 
     /** @var $loginManager \FOS\UserBundle\Security\LoginManager */ 
     $loginManager = $container->get('fos_user.security.login_manager'); 
     $firewallName = $this->section; 

     /** @var UserInterface $userObject */ 
     $userObject = $userManager->findUserBy(array('username' => $this->user)); 
     $loginManager->logInUser($firewallName, $userObject); 

     // save the login token into the session and put it in a cookie 
     $container->get('session')->set('_security_' . $firewallName, 
      serialize($container->get('security.token_storage')->getToken())); 
     $container->get('session')->save(); 
     $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId())); 

     return $client; 
    } 

    public function testIndex() 
    { 
     //CRUD index 
     $this->client->request('GET', sprintf('/%s/%s',$this->section,$this->prefix)); 
     $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 
    } 

    public function testShow() 
    { 
     //CRUD show 
     $this->client->request('GET', sprintf('/%s/%s/%s/show',$this->section,$this->prefix, $this->entityId)); 
     $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 
    } 

    public function testEdit() 
    { 
     //CRUD edit 
     $this->client->request('GET', sprintf('/%s/%s/%s/edit',$this->section,$this->prefix, $this->entityId)); 
     $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 
    } 

    public function testNew() 
    { 
     //CRUD new 
     $this->client->request('GET', sprintf('/%s/%s/new',$this->section,$this->prefix)); 
     $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 
    } 
} 

以及用於一個測試類爲控制器中的一個的示例實體

class AgendaCategoryControllerTest extends AbstractControllerTest 
{ 
    protected $user = '[email protected]'; 
    protected $section = 'admin'; 
    protected $prefix = 'agenda-category'; 
    protected $entityId = '40'; 
} 

如果我單獨運行

php phpunit.phar src/Bundle/Tests/Controller/Admin/AControllerTest.php

php phpunit.phar src/Bundle/Tests/Controller/Admin/BControllerTest.php

這是可以的。 如果運行起來有這種怪異的錯誤

php phpunit.phar -c phpunit.xml.dist --testsuite=Admin 
+0

測試不得鏈接。如果是這樣,他們必須在相同的測試案例。 – COil

+0

你是什麼意思?一個控制器測試對index show edit new進行單獨測試。對於每個實體,我使用不同的ControllerTest類 – Tom

+0

很難看到你在沒有測試的情況下做什麼。 – COil

回答

1

你可以做您的安裝程序,方法如下測試之間重置您的測試客戶端:

public function setUp() 
{ 
    $this->client = $this->createAuthorizedClient(); 

    $this->client->restart(); 
} 

您可能需要重新啓動進入你的createAuthorizedClient方法來確保它不會重置您的授權信息。

+0

您是完全正確的。非常感謝你 :) – Tom

相關問題