feat(testing): allow merging data in mocked fixture responses
Closes https://github.com/saloonphp/saloon/issues/416
This pull request allows merging data in fixtures:
MockResponse::fixture('user', merge: [
'name' => 'Sam Carré',
])
This is useful when the mocked response is used by other part of the code that depends on it, and assertions are performed later on the result of that code.
One (very) simple example of that would be testing if a data transfer object can be created:
$response = connector()->send(new DTORequest, new MockClient([
MockResponse::fixture('user', merge: [
'name' => 'Sam Carré',
]),
]));
expect($response->dto())
->name->toBe('Sam Carré') // Sam Carré instead of Sam
->actualName->toBe('Sam')
->twitter->toBe('@carre_sam');
One small concern I have is that this code uses array_merge, while I'm certain to have use cases where array_merge_recursive would be more useful.
I wanted to stay consistent with implementations of MergeableBody which all use array_merge. Would you be open to a PR that updates all of these to array_merge_recursive?
@Sammyjo20 do you think you could take a look at that? this would really ease testing requests 🙏
You can amend my previous concern, I refactored the pull request to support dot-notation, which fixes the issue.
I've been testing this in a work project, array_merge was not enough most of the time, and I needed dot-notation support for most fixtures.
Hey @innocenzi thank you for this PR, it looks like a great addition! The only thing I ask is instead of having it as a second argument could we have a method that chains onto the ::fixture() call? Like MockResponse::fixture()->merge([]) because I've got an upcoming feature that will also share this API so it would be good if it was unified. What do you think?
That's a nice idea—I updated the PR!
@Sammyjo20 any chance you could take another look at this?
Hi @innocenzi sorry about the delay. I'm going through all of Saloon's issues and PRs again at the moment and I will update accordingly.