BDD with Behat and Mink

And Composer!

Anthony Shull

  • Developer at Rock River Star
  • I love *nix, tmux, and vim
  • Falo um poquinho de português
  • I have a (standard) poodle named Acadia

The Application — Colossus

  1. Accept POST requests with JSON alerts
  2. Display alerts in a dashboard

Composer — Dependency Manager

Think Maven, Pip, Rubygems, NPM but for PHP.

Project Structure


                "require": {
                  "silex/silex" : "1.0.*@dev",
                  "twig/twig" : "1.*",
                  "symfony/twig-bridge" : "~2.3",

                  "behat/behat" : "2.4.*@stable",
                  "behat/mink" : "1.4@stable",
                  "behat/mink-extension": "*",
                  "behat/mink-goutte-driver": "*"
                "config" : {
                  "bin-dir" : "bin/"

Download dependencies

              :> composer install

Behat and Mink

  • Behat — good for command line programs
  • Mink — necessary for web acceptance tests


* this goes in the project root directory

                    base_url:  ""
                    goutte: ~

Initialize Behat

              :> bin/behat --init

Let's Write Some Features!

Well, at least two...

Features are written in Gherkin. It's the same language that Cucumber uses.


              Feature: Alert
                In order to be aware of problems with our applications
                As a systems administrator
                I need to be able to receive alerts and display them

              Scenario: View alerts
                Given I am on the homepage
                Then I should see a "table" element

              Scenario: Post alerts
                Given I send a JSON alert to the homepage
                Then I should receive a JSON response OK

Run Behat

              :> bin/behat
              2 Scenarios (1 passed, 1 undefined)
              2 Steps (2 passed, 2 undefined)


              use Behat\MinkExtension\Context\MinkContext;

              class FeatureContext extends MinkContext
                 * @Given /^I send a JSON alert to the homepage$/
                public iSendAJsonAlertToTheHomepage()
                  // To fail throw new Exception();
                  // Otherwise the tests will pass
                 * @Then /^The response JSON should be OK$/
                public theResponseJsonShouldBeOk() 
                  // We expect a JSON response of { "Status" : "OK" } 

Run Behat Again

              :> bin/behat

              2 scenarios (2 passed)
              4 steps (4 passed)


            :> bin/behat -dl

Extensions allow you to write features without needing to define them. They are nice.

Actually, Mink is an extension you're already using them.