spatie / phpunit-snapshot-assertions

A way to test without writing actual test cases
https://spatie.be/courses/testing-laravel-with-pest/snapshot-testing
MIT License
642 stars 71 forks source link

snapshotConstraint proposal #137

Closed Firtzberg closed 2 years ago

Firtzberg commented 2 years ago

This package is great! I just wanted to share some thoughts.

Inside tests using MatchesSnpashots trait I started adding this handy function.

    public function snapshotConstraint(): Constraint
    {
        return self::callback(function ($argument) {
            $this->assertMatchesJsonSnapshot([$argument]);
            return true;
        });
    }

Within a test case using mocks where I don't care what the parameter value exactly is, but want to make sure it didn't change I do the following

        // Find record in cache
        $this->redisMock->expects(self::once())
            ->method('get')
            // Compare key to key snapshot
            ->with($this->snapshotConstraint())
            ->willReturn(json_encode($data, JSON_THROW_ON_ERROR));

I guess this can be implemented in a better way by defining a new constraint class extending the PHPUnit Constraint.