Archives


Master Symfony2 fundamentals

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Discover the SensioLabs Support

Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com

Fabien Potencier
New in Symfony 2.2: Functional Tests Speed-Up
by Fabien Potencier – January 16, 2013 – 3 comments

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!

Comments RSS

  • Vazgen Manukyan
    #1 Vazgen Manukyan said on the 2013/01/17 at 00:05
    This is a really good feature
  • Grégoire Pineau
    #2 Grégoire Pineau said on the 2013/01/17 at 10:34
    The configuration is under the "framework" key.
    May be you should update the post.
  • Frank Stelzer
    #3 Frank Stelzer said on the 2013/01/22 at 16:15
    Nice improvement!

    One small side note: The option is called "enabled" and not "enable" as it is mentioned in the snippet.