Integrating Symfony2 with Behat¶
Symfony2 is a Web Application Framework that can be easily integrated and used seamlessly with Behat 3. As a prerequisite for this cookbook you need to have working Symfony2 application.
In this cookbook we will cover:
- Installing Behat dependency with Composer.
- Initialising Behat suite.
- Installing and enabling Symfony2 extension.
- Accessing application services in contexts.
- Using Symfony2 test client as a Mink driver.
Installing Behat in your Symfony2 Project¶
Recommended way of managing Behat dependency in your project is to use Composer. Assuming that you already have composer.json file in your project you only need to add one new entry to it and install. It can be done automatically for you with this command:
$ php composer.phar require --dev behat/behat
Note that we have used --dev switch for Composer. It means that Behat will be installed as a require-dev dependency in your project, and will not be present in production. For further information please check Composer documentation.
After execution of this command you should see information about files initialised in your project, and you should be able to write your first scenario. In order to verify Behat initialisation you can just run following command:
If the command above doesn’t work, you may need to run bin/behat instead.
Next, let’s initialize Behat:
$ vendor/bin/behat --init
If you don’t feel familiar with Behat enough please read Quick Start first.
Installing and Enabling Symfony2 Extension¶
Great, you have a Behat suite working in your project, now it’s time to install Symfony2Extension. To do this you need to add another dependency, but in the same way we did it a while ago:
$ php composer.phar require --dev behat/symfony2-extension
Now it’s time to enable extension in your behat.yml file. If it doesn’t exist just create such file in your project root and fill it with following content:
If this file already exists just change its contents accordingly. From that point you should be able to run Behat and Symfony2 extension will be loaded and ready to work with.
Accessing Application Services in Contexts¶
The extension we have just installed detects the default Symfony configuration and allows to use your application services in context classes. To make a service available in a context you need to change your behat.yml configuration and tell the extension which services to inject:
This configuration will try to to match the $session dependency of your FeatureContext constructor by injecting the session service into the context. Be careful because if such a service does not exist or its name does not match, it will not work and you will end up with a Behat exception.
Accessing Symfony2 parameters in behat.yml¶
The extension makes it possible to use Symfony’s parameters. To make a Symfony parameter available in a context you need to change your behat.yml configuration and tell the extension which parameter to inject:
This configuration will try to match the $mySymfonyParam dependency of your FeatureContext constructor by injecting the my.symfony_param parameter into the context. If the parameter does not exists, you will get a Behat exception. The parameters needs to be quoted (double %) for escaping because the Behat configuration is also using Symfony’s DIC and supports Behat’s parameters injection.
Using KernelDriver with your Behat Suite¶
Symfony2 has a build-in Test Client, which can help you with web acceptance testing, why not make use of it? Especially because Behat has a Mink Extension that makes those kind of testing even easier.
The advantage of using KernelDriver instead of standard Mink driver is that you don’t need to run web server in order to access a page. Also you can even use Symfony Profiler and inspect your application directly!. You can read more about test client in Symfony Documentation.
If you don’t have Mink and MinkExtension yet, you can install those two with:
$ php composer.phar require --dev behat/mink
$ php composer.phar require --dev behat/mink-extension
In order to install BrowserKit Driver you need to execute following command:
$ php composer.phar require --dev behat/mink-browserkit-driver
Now you are only one step from being ready to make full use of Symfony2 extension in your project. You need to enable extension in your behat.yml file as follows:
Et voilà! Now you are ready to drive your Symfony2 app development with Behat3!
- Previous chapter
- Next chapter
- Accessing Contexts from each other