Spatie recently released a tabular-assertions package you can use to write tabular assertions with Pest or PHPUnit. Inspired by Jest's test.each table syntax (i.e., "table as a data provider"), "Tabular assertions allow you to describe data in a Markdown table-like format and compare it to the actual data":
test('it compares a table', function () { $order = Order::factory() ->addItem('Pen', 2) ->addItem('Paper', 1) ->addItem('Pencil', 5) ->create(); expect($order->items)->toMatchTable(' | #id | #order_id | name | quantity | | #1 | #1 | Pen | 2 | | #2 | #1 | Paper | 1 | | #3 | #1 | Pencil | 5 | ');});
This package works with both Pest and PHPUnit, and you'll notice the immediate benefit of formatting tabular data in a way that is immediately readable. In my experiments with this package thus far, writing assertions for datasets in this way also feels fun. Some of the main features this package will bring to your test suite include the following:
- Hand-write expectations in an easy-to-read tabular format
- Error messages are clear
- Works for both Pest and PHPUnit
- Supports custom assertions
- Support for dynamic values
You can learn more about this package, get full installation instructions, and view the source code on GitHub. If you'd like to learn more about the background of this package, the author, Sebastian De Deyne, wrote about Introducing tabular assertions.