Support for Symfony Functional Tests

By Jérôme Vieilledent, on Feb 19, 2021

Blackfire provides a built-in test system thanks to Blackfire Player, an Open-Source HTTP client capable of following scenarios written in a dedicated Domain Specific Language (aka DSL). These scenarios allow you to run your performance tests on critical parts of your web application for which you define the steps to follow. In addition, you may introduce functional tests in your scenarios, which are known as expectations in Blackfire Player scenarios.

However, you may already have implemented such scenarios, e.g. using the WebTestCase class provided by the Symfony framework. In such case, you may not want to define (and maintain!) your functional tests scenarios using another tool.

New Integration with Symfony

In order to avoid the multiplication of tools, we implemented a new integration with our Builds system, based on Symfony Functional Tests and PHPUnit. It is now part of the PHP SDK, as of version 1.24.0.

Thanks to this integration, you can leverage these functional tests in order to create Blackfire Builds, directly from your CI system. It uses Symfony Panther in the background, in order to send real HTTP requests, and to manage the PHP built-in web server (even though it is recommended to use a more performant one like Symfony Local Web Server).

To get you started, you first need to install the Blackfire PHP SDK and Symfony Panther, and configure PHPUnit:

composer require blackfire/php-sdk symfony/panther
<!-- phpunit.xml.dist -->
    <extensions>
        <!-- Add Symfony Panther extension -->
        <extension class="Symfony\Component\Panther\ServerExtension" />
        <!-- Blackfire extension -->
        <extension class="\Blackfire\Bridge\PhpUnit\BlackfireBuildExtension">
            <arguments>
                <string><!-- Your environment name or UUID --></string>
                <string><!-- Name for the build (optional) --></string>
            </arguments>
        </extension>
    </extensions>

Then, you need to make your test case classes extend BlackfireTestCase (or make them use the BlackfireTestCaseTrait), and change a couple of lines in the tests for which you want to add performance tests:

namespace App\Tests\Controller;
 ​
 use App\Pagination\Paginator;
 use Blackfire\Bridge\PhpUnit\BlackfireTestCase;
 ​
 class BlogControllerTest extends BlackfireTestCase
 {
     // Give a title to the scenario.
     protected const BLACKFIRE_SCENARIO_TITLE = 'Blog Controller';
 ​
     public function testIndex(): void
     {
         // Create the Blackfire enabled HTTP browser.
         $client = static::createBlackfiredHttpBrowserClient();
         $crawler = $client->request('GET', '/en/blog/');
 ​
         $this->assertResponseIsSuccessful();
 ​
         $this->assertCount(
             Paginator::PAGE_SIZE,
             $crawler->filter('article.post'),
             'The homepage displays the right number of posts.'
         );
     }
 }

When running PHPUnit, a Blackfire Build is automatically started, and a Scenario is created for each instance of BlackfireTestCase by default. Note that it is still possible to control the scenario aspect.

Build result screenshot

Give Blackfire a try!

Builds are available for Premium and Enterprise plans.

Play with the demo or subscribe now!

Happy Profiling!

Jérôme Vieilledent

As a Developer Advocate, Jérôme is all about spreading the love! His technical/development background enable him to talk as a peer to peers with our developer users. You’ll read his tips and advices on performance management with Blackfire. And he’ll support you as a day-to-day user.