multiple snapshots

This commit is contained in:
Fabio Ivona
2023-07-27 11:46:22 +02:00
parent b60d21dfe2
commit 1efb9de043
17 changed files with 74 additions and 2 deletions

View File

@ -806,6 +806,7 @@ final class Expectation
public function toMatchSnapshot(string $message = ''): self
{
$snapshots = TestSuite::getInstance()->snapshots;
$snapshots->startNewExpectation();
$testCase = TestSuite::getInstance()->test;
assert($testCase instanceof TestCase);
@ -833,7 +834,7 @@ final class Expectation
} else {
$filename = $snapshots->save($string);
$testCase::markTestIncomplete('Snapshot created at ['.$filename.'].');
//$testCase::markTestIncomplete('Snapshot created at ['.$filename.'].');
}
return $this;

View File

@ -12,6 +12,8 @@ use Pest\TestSuite;
*/
final class SnapshotRepository
{
private static array $expectationsCounter = [];
/**
* Creates a snapshot repository instance.
*/
@ -106,6 +108,33 @@ final class SnapshotRepository
// remove extension from filename
$relativePath = substr($relativePath, 0, (int) strrpos($relativePath, '.'));
return sprintf('%s/%s.snap', $this->testsPath.'/'.$this->snapshotsPath.$relativePath, TestSuite::getInstance()->getDescription());
$description = TestSuite::getInstance()->getDescription();
if($this->getCurrentSnapshotCounter() > 1){
$description .= '__' . $this->getCurrentSnapshotCounter();
}
return sprintf('%s/%s.snap', $this->testsPath.'/'.$this->snapshotsPath.$relativePath, $description);
}
private function getCurrentSnapshotKey(): string
{
return TestSuite::getInstance()->getFilename().'###'.TestSuite::getInstance()->getDescription();
}
private function getCurrentSnapshotCounter(): int
{
return self::$expectationsCounter[$this->getCurrentSnapshotKey()] ?? 0;
}
public function startNewExpectation(): void
{
$key = $this->getCurrentSnapshotKey();
if(!isset(self::$expectationsCounter[$key])){
self::$expectationsCounter[$key] = 0;
}
self::$expectationsCounter[$key]++;
}
}

View File

@ -0,0 +1,7 @@
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Snapshot</h1>
</div>
</div>
</div>

View File

@ -0,0 +1,3 @@
{
"key": " <div class=\"container\">\n <div class=\"row\">\n <div class=\"col-md-12\">\n <h1>Snapshot<\/h1>\n <\/div>\n <\/div>\n <\/div>"
}

View File

@ -0,0 +1,7 @@
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Snapshot</h1>
</div>
</div>
</div>

View File

@ -0,0 +1,3 @@
{
"key": " <div class=\"container\">\n <div class=\"row\">\n <div class=\"col-md-12\">\n <h1>Snapshot<\/h1>\n <\/div>\n <\/div>\n <\/div>"
}

View File

@ -120,3 +120,15 @@ test('not failures', function () {
expect($this->snapshotable)->not->toMatchSnapshot();
})->throws(ExpectationFailedException::class);
test('multiple snapshot expectations', function () {
expect("foo bar 1")->toMatchSnapshot();
expect("foo bar 2")->toMatchSnapshot();
});
test('multiple snapshot expectations with datasets', function () {
expect("foo bar 1")->toMatchSnapshot();
expect("foo bar 2")->toMatchSnapshot();
})->with([1, 'foo', 'bar', 'baz']);