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

http://getcomposer.org

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

Project Structure

composer.json


              {
                "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

http://www.behat.org

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

behat.yml

* this goes in the project root directory

              default:
                extensions:
                  Behat\MinkExtension\Extension:
                    base_url:  "http://colossus.dev"
                    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.

alert.feature


              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)
            

FeaturesContext.php


              [...]
              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)
            

Extensions

http://extensions.behat.org/


            :> bin/behat -dl
          

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

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

Thanks!

http://www.rockriverstar.com/presentations/bdd/index.html