From 53dcda68fc3b9682ba0c065223056412547ec56c Mon Sep 17 00:00:00 2001 From: philipp lang Date: Wed, 17 Nov 2021 22:33:25 +0100 Subject: [PATCH] Ad FakeMember --- src/Api.php | 40 +++--- src/Authentication/NamiGuard.php | 6 +- src/Backend/FakeBackend.php | 204 ++++++++++++++++++++++++++++--- src/NamiUser.php | 21 ++-- 4 files changed, 231 insertions(+), 40 deletions(-) diff --git a/src/Api.php b/src/Api.php index 3e49a20..d9ac0a0 100644 --- a/src/Api.php +++ b/src/Api.php @@ -29,27 +29,37 @@ class Api { return Backend::init($this->cookie); } - public function findNr($nr) { - return $this->search(['mitgliedsNummber' => $nr]); + public function findNr(int $nr): Member + { + return $this->find(['mitgliedsNummber' => $nr]); } - public function search($payload) { - $url = self::$url.'/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode($payload)).'&page=1&start=0&limit=10'; + /** + * @param array $payload + */ + public function find(array $payload): ?Member + { + return $this->search($payload)->first(); + } + + /** + * @param array $payload + * @return Collection + */ + public function search(array $payload): Collection + { + $url = self::$url.'/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode(json_encode($payload) ?: '[]').'&page=1&start=0&limit=10'; $response = $this->http()->get($url); - if ($response->json()['success'] === true) { - - if (!count($response->json()['data'])) { - return null; - } - $data = collect($response->json()['data'][0])->mapWithKeys(function($value, $key) { - return [ str_replace('entries_', '', $key) => $value ]; - }); - - return Member::fromNami($data); + if ($response->json()['success'] !== true) { + $this->exception('Search failed', ['url' => $url], $response->json()); } - $this->exception('Search failed', ['url' => $url], $response->json()); + return collect($response->json()['data'])->map(function($member) { + return Member::fromNami(collect($member)->mapWithKeys(function($value, $key) { + return [ str_replace('entries_', '', (string) $key) => $value ]; + })); + }); } protected function loggedInAlready(): bool { diff --git a/src/Authentication/NamiGuard.php b/src/Authentication/NamiGuard.php index e2aafc8..6965b30 100644 --- a/src/Authentication/NamiGuard.php +++ b/src/Authentication/NamiGuard.php @@ -69,9 +69,13 @@ class NamiGuard { { try { $api = Nami::login($credentials['mglnr'], $credentials['password']); + $user = $api->findNr($credentials['mglnr']); $payload = [ - 'credentials' => $credentials + 'credentials' => $credentials, + 'firstname' => $user->firstname, + 'lastname' => $user->lastname, + 'group_id' => $user->group_id, ]; $this->setUser(NamiUser::fromPayload($payload)); diff --git a/src/Backend/FakeBackend.php b/src/Backend/FakeBackend.php index 558a26d..5c5e489 100644 --- a/src/Backend/FakeBackend.php +++ b/src/Backend/FakeBackend.php @@ -2,6 +2,7 @@ namespace Zoomyboy\LaravelNami\Backend; +use GuzzleHttp\Promise\FulfilledPromise; use GuzzleHttp\Psr7\Response as GuzzleResponse; use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Cache; @@ -148,30 +149,185 @@ class FakeBackend { ]); } - private function notAuthorizedResponse() { - return $this->response([ - 'success' => true, - 'data' => null, - 'responseType' => 'ERROR', - 'message' => 'Session expired', - 'title' => 'Exception', - ]); - } - public function response($data) { return new Response(new GuzzleResponse(200, [], json_encode($data))); } /** * @param string $mglnr - * @param array $groups */ - public function fakeLogin(string $mglnr, array $groups): void + public function fakeLogin(string $mglnr): self { app(LoginFake::class)->succeeds($mglnr); - foreach ($groups as $group) { - GroupFake::addGroup($group); - } + + return $this; + } + + /** + * @param int $mitgliedsNr + * @param array $data + */ + public function addSearch(int $mitgliedsNr, array $data): self + { + Http::fake(function($request) use ($data, $mitgliedsNr) { + 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=10') { + $content = [ + 'success' => true, + 'data' => [$data], + 'responseType' => 'OK', + 'totalEntries' => 1, + ]; + return Http::response(json_encode($content) ?: '{}', 200); + } + }); + + return $this; + } + + /** + * @param array $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 $data + */ + public function fakeMember(array $data): self + { + Http::fake(function($request) use ($data) { + if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode('[]').'&page=1&start=0&limit=10') { + return Http::response(json_encode([ + 'success' => true, + 'data' => [[ + 'entries_id' => $data['id'], + 'entries_gruppierungId' => $data['gruppierungId'], + ]] + ]) ?: '{}', 200); + } + + if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/{$data['gruppierungId']}/{$data['id']}") { + $content = [ + 'success' => true, + 'data' => $data, + ]; + + return Http::response(json_encode($content) ?: '{}', 200); + } + }); + + return $this; + } + + /** + * @param array $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; + } + + /** + * @param array $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 $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 $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> $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 $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 $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; } public function fakeFailedLogin(string $mglnr): void @@ -179,7 +335,8 @@ class FakeBackend { app(LoginFake::class)->fails($mglnr); } - public function asForm() { + public function asForm(): self + { return $this; } @@ -187,4 +344,19 @@ class FakeBackend { throw new \Exception('no handler found for URL '.$url); } + /** + * @param array $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); + } + } diff --git a/src/NamiUser.php b/src/NamiUser.php index de74bd5..15784e5 100644 --- a/src/NamiUser.php +++ b/src/NamiUser.php @@ -2,24 +2,33 @@ namespace Zoomyboy\LaravelNami; +use Cache; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Database\Eloquent\Model; -use Cache; class NamiUser implements Authenticatable { public $mglnr; public $password; + public string $firstname; + public string $lastname; + public int $group_id; public function __construct($attributes) { $this->mglnr = $attributes['mglnr']; $this->password = $attributes['password']; + $this->firstname = $attributes['firstname']; + $this->lastname = $attributes['lastname']; + $this->group_id = $attributes['group_id']; } public static function fromPayload($payload) { $user = new static([ 'mglnr' => data_get($payload, 'credentials.mglnr'), 'password' => data_get($payload, 'credentials.password'), + 'firstname' => data_get($payload, 'firstname'), + 'lastname' => data_get($payload, 'lastname'), + 'group_id' => data_get($payload, 'group_id'), ]); return $user; @@ -30,7 +39,7 @@ class NamiUser implements Authenticatable { } public function getNamiGroupId() { - return $this->api()->findNr($this->mglnr)->group_id; + return $this->group_id; } public function getAuthIdentifierName() { @@ -42,15 +51,11 @@ class NamiUser implements Authenticatable { } public function getFirstname() { - return Cache::remember('member-'.$this->mglnr.'-firstname', 3600, function() { - return $this->api()->findNr($this->mglnr)->firstname; - }); + return $this->firstname; } public function getLastname() { - return Cache::remember('member-'.$this->mglnr.'-lastname', 3600, function() { - return $this->api()->findNr($this->mglnr)->lastname; - }); + return $this->firstname; } public function getAuthIdentifier() {