Skip to content

Commit

Permalink
added option --without-creating-snapshots (#18)
Browse files Browse the repository at this point in the history
added option --without-creating-snapshots
  • Loading branch information
n0-m4d authored Apr 14, 2020
1 parent 21a9e2e commit 533159b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,16 @@ The library currently supports the following wildcards:
- DateTimeWildcard
- DateTimeOrNullWildcard
- StringWildcard

### Usage in CI
When running your tests in Continuous Integration you would possibly want to disable the creation of snapshots.

By using the --without-creating-snapshots parameter, PHPUnit will fail if the snapshots don't exist.

```
> ./vendor/bin/phpunit -d --without-creating-snapshots
1) ExampleTest::test_it_matches_a_string
Snapshot "ExampleTest__test_it_matches_a_string__1.txt" does not exist.
You can automatically create it by removing `-d --without-creating-snapshots` of PHPUnit's CLI arguments.
```
27 changes: 27 additions & 0 deletions src/MatchesSnapshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ public function assertMatchesXmlSnapshot(string $actual, array $wildcards = [])
$this->doSnapshotAssertion($actual, new XmlDriver(), $wildcards);
}

/**
* Determines whether or not the snapshot should be created instead of
* matched.
*
* Override this method if you want to use a different flag or mechanism
* than `-d --without-creating-snapshots`.
*/
protected function shouldCreateSnapshots() : bool
{
return ! in_array('--without-creating-snapshots', $_SERVER['argv'], true);
}

/**
* // phpcs:disable
* @param bool $withDataSet
Expand Down Expand Up @@ -138,6 +150,8 @@ private function doSnapshotAssertion(string $actual, Driver $driver, array $wild
);

if (!$snapshotHandler->snapshotExists($snapshot)) {
$this->assertSnapshotShouldBeCreated($filename);

$this->createSnapshotAndMarkTestIncomplete($snapshot, $actual);
}

Expand Down Expand Up @@ -202,4 +216,17 @@ private function registerSnapshotChange(string $message): void
{
$this->snapshotChanges[] = $message;
}

protected function assertSnapshotShouldBeCreated(string $snapshotFileName): void
{
if ($this->shouldCreateSnapshots()) {
return;
}

$this->fail(
"Snapshot \"$snapshotFileName\" does not exist.\n".
'You can automatically create it by removing '.
'`-d --without-creating-snapshots` of PHPUnit\'s CLI arguments.'
);
}
}

0 comments on commit 533159b

Please sign in to comment.