Lint
This commit is contained in:
parent
2b7aca9b0d
commit
40c2df0d35
|
@ -6,10 +6,4 @@ use Illuminate\Support\Facades\Facade;
|
||||||
|
|
||||||
class Backend extends Facade {
|
class Backend extends Facade {
|
||||||
protected static function getFacadeAccessor() { return 'nami.backend'; }
|
protected static function getFacadeAccessor() { return 'nami.backend'; }
|
||||||
|
|
||||||
public static function fake() {
|
|
||||||
static::swap($api = new FakeBackend());
|
|
||||||
|
|
||||||
return $api;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace Zoomyboy\LaravelNami\Backend;
|
namespace Zoomyboy\LaravelNami\Backend;
|
||||||
|
|
||||||
use GuzzleHttp\Promise\FulfilledPromise;
|
use GuzzleHttp\Promise\PromiseInterface;
|
||||||
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
||||||
use Illuminate\Http\Client\Response;
|
use Illuminate\Http\Client\Response;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
@ -14,145 +14,6 @@ use Zoomyboy\LaravelNami\Fakes\LoginFake;
|
||||||
|
|
||||||
class FakeBackend {
|
class FakeBackend {
|
||||||
|
|
||||||
public ?array $loggedIn = null;
|
|
||||||
|
|
||||||
public function __construct() {
|
|
||||||
$this->members = collect([]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addMember(array $member) {
|
|
||||||
$member['mitgliedsNummer'] = $member['id'];
|
|
||||||
$this->members->push($member);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function init($cookie) {
|
|
||||||
return Http::withOptions(['cookies' => $cookie->forBackend()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function put($url, $data) {
|
|
||||||
if (is_null($this->cookie->getCookieByName('JSESSIONID'))) {
|
|
||||||
return $this->notAuthorizedResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->response([
|
|
||||||
'id' => $memberId
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->urlNotFoundException($url);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get($url) {
|
|
||||||
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" => []
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
])));
|
|
||||||
}
|
|
||||||
|
|
||||||
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"
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
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']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->urlNotFoundException($url);
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
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)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $mglnr
|
* @param string $mglnr
|
||||||
*/
|
*/
|
||||||
|
@ -199,7 +60,7 @@ class FakeBackend {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<string, string> $data
|
* @param array<string, string|int|true> $data
|
||||||
*/
|
*/
|
||||||
public function fakeMember(array $data): self
|
public function fakeMember(array $data): self
|
||||||
{
|
{
|
||||||
|
@ -207,7 +68,7 @@ class FakeBackend {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<int, array<string, string>> $data
|
* @param array<int, array<string, mixed>> $data
|
||||||
*/
|
*/
|
||||||
public function fakeMembers(array $data): self
|
public function fakeMembers(array $data): self
|
||||||
{
|
{
|
||||||
|
@ -240,8 +101,8 @@ class FakeBackend {
|
||||||
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$member['id']}/flist") {
|
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([
|
return Http::response(json_encode([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'totalEntries' => count($member['courses'] ?? []),
|
'totalEntries' => collect($member['courses'] ?? [])->count(),
|
||||||
'data' => collect($member['courses'])->map(fn ($course) => ['id' => $course['id']]),
|
'data' => collect($member['courses'] ?? [])->map(fn ($course) => ['id' => $course['id']]),
|
||||||
]) ?: '{}', 200);
|
]) ?: '{}', 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -331,10 +192,9 @@ class FakeBackend {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $groupId
|
* @param array<int, array<int, array{name: string, id: int}>> $matches
|
||||||
* @param array<int, array<int, array{name: string, id: int}>> $data
|
|
||||||
*/
|
*/
|
||||||
public function fakeSubactivities($matches): self
|
public function fakeSubactivities(array $matches): self
|
||||||
{
|
{
|
||||||
Http::fake(function($request) use ($matches) {
|
Http::fake(function($request) use ($matches) {
|
||||||
foreach ($matches as $activityId => $data) {
|
foreach ($matches as $activityId => $data) {
|
||||||
|
@ -376,30 +236,21 @@ class FakeBackend {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fakeFailedLogin(string $mglnr): void
|
public function fakeFailedLogin(): void
|
||||||
{
|
{
|
||||||
app(LoginFake::class)->fails($mglnr);
|
app(LoginFake::class)->fails();
|
||||||
}
|
|
||||||
|
|
||||||
public function asForm(): self
|
|
||||||
{
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function urlNotFoundException($url) {
|
|
||||||
throw new \Exception('no handler found for URL '.$url);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<int, array{name: string, id: int}> $data
|
* @param array<int, array{name: string, id: int}> $data
|
||||||
*/
|
*/
|
||||||
private function dataResponse(array $data): FulfilledPromise
|
private function dataResponse(array $data): PromiseInterface
|
||||||
{
|
{
|
||||||
$content = [
|
$content = [
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'data' => collect($data)->map(fn ($item) => ['descriptor' => $item['name'], 'id' => $item['id'], 'name' => ''])->toArray(),
|
'data' => collect($data)->map(fn ($item) => ['descriptor' => $item['name'], 'id' => $item['id'], 'name' => ''])->toArray(),
|
||||||
'responseType' => 'OK',
|
'responseType' => 'OK',
|
||||||
'totalEntries' => count ($data),
|
'totalEntries' => count($data),
|
||||||
];
|
];
|
||||||
|
|
||||||
return Http::response(json_encode($content) ?: '{}', 200);
|
return Http::response(json_encode($content) ?: '{}', 200);
|
||||||
|
|
|
@ -23,7 +23,7 @@ class LoginFake extends Fake {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fails(string $mglnr): void
|
public function fails(): void
|
||||||
{
|
{
|
||||||
Http::fake(function($request) {
|
Http::fake(function($request) {
|
||||||
if ($request->url() === 'https://nami.dpsg.de/ica/pages/login.jsp') {
|
if ($request->url() === 'https://nami.dpsg.de/ica/pages/login.jsp') {
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Zoomyboy\LaravelNami;
|
|
||||||
|
|
||||||
use PHPUnit\Framework\Assert;
|
|
||||||
use Zoomyboy\LaravelNami\Backend\Backend;
|
|
||||||
|
|
||||||
trait FakesNami {
|
|
||||||
|
|
||||||
public function fakeNami() {
|
|
||||||
Backend::fake();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function fakeNamiMembers($members) {
|
|
||||||
foreach ($members as $member) {
|
|
||||||
Backend::addMember($member);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function assertMemberExists($groupId, $data) {
|
|
||||||
Assert::assertNotNull($existing = Nami::member($groupId, $data['id']));
|
|
||||||
|
|
||||||
foreach ($data as $key => $value) {
|
|
||||||
Assert::assertEquals($value, $existing[$key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue