PHPUnit with YAML

After diving into some database integration testing, I found that my data model was incompatible in a XML format. Using hash tags is a no-no, as it’s against XML specifications. Well darn, what is a coder to do? YAML to the rescue.

First, what is YAML? Besides it being fun to say, it’s a “human friendly data serialization language”. HFDSL doesn’t sound as cool as YAML, and just like PHP, you shouldn’t look to far into the acroymn, less you are fond of infinite loops.

“YAML is a recursive acronym for “YAML Ain’t Markup Language“. Early in its development, YAML was said to mean “Yet Another Markup Language“,[3] but was retronymed to distinguish its purpose as data-oriented, rather than document markup.” – Wikipedia

Fantastic, moving on…

PHPUnit uses YAML as a supported format for creating datasets. Tables, columns, and rows are split up using colons, dashes and spacing to define your database objects. For example, let’s say we have a dataset of books.

books: #table name
- #begin a new record, followed by key: value pair
id: 1
title: Moby Dick
author: Herman Melville
id: 2
title: The Hobbit
author: J. R. R. Tolkien

This will add two rows to the table books with its respective data.

YAML can definitely be easier to read in some scenarios, as there is less to read. After using it for a couple months, it has kind of grown on me as well. It is a nice format to use  for config files when XML is overkill.