How To Configure Behat with Symfony 4
Every project needs tests to maintain the code stability and help eliminate potential bugs immediately. When building REST API, you need to test its functionality. One of the great tools in the PHP world is Behat. It is an open-source BDD framework for auto-testing your business expectations which is using Gherkin language.
In this article, I will cover step by step guide on how to configure Behat with Symfony 4 framework to test JSON REST API.
Assumed you already have a Symfony 4 application and REST API running (for example using FOSRestBundle).
Installing and configuring Behat
composer req behat/behat --dev
Note: The —-dev
flag indicates that the package will be added to the require-dev
section in your composer.json
file.
Note: No need to run behat --init
, see Installing Behat Symfony2 Extension section below to find out why.
Install Behat Mink
Mink is an open source browser emulator for web applications. It is used to simulate the interaction between browser and our REST API application to check if it works correctly when running tests.
Install it by running command:
composer req behat/mink --dev
Installing Behat Symfony2 Extension
Note: it might be confusing that it is named Symfony2 extension, although it works fine with Symfony 2+ versions.
Symfony2Extension is an integration layer between Behat 3+ and Symfony2+ frameworks which provides a complete integration with Symfony2+ bundles structure, an initialized and booted kernel instance for your contexts and additional symfony2
Mink driver, if the Mink Extension is installed (see Installing Behat Mink Extension section below).
Install Symfony2Extension using Composer:
composer req behat/symfony2-extension --dev
Thanks to Symfony Flex and its recipes approach, it will automatically create a basic structure, a directory called features
in your main project dir with the default FeatureContext
class and bootstrap
directory containing bootstrap.php
file.
Additionally, an example demo.feature
feature file will be created inside features
directory and Behat configuration file called behat.yml.dist
in your main project directory. The example configuration will look like:
Copy behat.yml.dist
to behat.yml
which you will need to edit later on.
Installing Mink BrowserKit Driver
BrowserKitDriver provides a bridge for the Symfony BrowserKit component. BrowserKit is a browser emulator provided by the Symfony project.
Run command:
composer req behat/mink-browserkit-driver --dev
BrowserKit will be used now to emulate out functional tests.
Installing Behat Mink Extension
MinkExtension is an integration layer between Behat 3.0+ and Mink 1.5+ and it provides:
- Additional services for Behat (
Mink
,Sessions
,Drivers
).-
Behat\MinkExtension\Context\MinkAwareContext
which providesMink
instance for your contexts.- Base
Behat\MinkExtension\Context\MinkContext
context which provides base step definitions and hooks for your contexts or subcontexts. Or it could be even used as context on its own.
To install it, run Composer command:
composer req behat/mink-extension --dev
And adjust the Behat config file:
Thanks to this extension we can easily use Mink together with Behat and run functional tests.
Installing Behatch Contexts
Behatch Contexts is a package with most commonly used steps. It is an extension which provides, among others, a Behat context such as JSON, XML, REST, debug etc. It requires Mink and Mink Extension to be installed.
Install it by running the following command:
composer req behatch/contexts --dev
In behat.yml
file enable installed Behatch Contexts. In this case, behatch:context:json
and behatch:context:rest
context will be used to test JSON REST API. Also,Behatch\Extension
extension should be activated:
You can now create your first feature inside features
directory:
and run it by executing command:
APP_ENV=test bin/behat
Behat gives you a lot of power over the functional testing. Writing new features has never been so easy.
Enjoy writing your tests!
If you need to deploy Symfony 4 app quickly to production I recommend using one of the cheap VPS servers by DigitalOcean. By signing up from here you would receive $10 free credit for your droplet.
Before you go…
If you liked this article, tap the 👏 below so other people would see it here on Medium.