spatie/phpunit-snapshot-assertions

snapshotConstraint proposal

Closed this issue · 0 comments

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.