php 🚀
laravel 🚀
bdd 🚀
behat 🚀
phpunit 🚀
Example of Mixing it up Behat BDD and PHPUnit

Just wanted to show two things here. One is how sometimes I end up in PHPUnit to work out some ideas. And two how awesome it is to refactor thanks to PHPUnit.

In this example I had a Class responsible for saving data, we'll call FooSaveData. But before that save it has to process some incoming information against another set of data to save the results of that. So I made another class we'll call FooProcessData.

The BDD Story for this worked out well in that just like in Konstantin Kudryashov noted in this article I had a great reading business driven gherkin feature that I then, using Behat 3 and Suites was able to make some really great names for my classes and methods.

But this was fine as I was working on FooSaveData, but the fact that FooProcessData had to iterate over a large complex array of data I did not want to do inside of this Gherkin test. It would mean that I had to deal with 5 other steps, x number of model states I needed etc. Instead I just made a unit test for that class "FooProcessData" and focused on it for a bit outside the rest of this work.

To begin with I just use

File::put(base_path('tests/fixtures/data_to_process.json', json_encode($some_data, JSON_PRETTY_PRINT))

to put that data right where I want it for my unit tests.

Then from there I go around and around unil the thinking on the left reads more like the thinking on the right

start to end

Which is my favorite part of this really as I see code evolve to something I can come back 6 months later and read.

Let's Plan for a Future Where We're All As Stupid as We Are Today - Dan Milstein

At this point that "lego" like piece is done and I can go back to my Behat BDD Gherkin test to finish off where I left off.