Fabien Potencier
Contributed by Fabien Potencier in #4897

When writing functional tests, having access to the Symfony profiler can be very useful. You can use it to verify that a given page executes less than a certain number of database queries (read the dedicated cookbook entry), to check the time spent in the framework, or to test any other data collected during the request handling. But that comes with a cost as the profiler slows down the handling of a request quite a bit.

As of Symfony 2.2, the profiler is disabled by default in functional tests (at least in the default configuration that comes with the Standard Edition):

1
2
3
4
# in app/config/config_test.yml
framework:
    profiler:
        enabled: false

Even if it is disabled by default, you can still explicitly enable it for the very next request when you need it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public function testIndex()
{
    $client = static::createClient();

    // Enable the profiler for the next request
    // it does nothing if the profiler is not available
    $client->enableProfiler();

    $crawler = $client->request('GET', '/hello/Fabien');

    // ... write some assertions about the Response

    // Check that the profiler is enabled
    if ($profile = $client->getProfile()) {
        // check the number of requests
        $this->assertLessThan(10, $profile->getCollector('db')->getQueryCount());
    }
}

Free performance improvement is always nice!

Published in #Living on the edge