Skip to content
Advertisement

500 status code for symfony functional test

I’m performing a test just to see if a certain route is working. The page/route is found at http://localhost/login and when I go there in the browser it works fine and I’ve tested this with no cookies and my cache cleared.

However, as soon as I test it in the phpunit LoginControllerTest class I recieve a 500 status code internal server error.

<?php

namespace TestsAppBundleController;

use SymfonyBundleFrameworkBundleTestWebTestCase;

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());
    }
    // ...

And so running phpunit fails with:

Failed asserting that 500 matches expected 200.

Here is the security.yml in case that is of any use in understanding the problem.

# 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 }

The route is defined and working (as expected).

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

Advertisement

Answer

The 500 error can be caused by anything in your case.

What you could do in such situation is to dump the content of the response and check the Symfony debugging message.

I usually use such code snippet for quick checking such errors directly in terminal:

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

where div.text_exception > h1 is the xpath from Symfony2 debug page to the message itself

Then just print the $error

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement