Search code examples
phpsymfonyphpunitsymfony4

Symfony PHPUnit WebTestCase with RenderController


I have implemented this test in my app

public function testHome()
{
    $client = static::createClient();
    $client->request('GET', '/');
    $this->assertEquals(200, $client->getResponse()->getStatusCode());
}

It fails because of a render controller in my twig

{% extends 'base-pages.html.twig' %}

{% block main %}
    <main>
        {{ render(controller('App\\Controller\\AppController::futureEvents')) }}
        {{ include('includes/popular-places.html.twig') }}
        {{ include('includes/news-event.html.twig') }}
        {{ include('includes/call-section.html.twig') }}
    </main>
{% endblock %}

The Controller Rendered:

public function futureEvents()
{
    $events = $this->getDoctrine()->getRepository(Event::class)->findAll();

    return $this->render('includes/events-home-list.html.twig', [
        'events' => $events,
    ]);
}

The error:

There was 1 failure:

1) App\Tests\Controller\HomeControllerTest::testHome Failed asserting that 500 matches expected 200.

/var/www/html/app-web/tests/Controller/HomeControllerTest.php:15

Why does that happen? Is there a way to handle renderController in a Web Test Case?


Solution

  • I have found it. According to this doc: https://symfony.com/doc/current/testing/database.html#changing-database-settings-for-functional-tests

    You have to add a env in the php section in your phpunit.xml.dist.

    <env name="DATABASE_URL" value="mysql://root:root@localhost/app" />