Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Add toSnapshot early return #1017

Merged
merged 2 commits into from
Nov 29, 2023
Merged

[2.x] Add toSnapshot early return #1017

merged 2 commits into from
Nov 29, 2023

Conversation

markhuot
Copy link
Contributor

Sometimes objects need native toString() and toArray() methods that are different from what you want to snapshot.

This adds an explicit toSnapshot() method that will be called first (when set) allowing for better snapshot values than the generic methods offer.

What:

  • Bug Fix
  • New Feature

Description:

I have a number of objects in my project that already contain toString() and toArray() methods for different reasons than snapshotting. For example a user object that may downcast to a string by rendering the full name or to an array by returning the raw DB attributes.

But for snapshotting I want a mixture of both. I want the attributes with some computed fields and even some hidden fields. I'd like to include a user's "token" field in the snapshot, even though it's hidden from API responses so I can have confidence that the token logic isn't changing.

This adds an explicit ->toSnapshot() method call above the more abstract methods allowing consumers to customize exactly what data is snapshotted.

Sometimes objects need native toString() and toArray() methods that are different from what you want to snapshot.

This adds an explicit toSnapshot() method that will be called first (when set) allowing for better snapshot values than the generic methods offer.
@devajmeireles devajmeireles changed the title Add toSnapshot() early return [2.x] Add toSnapshot early return Nov 24, 2023
@@ -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(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! Can you add a test on it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test added in 2a54b58

@markhuot
Copy link
Contributor Author

Test added and this is ready for re-review. Test passes locally (as does the rest of the suite).

@nunomaduro nunomaduro merged commit 3974a65 into pestphp:2.x Nov 29, 2023
@markhuot markhuot deleted the patch-2 branch December 1, 2023 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants