# BDD With Behat

The BDD PHP framework Behat, allows you to test your PHP applications using human-readable sentences to write features and scenarios about how your applications should behave in order to test out its functionality. We can then run these tests to see if our application is behaving as expected. Let's spend about 15 minutes quickly going over Behat's installation process and learn how we can test our PHP applications behavior using the basics.

## Installation

To begin testing with Behat, we just need to install it on our local development machine. I'll be using Composer, but you can take a look at a the documentation for additional installation methods.

Within your PHP application (I have a blank PHP app named phpbehat created in advance) create a composer.json file with the following code to load in Behat:

In your terminal we can install it using the following command:

We should now be able to run the bin/behat command to work with Behat.

## Creating Features

We always begin by creating a new feature. A feature is something that we can use to describe a feature of our application and then implement it to get the test to pass.

A feature, at its simplest, consists of:

• Uses a .feature extension.
• Contains the feature's benefit, role, and the feature itself.
• May hold a list of scenarios.

Now I can't show you how to test all possible features for an application, as that would take far too long. But what I can do is show you how to go about writing a basic feature and you can adapt this code to make it work for your specific application.

To avoid complicating the learning process, let's create a very simple Phpadder.php file which contains a couple methods that we can use to add two numbers together and display their sum.

### The "Then" Step

This step starts off by checking if the actual sum (retrieved using our \$this->Adder object and its sum property) is not equal to the expected sum. If this evaluates to true that means we need to have Behat show a failure. To do so, we'll just throw a new exception displaying the actual sum so we can compare. Otherwise, we call our display method.

Time to run the tests.

## Running the Tests

Now that we have our features, scenarios, and steps laid out, let's run our tests using the following command:

You should see the following success messages inside of your terminal:

You can ensure that your tests are running correctly, by simply breaking something in your Phpadder.php file so that it doesn't run quite the same way your test expects it to. For example, if we change the add method to instead use subtraction, like so:

And then we rerun our tests: bin/behat. You can see in our terminal, we now have a failing test, because it's no longer adding our numbers but subtracting them:

## Conclusion

In conclusion, you can follow a small four step process for testing your PHP apps using Behat:

1. Define a Feature
2. Define a Scenario
3. Define Step Definitions
4. Run the tests using bin/behat

So, with just that small amount of code, we should now have a basic understanding of how to work with Behat to test our PHP applications. For more information about working with Behat please checkout their documentation.