2016-04-26 46 views
5

我很努力去理解我的功能測試或項目設置有什麼問題:phpunit執行只是打印出以下信息(我不打印出測試套件 - 即它不是來自客戶端 - > getResponse()打印或任何東西)。Symfony2功能測試打印出重定向html並停止測試執行

phpunit -c app --group temp1 src/AppBundle/Tests/Controller/SecurityControllerTest.php 

我的測試代碼:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <meta http-equiv="refresh" content="1;url=/" /> 

     <title>Redirecting to /</title> 
    </head> 
    <body> 
     Redirecting to <a href="/">/</a>. 
    </body> 
</html> 

從命令行運行PHPUnit之後:另外,該文本打印出到命令行之後的整個測試執行立即停止而沒有任何結果的信息相當簡單:

class SecurityControllerTest extends WebTestCase 
{ 
    /** 
    * test login with succesfull case 
    * 
    * @group login 
    * @group temp1 
    */ 
    public function testLogin_success() 
    { 
     $client = static::createClient(); 
     $crawler = $client->request('GET', '/'); 

     // temp - just to test that the initial crawler location is correct 
     $this->assertGreaterThan(0, $crawler->filter('html:contains("System Login")')->count(), "login page not found"); 

     $client->followRedirects(true); 

     $crawler = $this->loginSubmit($client, $crawler, "[email protected]", "basic_user1_password"); 
     // THE BELOW ROWS ARE NEVER REACHED 

     // test that the user can access the user home 
     $crawler = $client->request('GET', '/user/myprofile'); 
     $this->assertGreaterThan(0, $crawler->filter('html:contains("Welcome to your user profile")')->count(), "User homepage not accessible after login"); 
    } 

    private function loginSubmit($client, $crawler, $username, $password) 
    {   
     $loginButton = $crawler->selectButton('Login'); 
     $loginForm = $loginButton->form(); 
     $loginForm['form[email]'] = $username; 
     $loginForm['form[password]'] = $password; 

     // BELOW IS THE ROW THAT RESULTS IN THE PRINTOUT + TEST EXECUTION STOP 
     return $client->submit($loginForm); 
    } 

    //.... 
} 

此外,e xactly相同的測試用例在我正在處理的另一個項目上工作正常,所以我一直試圖挖掘項目配置中的差異,但沒有運氣。

任何幫助非常感謝 - 如果這可能是有用的/相關的,隨意要求其他代碼/配置文件內容。

使用symfony的2.3.12 & PHPUnit的4.1.0

更新:特定的代碼鏈,導致錯誤

所以管理由解決基本的項目會議議題夫婦解決此問題後,幾天前,我回到調試這個問題還有點進一步。而目前看來,結果是由於下面的代碼鏈,第一呼叫前轉:

$this->forward('bundle:controller:action')->send(); 

,並在轉發控制器動作調用重定向:

$this->redirect($this->generateUrl($route, $parameters))->send(); 

顯然這個控制器的流動似乎有點怪總的來說,但問題仍然是爲什麼這導致了觀察結果?

+1

斷言來自下面的行永遠不會到達的評論。我會將這些問題添加到 – ejuhjav

+0

您是使用默認Web服務器還是使用apache進行檢查? –

+0

@GaneshPatil我從phpunit的命令行運行這個(更新了問題) – ejuhjav

回答

1

您可以嘗試在登錄功能,增加一些檢查:

private function loginSubmit($client, $crawler, $username, $password) 
{ 
    // Check that the HTTP status is good. 
    $this->assertEquals(200, $client->getResponse()->getStatusCode()); 

    // Check that there is a form. 
    $this->assertEquals(1, $crawler->filter('form')->count()); 

    $loginButton = $crawler->selectButton('Login'); 
    $loginForm = $loginButton->form(); 
    $loginForm['form[email]'] = $username; 
    $loginForm['form[password]'] = $password; 

    // BELOW IS THE ROW THAT RESULTS IN THE PRINTOUT + TEST EXECUTION STOP 
    $crawler = $client->submit($loginForm); 

    // Check that the HTTP status is good. 
    $this->assertEquals(200, $client->getResponse()->getStatusCode()); 

    // Check that the login is successful, it depends on your code. 
    // For example if you show a message "Logged in succesfully." in a <div>: 
    $this->assertEquals(1, $crawler->filter('div:contains("Logged in succesfully.")')->count()); 

    // If nothing works, display the rendered HTML to see if there is an error: 
    // die($this->client->getResponse()->getContent()); 

    return $crawler; 
} 
+0

感謝您的努力,但由此產生的行爲保持完全一樣。調用$ client-> submit($ loginForm);仍然打印出相同的信息並中斷測試執行。 – ejuhjav

+0

@ejuhjav:如果我們評論$ crawler = $ client-> submit($ loginForm);這段代碼是否爲所有剩餘的斷言提供了正確的結果? –

+0

@GaneshPatil'$ crawler'不會被定義,它會拋出一個PHP錯誤。 –

2

在功能測試(official doc)登錄,在時,當我在執行第二次的$client->request(...)我有這個問題。將自己的測試課中單獨的測試分開並不能解決問題。

我通過解決了這個問題,而不是設置了一個cookie。幸運的是我的測試不依賴於cookie,所以所有的測試都通過了。

也許這個信息會幫助你,更加孤立你的問題。

+1

感謝您的答案:我設法解決了這個問題前幾天,通過使用來自請求的會話和php標準會話函數(如session_id())來快速混合項目中完成的會話管理,在測試環境中沒有訪問同一個會話)。我現在回到這個原始問題,並更詳細地指出了罪魁禍首。更新了我的問題,因爲我仍然不知道導致這個結果的確切原因是什麼。 – ejuhjav

+0

我有同樣的問題,我認爲這是由於我在重定向期間測試會話Flashbag的事實。執行情況良好,測試通過,但仍然有醜陋的html輸出。 –

1

我曾與測試代碼

$this->assertTrue($client->getResponse()->isSuccessful()); 

我是通過這個斷言運行相對URL的數組這個問題。其中一個網址沒有成功,我的應用程序針對該情況進行了重定向。而不是失敗斷言,phpunit吐出RedirectResponse html代碼並死亡。