blog

  • Home
  • blog
  • BDD in Laravel: Getting Started with Behat and PhpSpec

BDD in Laravel: Getting Started with Behat and PhpSpec

Getting Started with BDD in Laravel

Introduction

BDD is a complicated subject for many developers, and getting started with it the right way often does not come easy – especially when needing to implement it into existing frameworks. This tutorial aims to help you get a BDD-powered Laravel project up and running in very little time, introducing you to the basic concepts and workflow you’ll need to proceed on your own. We’ll be installing and using Behat and PhpSpec.

In the tutorial, we assume you’re working on a Unix system and have basic theoretical knowledge of what BDD is about, but little or no practical experience.

We’ll also assume that us saying “Run the command” implies the command should be run in the terminal of the operating system.

Prerequisites

Optionally, if you intend to build what we set up here into a proper application, add in:

  • a database (MySQL)
  • Caching layers (Redis, Memcached…)

Creating a New Laravel App

To create a new Laravel application, we run the following command:

composer create-project laravel/laravel bdd-setup

The sample application is now created, and should greet you with “Laravel 5” if you visit the root of the app.

Laravel Greeting Screen

Setting up Behat

Several packages are required in order to make Behat play well with Laravel. Let’s install them all into our application’s development environment (with the --dev flag) and explain each.

composer require behat/behat behat/mink behat/mink-extension laracasts/behat-laravel-extension --dev
sudo ln -s /home/vagrant/Code/bdd-setup/vendor/bin/behat /usr/local/bin/behat

behat/behat is the main package for Behat. The behat/mink package is used to emulate a browser, so we can have the test suite check our URLs and their output. behat/mink-extension is the glue for Mink and Behat, and the last package, behat-laravel-extension is Jeffrey Way’s own implementation of Behat bindings, specifically made for Laravel.

The last sudo ln -s line is optional, and adds Behat’s executable to a location in the $PATH, so the behat command can be executed without the vendor/bin prefix from our project’s root folder. In other words, behat --init instead of vendor/bin/behat --init.

Continue reading %BDD in Laravel: Getting Started with Behat and PhpSpec%

LEAVE A REPLY