Skip to content

Commit

Permalink
Merge pull request #1017 from markhuot/patch-2
Browse files Browse the repository at this point in the history
[2.x] Add `toSnapshot` early return
  • Loading branch information
nunomaduro authored Nov 29, 2023
2 parents 7177791 + 2a54b58 commit 3974a65
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Mixins/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ public function toMatchSnapshot(string $message = ''): self

$string = match (true) {
is_string($this->value) => $this->value,
is_object($this->value) && method_exists($this->value, 'toSnapshot') => $this->value->toSnapshot(),
is_object($this->value) && method_exists($this->value, '__toString') => $this->value->__toString(),
is_object($this->value) && method_exists($this->value, 'toString') => $this->value->toString(),
$this->value instanceof \Illuminate\Testing\TestResponse => $this->value->getContent(), // @phpstan-ignore-line
Expand Down
Original file line number Diff line number Diff line change
@@ -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>"
}
20 changes: 20 additions & 0 deletions tests/Features/Expect/toMatchSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ public function toArray()
])->toMatchSnapshot();
});

test('pass with `toSnapshot`', function () {
TestSuite::getInstance()->snapshots->save(json_encode(['key' => $this->snapshotable], JSON_PRETTY_PRINT));

$object = new class($this->snapshotable)
{
public function __construct(protected string $snapshotable)
{
}

public function toSnapshot()
{
return json_encode([
'key' => $this->snapshotable,
], JSON_PRETTY_PRINT);
}
};

expect($object)->toMatchSnapshot();
});

test('failures', function () {
TestSuite::getInstance()->snapshots->save($this->snapshotable);

Expand Down

0 comments on commit 3974a65

Please sign in to comment.