saloon icon indicating copy to clipboard operation
saloon copied to clipboard

feat(testing): allow merging data in mocked fixture responses

Open innocenzi opened this issue 1 year ago • 5 comments

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');

innocenzi avatar Jun 01 '24 04:06 innocenzi

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?

innocenzi avatar Jun 01 '24 04:06 innocenzi

@Sammyjo20 do you think you could take a look at that? this would really ease testing requests 🙏

innocenzi avatar Jun 12 '24 09:06 innocenzi

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.

innocenzi avatar Jun 22 '24 02:06 innocenzi

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?

Sammyjo20 avatar Jun 26 '24 06:06 Sammyjo20

That's a nice idea—I updated the PR!

innocenzi avatar Jun 26 '24 06:06 innocenzi

@Sammyjo20 any chance you could take another look at this?

innocenzi avatar Oct 31 '24 16:10 innocenzi

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.

Sammyjo20 avatar Dec 03 '24 00:12 Sammyjo20