mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
multiple snapshots
This commit is contained in:
@ -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;
|
||||
|
||||
@ -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]++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1 @@
|
||||
foo bar 1
|
||||
@ -0,0 +1 @@
|
||||
foo bar 2
|
||||
@ -0,0 +1 @@
|
||||
foo bar 1
|
||||
@ -0,0 +1 @@
|
||||
foo bar 2
|
||||
@ -0,0 +1 @@
|
||||
foo bar 1
|
||||
@ -0,0 +1 @@
|
||||
foo bar 2
|
||||
@ -0,0 +1 @@
|
||||
foo bar 1
|
||||
@ -0,0 +1 @@
|
||||
foo bar 2
|
||||
@ -0,0 +1 @@
|
||||
foo bar 1
|
||||
@ -0,0 +1 @@
|
||||
foo bar 2
|
||||
@ -0,0 +1,7 @@
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<h1>Snapshot</h1>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -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>"
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<h1>Snapshot</h1>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -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>"
|
||||
}
|
||||
@ -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']);
|
||||
|
||||
Reference in New Issue
Block a user