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

Allow passing variables to Snapshot to enable dynamic responses #190

Open
chr-hertel opened this issue Mar 28, 2024 · 0 comments · May be fixed by #191
Open

Allow passing variables to Snapshot to enable dynamic responses #190

chr-hertel opened this issue Mar 28, 2024 · 0 comments · May be fixed by #191

Comments

@chr-hertel
Copy link

chr-hertel commented Mar 28, 2024

Hi there,

first of all thanks for the package, it's more like a standard for me to install when using controller level tests etc. Thanks!

There's one thing that I mostly run into and had different strategies to get around: dynamic values

Let's assume if test the json response of a POST endpoint for creating a resource and unfortunately I'm unable to use uuid generated on client side but auto increment (i know 🙈)

POST /api/book

Response:

{
     "id": "123456",
     "title": "Some Title",
     "author": "Jane Doe",
     "ISBN": "978-0-545-01022-1",
     "cover": "https://bucket.foo/bar/9128124.webp",
     "published": 2017,
     "createdAt": "2024-03-08T17:37:09+00:00"
     "more": "values"
}

And id and createdAt change every time. Yes, there are different strategies possible (e.g. mocking on api side, custom driver, stripping those parts ...), but I wanted to propose a built-in solution to support this out of the box:

#[DataProvider('provideExampleRequests')]
public function testCreateBookEndpoint(string $requestBody): void
{
    $response = $this->client->('POST', $requestBody);

    // explicit template method
    $this->assertMatchesJsonPathSnapshot($response, [
         '$.id' => '@\d@',
         '$.createdAt' => '@([\d:\-\+])@',
    ];
}

With the key being a json path and the key a regex.

Honestly, not so sure if that's easy, but wanted to get your opinion and check if i'm missing an obvious solution here.

WDYT?

PS: OC I'd be up to work on the solution if you consider this feature valuable.

@chr-hertel chr-hertel linked a pull request Apr 2, 2024 that will close this issue
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 a pull request may close this issue.

1 participant