2016-09-04 25 views
0

我正在執行測試以查看某個路由是否正常工作。頁面/路由發現在http://localhost/login,當我在瀏覽器中它的工作正常,我已經測試了這沒有餅乾和我的緩存清除。symfony功能測試的500個狀態代碼

但是,只要我在phpunit LoginControllerTest類中測試它,我會收到一個500狀態碼內部服務器錯誤。

<?php 

namespace Tests\AppBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 

class LoginControllerTest extends WebTestCase 
{ 
    public function testLoginPage() 
    { 
     $client = static::createClient(); 

     $crawler = $client->request('GET', '/login'); 
     // var_dump($client->getRequest()->getContent()); // Is empty 
     // 200 => 'OK' 
     $this->assertEquals(200, $client->getResponse()->getStatusCode()); 
    } 
    // ... 

所以運行phpunit失敗:

Failed asserting that 500 matches expected 200. 

這裏是security.yml在沒有任何用處的理解問題的情況下。

# app/config/security.yml 
security: 
    providers: 
     db_provider: 
      entity: 
       class: AppBundle:Users 
       property: Username 

    role_hierarchy: 
     ROLE_ADMIN:  ROLE_USER 
     ROLE_SUPER_ADMIN: [ ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ] 

    firewalls: 
     secured_area: 
      pattern: ^/ 
      anonymous: ~ 

     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 

     default: 
      pattern: ^/manage 
      guard: 
       authenticators: 
        - app.token_authenticator 

    access_control: 
     - { path: ^/manage, roles: ROLE_USER } 

該路線已定義並正常工作(如預期)。

// ... 
class LoginController extends Controller 
{ 
    /** 
    * @Route("/login", name="LoginForm") 
    */ 
    public function loginFormAction(Request $request) 
    { 
    // ... 

回答

2

500錯誤可能是由您的案例中的任何內容引起的。

你在這種情況下可以做的是轉儲響應的內容並檢查Symfony調試消息。

我通常直接在終端使用這些代碼段用於快速檢測這樣的錯誤:

if (!$response->isSuccessful()) { 
    $block = $crawler->filter('div.text_exception > h1'); 
    if ($block->count()) { 
     $error = $block->text(); 
    } 
} 

其中div.text_exception > h1是Symfony2的調試頁面中的XPath的消息本身

然後,只需打印$error

+1

感謝您讓我走向正確的方向。我一直在查看響應的輸出,並使用您的代碼在終端中打印錯誤。我發現使用'''crawler-> filter('title');''提供了一個很好的友好錯誤信息 - 你給的xpath實際上並不存在我得到的響應。我現在應該能夠修復這些錯誤。 – iyop45