Image for post

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 featuresin 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:

# behat.yml.dist default: suites: default: contexts: – FeatureContext: kernel: ‘@kernel’ extensions: Behat\Symfony2Extension: kernel: bootstrap: features/bootstrap/bootstrap.php class: App\Kernel

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 (MinkSessionsDrivers).

– Behat\MinkExtension\Context\MinkAwareContext which provides Mink 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:

# behat.yml default: suites: default: contexts: – FeatureContext: kernel: ‘@kernel’ – Behat\MinkExtension\Context\MinkContext extensions: Behat\Symfony2Extension: kernel: bootstrap: features/bootstrap/bootstrap.php class: App\Kernel Behat\MinkExtension: base_url: « http://example.com/ » sessions: default: symfony2: ~

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:

# behat.yml default: suites: default: contexts: – FeatureContext: kernel: ‘@kernel’ – Behat\MinkExtension\Context\MinkContext – behatch:context:json – behatch:context:rest extensions: Behat\Symfony2Extension: kernel: bootstrap: features/bootstrap/bootstrap.php class: App\Kernel Behat\MinkExtension: base_url: « http://example.com/ » sessions: default: symfony2: ~ Behatch\Extension: ~

You can now create your first feature inside features directory:

# features/books.feature Feature: Books feature Scenario: Adding a new book When I add « Content-Type » header equal to « application/json » And I add « Accept » header equal to « application/json » And I send a « POST » request to « /api/books/ » with body: «  » » { « title »: « King », « author »: « T. M. Frazier », « enabled »: true } «  » » Then the response status code should be 201 And the response should be in JSON And the header « Content-Type » should be equal to « application/json » And the JSON nodes should contain: | title | King | | author | T. M. Frazier | And the JSON node « enabled » should be true

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!