laravel-nami-api/src/Backend/FakeBackend.php

299 lines
10 KiB
PHP
Raw Normal View History

2021-05-13 23:25:00 +02:00
<?php
namespace Zoomyboy\LaravelNami\Backend;
2021-11-18 19:55:14 +01:00
use GuzzleHttp\Promise\PromiseInterface;
2021-07-17 16:57:59 +02:00
use GuzzleHttp\Psr7\Response as GuzzleResponse;
2021-06-13 11:24:23 +02:00
use Illuminate\Http\Client\Response;
2021-07-17 16:57:59 +02:00
use Illuminate\Support\Facades\Cache;
2021-06-13 11:24:23 +02:00
use Illuminate\Support\Facades\Http;
2021-06-21 22:37:23 +02:00
use Illuminate\Support\Str;
2021-11-17 00:26:23 +01:00
use Zoomyboy\LaravelNami\Fakes\Fake;
use Zoomyboy\LaravelNami\Fakes\FakeInstance;
use Zoomyboy\LaravelNami\Fakes\LoginFake;
2021-06-13 11:24:23 +02:00
2021-05-13 23:25:00 +02:00
class FakeBackend {
2021-11-17 00:26:23 +01:00
/**
* @param string $mglnr
*/
2021-11-17 22:33:25 +01:00
public function fakeLogin(string $mglnr): self
2021-11-17 00:26:23 +01:00
{
app(LoginFake::class)->succeeds($mglnr);
2021-11-17 22:33:25 +01:00
return $this;
}
/**
* @param int $mitgliedsNr
* @param array <string, mixed> $data
*/
public function addSearch(int $mitgliedsNr, array $data): self
{
Http::fake(function($request) use ($data, $mitgliedsNr) {
2021-11-17 23:40:47 +01:00
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode(['mitgliedsNummber' => $mitgliedsNr]) ?: '{}').'&page=1&start=0&limit=100') {
2021-11-17 22:33:25 +01:00
$content = [
'success' => true,
'data' => [$data],
'responseType' => 'OK',
'totalEntries' => 1,
];
return Http::response(json_encode($content) ?: '{}', 200);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeNationalities(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/baseadmin/staatsangehoerigkeit') {
return $this->dataResponse($data);
}
});
return $this;
}
/**
2021-11-18 19:55:14 +01:00
* @param array<string, string|int|true> $data
2021-11-17 22:33:25 +01:00
*/
public function fakeMember(array $data): self
2021-11-17 23:40:47 +01:00
{
return $this->fakeMembers([$data]);
}
2022-02-11 01:23:31 +01:00
public function singleMemberUrl(int $gruppierungId, int $memberId): string
{
return "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$gruppierungId}/{$memberId}";
}
2021-11-17 23:40:47 +01:00
/**
2021-11-18 19:55:14 +01:00
* @param array<int, array<string, mixed>> $data
2021-11-17 23:40:47 +01:00
*/
public function fakeMembers(array $data): self
2021-11-17 22:33:25 +01:00
{
Http::fake(function($request) use ($data) {
2021-11-17 23:40:47 +01:00
foreach ($data as $member) {
2022-02-11 01:23:31 +01:00
if ($request->url() === $this->singleMemberUrl($member['gruppierungId'], $member['id']) && $request->method() === 'GET') {
2021-11-17 23:40:47 +01:00
$content = [
'success' => true,
'data' => $member,
];
return Http::response(json_encode($content) ?: '{}', 200);
}
2022-01-02 21:49:57 +01:00
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$member['id']}/flist") {
$content = [
'success' => true,
'data' => array_map(function($membership) {
return (object) [
'entries_aktivVon' => $membership['aktivVon'],
'entries_aktivBis' => $membership['aktivBis'],
'entries_gruppierung' => $membership['gruppierung'],
'id' => $membership['id'],
'entries_taetigkeit' => $membership['taetigkeit'],
'entries_untergliederung' => $membership['untergliederung'],
];
}, $member['memberships'] ?? [])
];
return Http::response(json_encode($content) ?: '{}', 200);
}
2021-11-17 22:33:25 +01:00
}
2021-11-17 23:40:47 +01:00
foreach (collect($data)->chunk(100) as $i => $chunk) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode('{}').'&page='.($i+1).'&start='.($i*100).'&limit=100') {
return Http::response(json_encode([
'success' => true,
'totalEntries' => count($data),
'data' => collect($chunk)->map(fn ($member) => [
'entries_id' => $member['id'],
'entries_gruppierungId' => $member['gruppierungId'],
])->toArray(),
]) ?: '{}', 200);
}
2021-11-17 22:33:25 +01:00
}
2021-11-18 01:54:41 +01:00
foreach ($data as $member) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$member['id']}/flist") {
return Http::response(json_encode([
'success' => true,
2021-11-18 19:55:14 +01:00
'totalEntries' => collect($member['courses'] ?? [])->count(),
'data' => collect($member['courses'] ?? [])->map(fn ($course) => ['id' => $course['id']]),
2021-11-18 01:54:41 +01:00
]) ?: '{}', 200);
}
2021-11-18 02:16:12 +01:00
foreach ($member['courses'] ?? [] as $course) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$member['id']}/{$course['id']}") {
return Http::response(json_encode([
'success' => true,
'data' => $course,
]) ?: '{}', 200);
}
}
2021-11-18 01:54:41 +01:00
}
2021-11-17 22:33:25 +01:00
});
return $this;
}
2022-01-02 23:56:32 +01:00
public function fakeSingleMembership(int $memberId, int $membershipId, array $data) {
Http::fake(function($request) use ($data, $memberId, $membershipId) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/{$memberId}/{$membershipId}") {
$content = [
'success' => true,
'data' => $data,
'responseType' => 'OK',
];
return Http::response(json_encode($content) ?: '{}', 200);
}
});
return $this;
}
2021-11-17 22:33:25 +01:00
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeCountries(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/baseadmin/land') {
return $this->dataResponse($data);
}
});
return $this;
}
2021-11-18 01:54:41 +01:00
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeCourses(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/module/baustein') {
return $this->dataResponse($data);
}
});
return $this;
}
2021-11-17 22:33:25 +01:00
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeGenders(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/baseadmin/geschlecht') {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeRegions(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === 'https://nami.dpsg.de/ica/rest/baseadmin/region') {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param int $groupId
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeActivities(int $groupId, array $data): self
{
Http::fake(function($request) use ($data, $groupId) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/{$groupId}") {
return $this->dataResponse($data);
}
});
return $this;
}
/**
2021-11-18 19:55:14 +01:00
* @param array<int, array<int, array{name: string, id: int}>> $matches
2021-11-17 22:33:25 +01:00
*/
2021-11-18 19:55:14 +01:00
public function fakeSubactivities(array $matches): self
2021-11-17 22:33:25 +01:00
{
Http::fake(function($request) use ($matches) {
foreach ($matches as $activityId => $data) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/{$activityId}") {
return $this->dataResponse($data);
}
}
});
return $this;
}
/**
* @param int $groupId
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeFees(int $groupId, array $data): self
{
Http::fake(function($request) use ($data, $groupId) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/namiBeitrag/beitragsartmgl/gruppierung/{$groupId}") {
return $this->dataResponse($data);
}
});
return $this;
}
/**
* @param array<int, array{name: string, id: int}> $data
*/
public function fakeConfessions(array $data): self
{
Http::fake(function($request) use ($data) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/baseadmin/konfession") {
return $this->dataResponse($data);
}
});
return $this;
2021-11-17 00:26:23 +01:00
}
2021-11-18 19:55:14 +01:00
public function fakeFailedLogin(): void
2021-11-17 22:33:25 +01:00
{
2021-11-18 19:55:14 +01:00
app(LoginFake::class)->fails();
2021-06-13 11:24:23 +02:00
}
2021-11-17 22:33:25 +01:00
/**
* @param array<int, array{name: string, id: int}> $data
*/
2021-11-18 19:55:14 +01:00
private function dataResponse(array $data): PromiseInterface
2021-11-17 22:33:25 +01:00
{
$content = [
'success' => true,
'data' => collect($data)->map(fn ($item) => ['descriptor' => $item['name'], 'id' => $item['id'], 'name' => ''])->toArray(),
'responseType' => 'OK',
2021-11-18 19:55:14 +01:00
'totalEntries' => count($data),
2021-11-17 22:33:25 +01:00
];
return Http::response(json_encode($content) ?: '{}', 200);
}
2021-05-13 23:25:00 +02:00
}