diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 1b8cfa42..6eed299a 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -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; diff --git a/src/Repositories/SnapshotRepository.php b/src/Repositories/SnapshotRepository.php index 78b2ed11..da321dfb 100644 --- a/src/Repositories/SnapshotRepository.php +++ b/src/Repositories/SnapshotRepository.php @@ -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]++; } } diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations.snap new file mode 100644 index 00000000..7b5a8676 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations.snap @@ -0,0 +1 @@ +foo bar 1 \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations__2.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations__2.snap new file mode 100644 index 00000000..206d0d02 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations__2.snap @@ -0,0 +1 @@ +foo bar 2 \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set___1____1_.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set___1____1_.snap new file mode 100644 index 00000000..7b5a8676 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set___1____1_.snap @@ -0,0 +1 @@ +foo bar 1 \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set___1____1___2.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set___1____1___2.snap new file mode 100644 index 00000000..206d0d02 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set___1____1___2.snap @@ -0,0 +1 @@ +foo bar 2 \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____bar______bar__.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____bar______bar__.snap new file mode 100644 index 00000000..7b5a8676 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____bar______bar__.snap @@ -0,0 +1 @@ +foo bar 1 \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____bar______bar____2.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____bar______bar____2.snap new file mode 100644 index 00000000..206d0d02 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____bar______bar____2.snap @@ -0,0 +1 @@ +foo bar 2 \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____baz______baz__.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____baz______baz__.snap new file mode 100644 index 00000000..7b5a8676 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____baz______baz__.snap @@ -0,0 +1 @@ +foo bar 1 \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____baz______baz____2.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____baz______baz____2.snap new file mode 100644 index 00000000..206d0d02 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____baz______baz____2.snap @@ -0,0 +1 @@ +foo bar 2 \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____foo______foo__.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____foo______foo__.snap new file mode 100644 index 00000000..7b5a8676 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____foo______foo__.snap @@ -0,0 +1 @@ +foo bar 1 \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____foo______foo____2.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____foo______foo____2.snap new file mode 100644 index 00000000..206d0d02 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/multiple_snapshot_expectations_with_datasets_with_data_set____foo______foo____2.snap @@ -0,0 +1 @@ +foo bar 2 \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with______toString___2.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with______toString___2.snap new file mode 100644 index 00000000..c2b4dc0a --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with______toString___2.snap @@ -0,0 +1,7 @@ +
+
+
+

Snapshot

+
+
+
\ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with__toArray___2.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with__toArray___2.snap new file mode 100644 index 00000000..afd4f5f9 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with__toArray___2.snap @@ -0,0 +1,3 @@ +{ + "key": "
\n
\n
\n

Snapshot<\/h1>\n <\/div>\n <\/div>\n <\/div>" +} \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with__toString___2.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with__toString___2.snap new file mode 100644 index 00000000..c2b4dc0a --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with__toString___2.snap @@ -0,0 +1,7 @@ +
+
+
+

Snapshot

+
+
+
\ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with_array__2.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with_array__2.snap new file mode 100644 index 00000000..afd4f5f9 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with_array__2.snap @@ -0,0 +1,3 @@ +{ + "key": "
\n
\n
\n

Snapshot<\/h1>\n <\/div>\n <\/div>\n <\/div>" +} \ No newline at end of file diff --git a/tests/Features/Expect/toMatchSnapshot.php b/tests/Features/Expect/toMatchSnapshot.php index 91647f48..655ffbf0 100644 --- a/tests/Features/Expect/toMatchSnapshot.php +++ b/tests/Features/Expect/toMatchSnapshot.php @@ -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']);