Behat 3 Tables and TableNode Examples

Behaviour Driven Development (BDD) with Behat 3 is a thing of beauty. When combined with PHPSpec you get something I am hugely excited about.

However, there are many ways in which BDD can be daunting not just for the new-comer, but for a new project in general.

Once you have a feature or two written up, copy / pasting and doing a little editing can yield quick results but writing the code that lives in the underlying Feature Context can be a little harder. For me, this was most evident when dealing with Scenarios that contained tabular data.

I made myself a little Behat TableNode cheat sheet to help – at a glance, and whilst in my IDE (PHPStorm btw) – figure out just what might be in my TableNode objects for the specific methods available on that class.

This is an example of the scenario I was working with:

Behat 3 TableNode var_dump

  Background:
    Given the product with id: 3 has the following values:
      | asin       | title         | money | currency | description               |
      | CCCCC33333 | third product | 33.33 | GBP      | third product description |

And a dump of the resulting TableNode:

var_dump($table);

      │ class Behat\Gherkin\Node\TableNode#3316 (2) {
      │   private $table =>
      │   array(2) {
      │     [10] =>
      │     array(5) {
      │       [0] =>
      │       string(4) "asin"
      │       [1] =>
      │       string(5) "title"
      │       [2] =>
      │       string(5) "money"
      │       [3] =>
      │       string(8) "currency"
      │       [4] =>
      │       string(11) "description"
      │     }
      │     [11] =>
      │     array(5) {
      │       [0] =>
      │       string(10) "CCCCC33333"
      │       [1] =>
      │       string(13) "third product"
      │       [2] =>
      │       string(5) "33.33"
      │       [3] =>
      │       string(3) "GBP"
      │       [4] =>
      │       string(25) "third product description"
      │     }
      │   }
      │   private $maxLineLength =>
      │   array(5) {
      │     [0] =>
      │     int(10)
      │     [1] =>
      │     int(13)
      │     [2] =>
      │     int(5)
      │     [3] =>
      │     int(8)
      │     [4] =>
      │     int(25)
      │   }
      │ }

var_dump($table->getRowsHash());

      │ array(2) {
      │   'asin' =>
      │   array(4) {
      │     [0] =>
      │     string(5) "title"
      │     [1] =>
      │     string(5) "money"
      │     [2] =>
      │     string(8) "currency"
      │     [3] =>
      │     string(11) "description"
      │   }
      │   'CCCCC33333' =>
      │   array(4) {
      │     [0] =>
      │     string(13) "third product"
      │     [1] =>
      │     string(5) "33.33"
      │     [2] =>
      │     string(3) "GBP"
      │     [3] =>
      │     string(25) "third product description"
      │   }
      │ }

var_dump($table->getColumnsHash());

      │ array(1) {
      │   [0] =>
      │   array(5) {
      │     'asin' =>
      │     string(10) "CCCCC33333"
      │     'title' =>
      │     string(13) "third product"
      │     'money' =>
      │     string(5) "33.33"
      │     'currency' =>
      │     string(3) "GBP"
      │     'description' =>
      │     string(25) "third product description"
      │   }
      │ }

Hopefully this is as useful a reference to you as it has become for me. Being able to quickly ‘guess’ what is going to be in my TableNode objects and where has helped save me a good deal of time already.