Add callable for fakes

This commit is contained in:
philipp lang 2025-04-04 01:36:32 +02:00
parent f905c316ee
commit 7189511a55
3 changed files with 39 additions and 68 deletions

View File

@ -56,4 +56,29 @@ abstract class Fake
{ {
return Http::response('<html></html>'); return Http::response('<html></html>');
} }
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;
}
} }

View File

@ -121,9 +121,9 @@ class MemberFake extends Fake
} }
/** /**
* @param array<string, string|int|bool|null> $body * @param array<string, string|int|bool|null>|callable $body
*/ */
public function assertStored(int $groupId, array $body): void public function assertStored(int $groupId, array|callable $body): void
{ {
Http::assertSent(function ($request) use ($groupId, $body) { Http::assertSent(function ($request) use ($groupId, $body) {
$url = "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$groupId}"; $url = "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$groupId}";
@ -132,26 +132,14 @@ class MemberFake extends Fake
return false; return false;
} }
$requestBody = json_decode($request->body(), true); return $this->assertBodyMatches($request->body(), $body);
foreach ($body as $key => $value) {
if (!array_key_exists($key, $requestBody)) {
return false;
}
if ($requestBody[$key] !== $value) {
return false;
}
}
return true;
}); });
} }
/** /**
* @param array<string, string|int|bool|null> $body * @param array<string, string|int|bool|null>|callable $body
*/ */
public function assertUpdated(int $groupId, int $memberId, array $body): void public function assertUpdated(int $groupId, int $memberId, array|callable $body): void
{ {
Http::assertSent(function ($request) use ($groupId, $memberId, $body) { Http::assertSent(function ($request) use ($groupId, $memberId, $body) {
$url = "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$groupId}/{$memberId}"; $url = "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$groupId}/{$memberId}";
@ -160,19 +148,7 @@ class MemberFake extends Fake
return false; return false;
} }
$requestBody = json_decode($request->body(), true); return $this->assertBodyMatches($request->body(), $body);
foreach ($body as $key => $value) {
if (!array_key_exists($key, $requestBody)) {
return false;
}
if ($requestBody[$key] !== $value) {
return false;
}
}
return true;
}); });
} }

View File

@ -180,7 +180,7 @@ class MembershipFake extends Fake
}); });
} }
public function assertCreated(int $memberId, array $payload): void public function assertCreated(int $memberId, array|callable $payload): void
{ {
$url = "https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$memberId}"; $url = "https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$memberId}";
@ -189,39 +189,9 @@ class MembershipFake extends Fake
return false; return false;
} }
$requestBody = json_decode($request->body(), true); return $this->assertBodyMatches($request->body(), $payload);
foreach ($payload as $key => $value) {
if (!array_key_exists($key, $requestBody)) {
return false;
}
if ($requestBody[$key] !== $value) {
return false;
}
}
return true;
}); });
} }
public function assertUpdated(int $memberId, array $payload): void public function assertUpdated(int $memberId, array $payload): void
{ {
$url = "https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$memberId}/{$payload['id']}";
Http::assertSent(function ($request) use ($url, $payload) {
if ($request->url() !== $url || 'PUT' !== $request->method()) {
return false;
}
if (data_get($request, 'id') !== $payload['id']) {
return false;
}
if (data_get($request, 'aktivBis') !== data_get($payload, 'aktivBis')) {
return false;
}
return true;
});
}
}