From a0b413c22033d8af9c3cb0f4e0e9c0767244b53e Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 25 Jan 2024 15:59:17 +0000 Subject: [PATCH] docs: documentation handling dynamic data --- snapshot-testing.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/snapshot-testing.md b/snapshot-testing.md index 69c9375..364e5aa 100644 --- a/snapshot-testing.md +++ b/snapshot-testing.md @@ -33,6 +33,24 @@ And of course, you can "rebuild" the snapshots at any time by using the `--updat ./vendor/bin/pest --update-snapshots ``` +## Handling Dynamic Data + +Sometimes, the expected value may contain dynamic data that you cannot control, such as CSRF tokens in a form. In those cases, you can use [Expectation Pipes](/docs/custom-expectations#content-pipe-expectations) to replace that data. Here is an example: + +```php +expect()->pipe('toMatchSnapshot', function (Closure $next) { + if (is_string($this->value)) { + $this->value = preg_replace( + '/name="_token" value=".*"/', + 'name="_token" value="my_test"', + $this->value + ); + } + + return $next(); +}); +``` + --- In this chapter, we've seen how powerful snapshot testing is. In the following chapter, we will dive into Pest's custom helpers: [Custom Helpers](/docs/custom-helpers)