Download the PHP package goldquality/phpunit-snapshots without Composer

On this page you can find all versions of the php package goldquality/phpunit-snapshots. 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-snapshots

PHPUnit Snapshots

Snapshot testing is a testing technique that captures the output of a function/component/endpoint and saves it as a reference.

On subsequent test runs, the output is compared against the saved reference to ensure consistency and catch unexpected changes.


PAINLESS Testing

Testing the results of API responses can often be a tedious and monotonous task. It requires meticulously checking for specific fields, validating values, and ensuring the structure aligns with expectations. While powerful tools like Laravel's built-in testing utilities or Symfony's equivalent approaches provide fine-grained control, they tend to involve verbose and repetitive code.

Here's a classic example from Laravel's official documentation:

Here's how you can simplify the same test:

Instead of manually asserting each field and its structure, this approach stores the response in a snapshot file (e.g., test_response_ok_0.json) the first time the test runs. Future test runs will compare the current response to the saved snapshot, ensuring consistency. If the response changes intentionally, updating the snapshot is a simple process, saving significant time and effort.

Benefits


Comparison with competitors

phpunit-snapshots vs spatie/phpunit-snapshot-assertions:

Installation

Configuration

  1. Create or add to existing BaseTestCase e.g. ApiTestCase

see example implementation

Usage

  1. Extend TestCase: Extend the ApiTestCase .
  2. Invoke assert method: Use the assertSnapshot() or assertResponseSnapshot() method in your test cases with the response content as an argument.
  3. Run your tests. On the initial test run, a snapshot file is created with the JSON response content.

Example of a generated snapshot file test_response_ok_0.json:

  1. Edit Snapshot: Open the created file and remove the protective line "DELETE THIS ROW" at the top.
  2. Masks (Optional): Add masks to manage auto-generated values.
  3. Re-run Tests: Verify that the response contents match your established snapshots.

This method allows developers to ensure their application's output remains consistent across updates and refactoring, enhancing test coverage and reliability with minimal effort.


Examples

Symfony Example:

Laravel Example:

Available masks


Advanced configuration

If you need more control you can extend SnapshotHandler and implement as you need.

Testing


FAQ

How do snapshot files work?

The first time a test using assertSnapshot() is run, a snapshot file (in JSON format) is created, capturing the component's or function's output. In subsequent runs, the output is compared against this file. If the outputs differ, the test will fail, alerting you to unintended changes.

My test fails due to a mismatch with the snapshot; what now?

If a test fails because the output does not match the snapshot, you should:

  1. Review the differences to determine if they are expected changes.
  2. If the changes are intended, manually edit the snapshot file or delete snapshot file and re-run the test to generate a new snapshot.
  3. If the changes are unintended, investigate and fix the underlying issue in your application.

What are masks, and how do I use them?

Masks are patterns used in snapshot files to handle dynamic values (e.g., timestamps, IDs) that might change between test runs. For example, @integer@ can be used for dynamic integer values. After the first test run, you can edit the snapshot to include these masks.

Why do I need to delete the "DELETE THIS ROW" line in the snapshot?

The "DELETE THIS ROW" line serves as a protective line to remind developers to review and edit the snapshot file after its initial creation. You should remove this line to finalize the snapshot structure.

Can I customize the naming of snapshot files?

Currently, the naming of snapshot files automatically based on your test case names.


All versions of phpunit-snapshots with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
ext-json Version *
coduo/php-matcher Version >=5
phpunit/phpunit Version >=9.6
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 goldquality/phpunit-snapshots contains the following files

Loading the files please wait ....