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

409 lines
13 KiB
PHP
Raw Normal View History

2021-05-13 23:25:00 +02:00
<?php
namespace Zoomyboy\LaravelNami\Backend;
2021-11-17 22:33:25 +01:00
use GuzzleHttp\Promise\FulfilledPromise;
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
public ?array $loggedIn = null;
2021-05-13 23:25:00 +02:00
2021-06-13 11:24:23 +02:00
public function __construct() {
$this->members = collect([]);
}
public function addMember(array $member) {
2021-06-21 22:37:23 +02:00
$member['mitgliedsNummer'] = $member['id'];
2021-06-13 11:24:23 +02:00
$this->members->push($member);
}
2021-06-21 22:37:23 +02:00
public function init($cookie) {
2021-11-17 00:26:23 +01:00
return Http::withOptions(['cookies' => $cookie->forBackend()]);
2021-06-13 11:24:23 +02:00
}
public function put($url, $data) {
2021-06-18 19:29:21 +02:00
if (is_null($this->cookie->getCookieByName('JSESSIONID'))) {
return $this->notAuthorizedResponse();
}
2021-06-13 11:24:23 +02:00
if (preg_match('|/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/([0-9]+)/([0-9]+)|', $url, $matches)) {
list($url, $groupId, $memberId) = $matches;
$existing = $this->members->search(function($m) use ($groupId, $memberId) {
return $m['gruppierungId'] == $groupId && $m['id'] == $memberId;
});
if ($existing !== false) {
$this->members[$existing] = $data;
}
2021-06-18 19:29:21 +02:00
return $this->response([
'id' => $memberId
]);
2021-06-13 11:24:23 +02:00
}
2021-06-13 15:43:49 +02:00
$this->urlNotFoundException($url);
2021-06-13 11:24:23 +02:00
}
public function get($url) {
2021-06-13 15:43:49 +02:00
if (preg_match('|/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/([0-9]+)/flist|', $url, $matches)) {
list($url, $groupId) = $matches;
$members = $this->members->filter(function($m) use ($groupId) {
return $m['gruppierungId'] == $groupId;
})->map(function($member) {
return [
"entries_id" => $member['id'],
"id" => $member['id'],
"entries_mitgliedsNummer" => $member['id'],
];
});
return $this->response([
"success" => true,
'data' => $members,
"responseType" => "OK",
"metaData" => []
]);
}
2021-06-13 11:24:23 +02:00
if (preg_match('|/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/([0-9]+)/([0-9]+)|', $url, $matches)) {
list($url, $groupId, $memberId) = $matches;
$member = $this->members->first(function($m) use ($groupId, $memberId) {
return $m['gruppierungId'] == $groupId && $m['id'] == $memberId;
});
return new Response(new GuzzleResponse(200, [], json_encode([
'success' => true,
'data' => $member
])));
}
2021-06-13 15:43:49 +02:00
if ($url === 'https://nami.dpsg.de/ica/pages/login.jsp') {
return;
}
if ($url === 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root') {
$groups = collect(data_get($this->groups, $this->loggedInAs))->map(function($group) {
return [
"descriptor" => "Solingen-Wald, Silva 100105",
"name" => "",
"representedClass" => "de.iconcept.nami.entity.org.Gruppierung",
"id" => $group
];
})->toArray();
return $this->response([
"success" => true,
"data" => $groups,
"responseType" => "OK"
]);
}
2021-06-21 22:37:23 +02:00
if (Str::contains($url, 'search-multi/result-list')) {
$query = parse_url($url)['query'];
parse_str($query, $q);
$params = json_decode($q['searchedValues'], true);
if (array_keys($params) === ['mitgliedsNummber']) {
return $this->findNr($params['mitgliedsNummber']);
}
}
2021-06-13 15:43:49 +02:00
$this->urlNotFoundException($url);
}
2021-06-21 22:37:23 +02:00
public function findNr($nr) {
$found = $this->members->first(fn($m) => $m['id'] === $nr);
$found = [
'entries_mitgliedsNummer' => $found['mitgliedsNummer'],
'entries_vorname' => $found['vorname'],
'entries_nachname' => $found['nachname'],
];
return $this->response([
"success" => true,
"data" => [$found],
"responseType" => "OK",
"totalEntries" => 1
]);
}
2021-06-13 15:43:49 +02:00
private function wrongLoginResponse() {
return $this->response([
"servicePrefix" => null,
"methodCall" => null,
"response" => null,
"statusCode" => 3000,
"statusMessage" => "Benutzer nicht gefunden oder Passwort falsch.",
"apiSessionName" => "JSESSIONID",
"apiSessionToken" => "qrjlt_YFVhtRPU-epc-58AB1",
"minorNumber" => 0,
"majorNumber" => 0,
]);
}
public function response($data) {
return new Response(new GuzzleResponse(200, [], json_encode($data)));
}
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;
}
/**
* @param array<string, string> $data
*/
public function fakeMember(array $data): self
2021-11-17 23:40:47 +01:00
{
return $this->fakeMembers([$data]);
}
/**
* @param array<int, array<string, string>> $data
*/
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) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$member['gruppierungId']}/{$member['id']}") {
$content = [
'success' => true,
'data' => $member,
];
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,
'totalEntries' => count($member['courses'] ?? []),
2021-11-18 02:16:12 +01:00
'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;
}
/**
* @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;
}
/**
* @param int $groupId
* @param array<int, array<int, array{name: string, id: int}>> $data
*/
public function fakeSubactivities($matches): self
{
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
}
public function fakeFailedLogin(string $mglnr): void
{
app(LoginFake::class)->fails($mglnr);
2021-06-13 15:43:49 +02:00
}
2021-11-17 22:33:25 +01:00
public function asForm(): self
{
2021-06-13 15:43:49 +02:00
return $this;
}
public function urlNotFoundException($url) {
throw new \Exception('no handler found for URL '.$url);
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
*/
private function dataResponse(array $data): FulfilledPromise
{
$content = [
'success' => true,
'data' => collect($data)->map(fn ($item) => ['descriptor' => $item['name'], 'id' => $item['id'], 'name' => ''])->toArray(),
'responseType' => 'OK',
'totalEntries' => count ($data),
];
return Http::response(json_encode($content) ?: '{}', 200);
}
2021-05-13 23:25:00 +02:00
}