Snapshot testing with PHPUnit

use Spatie\Snapshots\MatchesSnapshots;

class OrderTest
{
    use MatchesSnapshot;

    class test_it_casts_to_json()
    {
        $orderId = new Order(1);

        $this->assertMatchesJsonSnapshot($order->toJson());
    }
}

Again great work by the folks at Spatie:

The difference between a classic assertEquals and an assertMatchesSnapshot is that you don’t write the expectation yourself when snapshot testing. When a snapshot assertion happens for the first time, it creates a snapshot file with the actual output, and marks the test as incomplete. Every subsequent run will compare the output with the existing snapshot file to check for regressions.

A Package for Snapshot Testing in PHPUnit →
spatie/phpunit-snapshot-assertions (GitHub) →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.