PHPackages                             goldquality/phpunit-snapshots - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. goldquality/phpunit-snapshots

ActiveLibrary[Testing &amp; Quality](/categories/testing)

goldquality/phpunit-snapshots
=============================

Snapshot testing with PHPUnit

1.0.1(1y ago)8272MITPHPPHP &gt;=8.1CI passing

Since Jan 5Pushed 1y ago2 watchersCompare

[ Source](https://github.com/goldquality/phpunit-snapshots)[ Packagist](https://packagist.org/packages/goldquality/phpunit-snapshots)[ RSS](/packages/goldquality-phpunit-snapshots/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

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
----------------

[](#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:

```
use Illuminate\Testing\Fluent\AssertableJson;

public function test_fluent_json(): void
{
    $response = $this->getJson('/users/1');

    $response
    ->assertJson(fn (AssertableJson $json) =>
        $json->has('meta')
             ->has('users', 3)
             ->has('users.0', fn (AssertableJson $json) =>
                $json->where('id', 1)
                     ->where('name', 'Victoria Faith')
                     ->where('email', fn (string $email) => str($email)->is('victoria@gmail.com'))
                     ->missing('password')
                     ->etc()
             )
    );
}
```

Here's how you can simplify the same test:

```
namespace Tests\Feature;

use Tests\ApiTestCase;

class ExampleTest extends ApiTestCase
{
    public function test_response_ok(): void
    {
        $response = $this->getJson('/users/1');

        // Assert the response snapshot
        $this->assertSnapshot($response->collect()->toArray());   // Asserts that response content matches the test_response_ok_0.json
        // or $this->assertResponseSnapshot($response);
    }
}
```

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
--------

[](#benefits)

- **Speed:** Quickly create tests without manually managing output assertions.
- **Flexibility:** Combine with another asserts, like `assertDatabaseHas`, `assertQueue`, `assertCookie` and others.
- **Consistency:** Automatically verifies outputs against previously captured snapshots, reducing human error.
- **Focus on Differences**: When a test fails, you see the exact difference between the current response and the expected snapshot, making debugging faster.
- **Framework Agnostic:** Compatible with popular frameworks like Symfony, Laravel, Yii. Making it versatile for any project setup.

---

Comparison with competitors
---------------------------

[](#comparison-with-competitors)

phpunit-snapshots vs [spatie/phpunit-snapshot-assertions](https://github.com/spatie/phpunit-snapshot-assertions):

- phpunit-snapshots provides a robust array of masks for dynamic values, which allows for a wide set of patterns.
- phpunit-snapshots places a protection line ("DELETE THIS ROW") in newly created snapshots to prompt user review and manual editing, which encourages careful validation of the snapshot’s correctness.

---

Installation
------------

[](#installation)

```
composer require --dev goldquality/phpunit-snapshots
```

Configuration
-------------

[](#configuration)

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

```
