From bd2645804149853384bb7d703122e79e3dc0cc79 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 6 Apr 2025 16:59:50 +0200 Subject: [PATCH] Add body matches function --- src/Fakes/Fake.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Fakes/Fake.php b/src/Fakes/Fake.php index a3395d5..91af1d4 100644 --- a/src/Fakes/Fake.php +++ b/src/Fakes/Fake.php @@ -56,4 +56,30 @@ abstract class Fake { return Http::response(''); } + + public function assertBodyMatches(string $is, array|callable $should): bool + { + $requestBody = json_decode($is, true); + + if (!is_array($requestBody)) { + return false; + } + + if (is_callable($should)) { + return $should($requestBody); + } + + foreach ($should as $key => $value) { + if (!array_key_exists($key, $requestBody)) { + return false; + } + + if ($requestBody[$key] !== $value) { + return false; + } + } + + return true; + } + }