madewithlove / phpunit-snapshots
Snaphot testing for PHPUnit
Installs: 17 331
Dependents: 1
Suggesters: 0
Security: 0
Stars: 16
Watchers: 5
Forks: 3
Open Issues: 1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- phpunit/phpunit: ^5.7
- symfony/var-dumper: ^3.2
This package is auto-updated.
Last update: 2022-02-01 13:03:38 UTC
README
This trait allows you to use Jest-like snapshot testing in your PHPUnit tests.
It is a very basic trait and is only meant to snapshot JSON-encodable structures, not complex objects and such.
Installation
composer require madewithlove/phpunit-snapshots
Usage
Using snapshots in tests
Simply call the assertion on any encodable result (the result of a function, a variable, etc.). You can pass an identifier as second argument which will be used as title of the snapshot in the snapshot file.
<?php class MyTestCase extends \PHPUnit_Framework_TestCase { use \Madewithlove\PhpunitSnapshots\SnapshotAssertions; public function testSomething() { $this->assertEqualsSnapshot($this->someComplexOperation()); $this->assertEqualsSnapshot($this->someComplexOperation(), 'Compute something'); } }
This will generate a snapshot if we didn't have one for this test, else it will assert that the current results match the ones in the snapshot.
Updating all snapshots
You can update all snapshots in your tests by running the following:
$ phpunit -d --update
Testing
$ composer test
License
The MIT License (MIT). Please see License File for more information.