Download the PHP package spatie/phpunit-snapshot-assertions without Composer

On this page you can find all versions of the php package spatie/phpunit-snapshot-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 phpunit-snapshot-assertions

Snapshot testing with PHPUnit

Latest Version on Packagist run-tests Total Downloads

Snapshot testing is a way to test without writing actual test cases.

You can learn more in this free video from our Testing Laravel course. Don't worry you can use this package in non-Laravel projects too.

On the first run, the test runner will create a new snapshot.

On subsequent runs, the test will pass as long as the snapshot doesn't change.

If there's a regression, the test will fail!

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:

Usage

To make snapshot assertions, use the Spatie\Snapshots\MatchesSnapshots trait in your test case class. This adds a set of assertion methods to the class:

Snapshot Testing 101

Let's do a snapshot assertion for a simple string, "foo".

The first time the assertion runs, it doesn't have a snapshot to compare the string with. The test runner generates a new snapshot and marks the test as incomplete.

Snapshot ids are generated based on the test and testcase's names. Basic snapshots return a plain text or YAML representation of the actual value.

Let's rerun the test. The test runner will see that there's already a snapshot for the assertion and do a comparison.

If we change actual value to "bar", the test will fail because the snapshot still returns "foo".

When we expect a changed value, we need to tell the test runner to update the existing snapshots instead of failing the test. This is possible by adding a-d --update-snapshots flag to the phpunit command, or setting the UPDATE_SNAPSHOTS env var to true.

As a result, our snapshot file returns "bar" instead of "foo".

File snapshots

The MatchesSnapshots trait offers two ways to assert that a file is identical to the snapshot that was made the first time the test was run:

The assertMatchesFileHashSnapshot($filePath) assertion asserts that the hash of the file passed into the function and the hash saved in the snapshot match. This assertion is fast and uses very little disk space. The downside of this assertion is that there is no easy way to see how the two files differ if the test fails.

The assertMatchesFileSnapshot($filePath) assertion works almost the same way as the file hash assertion, except that it actually saves the whole file in the snapshots directory. If the assertion fails, it places the failed file next to the snapshot file so they can easily be manually compared. The persisted failed file is automatically deleted when the test passes. This assertion is most useful when working with binary files that should be manually compared like images or pdfs.

Image snapshots

The assertImageSnapshot requires the spatie/pixelmatch-php package to be installed.

This assertion will pass if the given image is nearly identical to the snapshot that was made the first time the test was run. You can customize the threshold by passing a second argument to the assertion. Higher values will make the comparison more sensitive. The threshold should be between 0 and 1.

Customizing Snapshot Ids and Directories

Snapshot ids are generated via the getSnapshotId method on the MatchesSnapshot trait. Override the method to customize the id. By default, a snapshot id exists of the test name, the test case name and an incrementing value, e.g. Test__my_test_case__1.

Example: Replacing the __ Delimiter With --

By default, snapshots are stored in a __snapshots__ directory relative to the test class. This can be changed by overriding the getSnapshotDirectory method.

Example: Renaming the __snapshots__ directory to snapshots

Using specific Drivers

The driver used to serialize the data can be specificied as second argument of the assertMatchesSnapshot method, so you can pick one that better suits your needs:

Writing Custom Drivers

Drivers ensure that different types of data can be serialized and matched in their own way. A driver is a class that implements the Spatie\Snapshots\Driver interface, which requires three method implementations: serialize, extension and match.

Let's take a quick quick look at the JsonDriver.

Drivers can be used by passing them as assertMatchesSnapshot's second argument.

Usage in CI

When running your tests in Continuous Integration you would possibly want to disable the creation of snapshots.

By using the --without-creating-snapshots parameter or by setting the CREATE_SNAPSHOTS env var to false, PHPUnit will fail if the snapshots don't exist.

Usage with parallel testing

If you want to run your test in parallel with a tool like Paratest, ou with the php artisan test --parallel command of Laravel, you will have to use the environment variables.

A note for Windows users

Windows users should configure their line endings in .gitattributes.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

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


All versions of phpunit-snapshot-assertions with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
ext-dom Version *
ext-json Version *
ext-libxml Version *
composer-runtime-api Version ^2.0
phpunit/phpunit Version ^10.0|^11.0
symfony/property-access Version ^5.2|^6.2|^7.0
symfony/serializer Version ^5.2|^6.2|^7.0
symfony/yaml Version ^5.2|^6.2|^7.0
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/phpunit-snapshot-assertions contains the following files

Loading the files please wait ....