Download the PHP package spatie/tabular-assertions without Composer

On this page you can find all versions of the php package spatie/tabular-assertions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package tabular-assertions

Write tabular assertions with Pest or PHPUnit

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Tabular assertions allow you to describe data in a Markdown table-like format and compare it to the actual data. This is especially useful when comparing large, ordered data sets like financial data or a time series.

With Pest:

With PHPUnit:

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

Why tabular assertions?

Tabular assertions have two major benefits over other testing strategies: expectations are optimized for readability & failed assertions can display multiple errors at once.

1. You can hand-write expectations that contain a lot of data and are optimized for readability. Text-based tables are compact, allow you to compare the data in two dimensions.

The alternative would be to write multiple assertions.

Expectations require you to assert each property individually. This makes it hard to see all dates at a glance, and is less readable in general.

Associative arrays require a lot of repetition with labels.

Arrays without keys can't be aligned properly (manually maintained spaces would be striped by code style fixers). This becomes unclear when asserting multiple columns with different lengths.

With tabular assertions, we get a compact, readable overview of the data, and because it's stored in a single string code style fixers won't reformat it.

2. Errors that can display multiple problems. With separate expectations, tests fail on the first failed assertion which means you don't have the full picture (small issue vs. everything broken)

If you serialize two datasets to a table, you can get a nice output in a visual diff like PhpStorm's output when you use assertEquals.

In this assertions, you can see one value is wrong and one row is missing in one glance. With separate assertions, you only see the first error your test runner comes across.

CleanShot 2023-02-09 at 14 48 38@2x

This style of testing really shines when you have a lot of data to assert. This example has 9 rows and 9 columns, which means we're comparing 81 data points while keeping it all readable.

Usage

Basic usage: Pest

With Pest, the plugin will be autoloaded and readily available. Use the custom toMatchTable() expectation to compare data with a table.

Basic usage: PHPUnit

With PHPUnit, add the Spatie\TabularAssertions\PHPUnit\TabularAssertions trait to the tests you want to use tabular assertions with. Use $this->assertMatchesTable() to compare data with a table.

Dynamic values

Sometimes you want to compare data without actually comparing the exact value. For example, you want to assert that each person is in the same team, but don't know the team ID because the data is randomly seeded on every run. A column can be marked as "dynamic" by prefixing its name with a #. Dynamic columns will replace values with placeholders. A placeholder is unique for the value in the column. So a team with ID 123 would always be rendered as #1, another team 456 with #2 etc.

For example, Sebastian & Freek are in team Spatie which has a random ID, and Christoph is in team Laravel with another random ID.

Custom assertions

Tabular assertions will cast the actual values to strings. We're often dealing with data more complex than stringables, in those cases it's worth creating a custom assertion method that prepares the data.

Consider the following example with a User model that has an id, name, and date_of_birth which will be cast to a Carbon object.

Because Carbon objects automatically append seconds when stringified, our table becomes noisy. Instead, we'll create a custom toMatchUsers assertion to prepare our data before asserting.

In PHPUnit, this would be a custom assertion method.

This can also useful for any data transformations or truncations you want to do before asserting. Another example: first_name and last_name might be separate columns in the database, but in assertions they can be combined to reduce unnecessary whitespace in the table.

Inspiration & alternatives

The idea for this was inspired by Jest, which allows you to use a table as a data provider.

Snapshot testing is also closely related to this. But snapshots aren't always optimized for readability, are stored in a separate file (not alongside the test), and are hard to write by hand (no TDD).

Testing

Tests are written with Pest. You can either use Pest's CLI or run composer test to run the suite.

In addition to tests, PhpStan statically analyses the code. Use composer analyse to run PhpStan.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of tabular-assertions with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spatie/tabular-assertions contains the following files

Loading the files please wait ....